Files
manyfold/app/controllers/comments_controller.rb
2026-07-26 15:14:23 +01:00

65 lines
1.7 KiB
Ruby

class CommentsController < ApplicationController
before_action :get_commentable
before_action :get_comment, except: [:create]
def show
respond_to do |format|
format.activitypub
format.html { redirect_to @commentable, status: :see_other }
end
end
def create
@comment = @commentable.comments.new(comment_params.merge(commenter: current_user))
authorize @comment
respond_to do |format|
format.html do
if @commentable.save
redirect_to @commentable, status: :see_other
else
render :nothing, status: :unprocessable_content
end
end
end
end
def destroy
authorize @comment
respond_to do |format|
format.html do
if @comment.destroy!
redirect_to @commentable, status: :see_other, notice: t(".destroyed")
else
render :nothing, status: :unprocessable_content
end
end
end
end
private
def comment_params
params.expect(comment: [:comment])
end
def get_comment
@comment = policy_scope(@commentable.comments).find_param(params[:id])
end
def get_commentable
# Allowlist for commentable class param.
# This isn't actually supplied by the user, it comes from the router, but best to be double safe.
commentable, commentable_param = {
"Model" => [Model, "model_id"],
"Creator" => [Creator, "creator_id"],
"Collection" => [Collection, "collection_id"]
}[params[:commentable_class]]
raise ActionController::BadRequest unless commentable
# Get the actual item
id = params[commentable_param]
@commentable = policy_scope(commentable).find_param(id)
authorize @commentable, :show?
end
end