Class: PlanMyStuff::CommentsController

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

Instance Method Summary collapse

Instance Method Details

#createObject

POST /issues/:issue_id/comments



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/plan_my_stuff/comments_controller.rb', line 6

def create
  @issue = PlanMyStuff::Issue.find(params[:issue_id].to_i, repo: params[:repo])

  PlanMyStuff::Comment.create!(
    issue: @issue,
    body: comment_params[:body],
    user: pms_current_user,
    visibility: comment_params[:visibility]&.to_sym || :public,
    waiting_on_reply: comment_params[:waiting_on_reply] == '1',
  )

  flash[:success] = 'Comment was successfully created.'
  redirect_to(plan_my_stuff.issue_path(@issue.number, repo: @issue.repo.full_name))
rescue PlanMyStuff::LockedIssueError => e
  pms_handle_rescue(e)
  flash[:error] = 'This issue is locked; no new comments can be posted.'
  redirect_to(plan_my_stuff.issue_path(@issue.number, repo: @issue.repo.full_name))
end

#editObject

GET /issues/:issue_id/comments/:id/edit



26
27
28
29
30
31
32
33
# File 'app/controllers/plan_my_stuff/comments_controller.rb', line 26

def edit
  load_comment
  return unless @comment
  return redirect_to_issue if issue_body_comment?
  return if can_edit?(@comment)

  redirect_to_unauthorized(plan_my_stuff.issue_path(@issue.number, repo: @issue.repo.full_name))
end

#updateObject

PATCH/PUT /issues/:issue_id/comments/:id



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/plan_my_stuff/comments_controller.rb', line 36

def update
  load_comment
  return unless @comment
  return redirect_to_issue if issue_body_comment?

  unless can_edit?(@comment)
    redirect_to_unauthorized(plan_my_stuff.issue_path(@issue.number, repo: @issue.repo.full_name))

    return
  end

  update_attrs = { body: comment_params[:body] }
  update_attrs[:visibility] = comment_params[:visibility].to_sym if @support_user && comment_params[:visibility]

  @comment.update!(**update_attrs, user: pms_current_user)

  flash[:success] = 'Comment was successfully updated.'
  redirect_to(plan_my_stuff.issue_path(@issue.number, repo: @issue.repo.full_name))
rescue PlanMyStuff::StaleObjectError => e
  pms_handle_rescue(e)
  flash.now[:error] = 'Comment was modified by someone else. Please review the latest changes and try again.'
  render(:edit, status: PlanMyStuff.unprocessable_status)
end