scinote-web/app/controllers/comments_controller.rb

108 lines
3.1 KiB
Ruby
Raw Normal View History

2020-11-20 19:29:40 +08:00
# frozen_string_literal: true
class CommentsController < ApplicationController
2020-11-21 03:25:28 +08:00
include ActionView::Helpers::TextHelper
include InputSanitizeHelper
include ApplicationHelper
include CommentHelper
2020-11-20 19:29:40 +08:00
before_action :load_object, only: %i(index create)
2020-11-21 03:25:28 +08:00
before_action :load_comment, only: %i(update destroy)
2020-11-20 19:29:40 +08:00
before_action :check_view_permissions, only: :index
before_action :check_create_permissions, only: :create
2020-11-21 03:25:28 +08:00
before_action :check_manage_permissions, only: %i(update destroy)
2020-11-20 19:29:40 +08:00
def index
comments = @commentable.comments.order(created_at: :asc)
2020-11-20 19:29:40 +08:00
render json: {
object_name: @commentable.name,
comment_addable: comment_addable?(@commentable),
2020-11-20 19:29:40 +08:00
comments: render_to_string(partial: 'shared/comments/comments_list.html.erb',
locals: { comments: comments })
}
end
2020-11-21 03:25:28 +08:00
def create
@comment = @commentable.comments.new(
message: params[:message],
user: current_user,
associated_id: @commentable.id
)
comment_create_helper(@comment, 'comment')
2020-11-21 03:25:28 +08:00
end
2020-11-20 19:29:40 +08:00
2020-11-21 03:25:28 +08:00
def update
old_text = @comment.message
@comment.message = params[:message]
comment_update_helper(@comment, old_text, 'comment')
end
2020-11-20 19:29:40 +08:00
2020-11-21 03:25:28 +08:00
def destroy
comment_destroy_helper(@comment)
2020-11-21 03:25:28 +08:00
end
2020-11-20 19:29:40 +08:00
private
def load_object
@commentable = case params[:object_type]
when 'Project'
Project.find_by(id: params[:object_id])
when 'MyModule'
MyModule.find_by(id: params[:object_id])
when 'Step'
Step.find_by(id: params[:object_id])
when 'Result'
Result.find_by(id: params[:object_id])
end
render_404 and return unless @commentable
end
def load_comment
@comment = Comment.find_by(id: params[:id])
2020-11-21 03:25:28 +08:00
render_404 and return unless @comment
2020-11-20 19:29:40 +08:00
@commentable = @comment.commentable
render_404 and return unless @commentable
end
2020-11-21 03:25:28 +08:00
def comment_params
params.permit(:message)
end
2020-11-20 19:29:40 +08:00
def check_view_permissions
case @commentable
when Project
render_403 and return unless can_read_project?(@commentable)
when MyModule
render_403 and return unless can_read_experiment?(@commentable.experiment)
when Step
render_403 and return unless can_read_protocol_in_module?(@commentable.protocol)
when Result
render_403 and return unless can_read_experiment?(@commentable.my_module.experiment)
else
render_403 and return
end
end
def check_create_permissions
case @commentable
when Project
render_403 and return unless can_create_comments_in_project?(@commentable)
when MyModule
render_403 and return unless can_create_comments_in_module?(@commentable)
when Step
render_403 and return unless can_create_comments_in_module?(@commentable.protocol.my_module)
when Result
render_403 and return unless can_create_comments_in_module?(@commentable.my_module)
else
render_403 and return
end
end
def check_manage_permissions
2020-11-21 03:25:28 +08:00
comment_editable?(@comment)
2020-11-20 19:29:40 +08:00
end
end