Class: SuggestedEditController

Inherits:
ApplicationController show all
Defined in:
app/controllers/suggested_edit_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#dashboard, #keyboard_tools, #upload

Instance Method Details

#approveObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/suggested_edit_controller.rb', line 19

def approve
  unless @edit.active?
    render json: { status: 'error', message: 'This edit has already been reviewed.' }, status: :conflict
    return
  end

  @post = @edit.post
  unless check_your_privilege('edit_posts', @post, false)
    render(json: { status: 'error', message: helpers.ability_err_msg(:edit_posts, 'review suggested edits') },
           status: :bad_request)

    return
  end

  comment = params[:comment].present? && !params[:comment].empty? ? params[:comment] : @edit.comment

  # The to_a / dup methods called on the tags for `opts` and `before` are necessary.
  # We need to work on a copy of them, because we update the post before the edit, which will change their values.
  # (We would otherwise be pointing to the same instance, and only see the updated version).
  opts = { before: @post.body_markdown,
           after: @edit.body_markdown,
           comment: comment,
           before_title: @post.title,
           after_title: @edit.title,
           before_tags: @post.tags.to_a,
           after_tags: @edit.tags }

  before = { before_body: @post.body,
             before_body_markdown: @post.body_markdown,
             before_tags_cache: @post.tags_cache.dup,
             before_tags: @post.tags.to_a,
             before_title: @post.title }

  @post.transaction do
    post_update_status = @post.update(applied_details)

    if post_update_status
      edit_update_status = @edit.update(before.merge(active: false,
                                                     accepted: true,
                                                     comment: comment,
                                                     rejected_comment: '',
                                                     decided_at: DateTime.now,
                                                     decided_by: current_user,
                                                     updated_at: DateTime.now))

      if @edit.errors.any?
        @post.errors.merge!(@edit.errors)
        raise ActiveRecord::Rollback
      end

      if edit_update_status
        PostHistory.post_edited(@post, @edit.user, **opts)
        AbilityQueue.add(@edit.user, "Suggested Edit Approved ##{@edit.id}")
      end
    end

    next
  end

  if @post.errors.any?
    render json: { status: 'error', message: @post.errors.full_messages.join(', ') }, status: :bad_request
  else
    flash[:success] = 'Edit approved successfully.'
    render json: { status: 'success', redirect_url: post_path(@post) }
  end
end

#category_indexObject



4
5
6
7
8
9
10
11
12
13
# File 'app/controllers/suggested_edit_controller.rb', line 4

def category_index
  @category = params[:category].present? ? Category.find(params[:category]) : nil
  @edits = if params[:show_decided].present? && params[:show_decided] == '1'
             SuggestedEdit.where(post: Post.undeleted.where(category: @category), active: false) \
                          .order('created_at DESC')
           else
             SuggestedEdit.where(post: Post.undeleted.where(category: @category), active: true) \
                          .order('created_at ASC')
           end
end

#rejectObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/suggested_edit_controller.rb', line 86

def reject
  unless @edit.active?
    render json: { status: 'error', message: 'This edit has already been reviewed.' }, status: :conflict
    return
  end

  @post = @edit.post

  unless check_your_privilege('edit_posts', @post, false)
    render(json: { status: 'error', redirect_url: helpers.ability_err_msg(:edit_posts, 'review suggested edits') },
           status: :bad_request)

    return
  end

  now = DateTime.now

  if @edit.update(active: false, accepted: false, rejected_comment: params[:rejection_comment], decided_at: now,
                  decided_by: current_user, updated_at: now)
    flash[:success] = 'Edit rejected successfully.'
    AbilityQueue.add(@edit.user, "Suggested Edit Rejected ##{@edit.id}")
    render json: { status: 'success', redirect_url: helpers.generic_share_link(@post) }
  else
    render json: { status: 'error', redirect_url: 'Cannot reject this suggested edit... Strange.' },
           status: :bad_request
  end
end

#showObject



15
16
17
# File 'app/controllers/suggested_edit_controller.rb', line 15

def show
  render layout: 'without_sidebar'
end