scinote-web/app/controllers/comments_controller.rb

118 lines
3.4 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)
2022-05-25 21:04:14 +08:00
object_url = nil
case @commentable
when Step
object_name = "#{@commentable.position + 1} #{@commentable.name}"
object_url = link_to(t('comments.step_url'), "#stepContainer#{@commentable.id}", class: 'scroll-page-with-anchor')
else
object_name = @commentable.name
end
2020-11-20 19:29:40 +08:00
render json: {
2022-05-25 21:04:14 +08:00
object_name: object_name,
object_url: object_url,
comment_addable: comment_addable?(@commentable),
comments: render_to_string(partial: 'shared/comments/comments_list',
locals: { comments: comments },
formats: :html)
2020-11-20 19:29:40 +08:00
}
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_project_comments?(@commentable)
2020-11-20 19:29:40 +08:00
when MyModule
render_403 and return unless can_create_my_module_comments?(@commentable)
2020-11-20 19:29:40 +08:00
when Step
render_403 and return unless can_create_comments_in_my_module_steps?(@commentable.protocol.my_module)
2020-11-20 19:29:40 +08:00
when Result
render_403 and return unless can_create_my_module_result_comments?(@commentable.my_module)
2020-11-20 19:29:40 +08:00
else
render_403 and return
end
end
def check_manage_permissions
render_403 unless comment_editable?(@comment)
2020-11-20 19:29:40 +08:00
end
end