Split draft commenting back out to a dedicated concern

This commit is contained in:
David Heinemeier Hansson
2025-04-12 11:05:56 +02:00
parent 27d90abbd6
commit 9f7bbbef58
3 changed files with 34 additions and 27 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
class Card < ApplicationRecord
include Assignable, Boostable, Colored, Engageable, Eventable, Golden,
include Assignable, Boostable, Colored, DraftCommentable, Engageable, Eventable, Golden,
Messages, Notifiable, Pinnable, Closeable, Scorable, Searchable, Staged,
Statuses, Taggable, Watchable
+33
View File
@@ -0,0 +1,33 @@
module Card::DraftCommentable
extend ActiveSupport::Concern
included do
after_save :capture_draft_comment
end
def draft_comment
find_or_build_initial_comment.body.content
end
def draft_comment=(body)
if body.present?
@draft_comment = body
else
messages.comments.destroy_all
end
end
private
def find_or_build_initial_comment
message = messages.comments.first || messages.new(messageable: Comment.new)
message.comment
end
def capture_draft_comment
if @draft_comment.present?
find_or_build_initial_comment.update! body: @draft_comment, creator: creator
end
@draft_comment = nil
end
end
-26
View File
@@ -3,35 +3,9 @@ module Card::Messages
included do
has_many :messages, -> { chronologically }, dependent: :destroy
after_save :capture_draft_comment
end
def capture(messageable)
messages.create! messageable: messageable
end
def draft_comment
find_or_build_initial_comment.body.content
end
def draft_comment=(body)
if body.present?
@draft_comment = body
else
messages.comments.destroy_all
end
end
private
def find_or_build_initial_comment
message = messages.comments.first || messages.new(messageable: Comment.new)
message.comment
end
def capture_draft_comment
if @draft_comment.present?
find_or_build_initial_comment.update! body: @draft_comment, creator: creator
end
@draft_comment = nil
end
end