Module: Unsakini::CommentOwnerControllerConcern

Extended by:
ActiveSupport::Concern
Included in:
CommentsController, ShareBoardController
Defined in:
app/controllers/concerns/unsakini/comment_owner_controller_concern.rb

Overview

Ensures user is owner of the comment and sets the ‘@comment` variable in the controllers

Instance Method Summary collapse

Instance Method Details

#ensure_commentObject

Ensures user is owner of the comment and sets the ‘@comment` variable in the controllers



7
8
9
10
11
12
13
14
# File 'app/controllers/concerns/unsakini/comment_owner_controller_concern.rb', line 7

def ensure_comment
  post_id = params[:post_id]
  comment_id = params[:comment_id] || params[:id]
  result = has_comment_access post_id, comment_id
  @comment = result[:comment]
  status = result[:status]
  head status if status != :ok
end

#ensure_comment_ownerObject

Ensures user is the owner of the comment. Must be run after #ensure_comment method.



30
31
32
# File 'app/controllers/concerns/unsakini/comment_owner_controller_concern.rb', line 30

def ensure_comment_owner
  render json: {}, status: :forbidden if @comment.user_id != @user.id
end

#has_comment_access(post_id, comment_id) ⇒ Object

Validate if user has access to comment in the post

Parameters:

  • post_id (Integer)

    post id

  • comment_id (Integer)

    comment id



20
21
22
23
24
25
26
27
# File 'app/controllers/concerns/unsakini/comment_owner_controller_concern.rb', line 20

def has_comment_access(post_id, comment_id)
  comment = Unsakini::Comment.where(id: comment_id, post_id: post_id, user_id: @user.id).first
  if comment.nil?
    return {status: :forbidden, comment: comment}
  else
    return {status: :ok, comment: comment}
  end
end