Clean up the controller

This commit is contained in:
David Heinemeier Hansson
2025-04-11 16:49:55 +02:00
parent f2811803d3
commit a8aa2c4f80
2 changed files with 18 additions and 17 deletions
+12 -17
View File
@@ -1,10 +1,11 @@
class Cards::CommentsController < ApplicationController
include CardScoped
before_action :set_comment, only: [ :show, :edit, :update, :destroy ]
before_action :require_own_comment, only: [ :edit, :update, :destroy ]
before_action :set_comment, only: %i[ show edit update destroy ]
before_action :ensure_authorship, only: %i[ edit update destroy ]
def create
@card.capture new_comment
@card.capture Comment.new(comment_params)
end
def show
@@ -23,21 +24,15 @@ class Cards::CommentsController < ApplicationController
end
private
def set_comment
@comment = @card.comments.find(params[:id])
end
def ensure_authorship
head :forbidden if Current.user != @comment.creator
end
def comment_params
params.require(:comment).permit(:body)
end
def new_comment
Comment.new(comment_params)
end
def set_comment
@comment = Comment.joins(:message)
.where(messages: { card_id: @card.id })
.find(params[:id])
end
def require_own_comment
head :forbidden unless Current.user == @comment.creator
end
end
+6
View File
@@ -3,9 +3,15 @@ module Card::Messages
included do
has_many :messages, -> { chronologically }, dependent: :destroy
has_many :comments, through: :messages, source: :messageable
after_save :capture_draft_comment
end
def comments
# FIXME: I could have sworn there was a way to declare this as a association?
Comment.joins(:message).where(messages: { card_id: id })
end
def capture(messageable)
messages.create! messageable: messageable
end