Comment-specific behavior needs to live in the comment

This really is a bit of a tangled mess. Everyone knows everything about
everyone else.
This commit is contained in:
David Heinemeier Hansson
2025-04-12 10:39:56 +02:00
parent bb64adfd1b
commit 81f431d946
6 changed files with 35 additions and 38 deletions
+2 -1
View File
@@ -1,5 +1,5 @@
class Card < ApplicationRecord
include Assignable, Boostable, Colored, Commentable, Engageable, Eventable, Golden,
include Assignable, Boostable, Colored, Engageable, Eventable, Golden,
Messages, Notifiable, Pinnable, Closeable, Scorable, Searchable, Staged,
Statuses, Taggable, Watchable
@@ -18,6 +18,7 @@ class Card < ApplicationRecord
scope :chronologically, -> { order created_at: :asc, id: :asc }
scope :latest, -> { order updated_at: :desc, id: :desc }
scope :in_collection, ->(collection) { where collection: collection }
scope :ordered_by_comments, -> { order comments_count: :desc }
scope :indexed_by, ->(index) do
case index
-20
View File
@@ -1,20 +0,0 @@
module Card::Commentable
extend ActiveSupport::Concern
included do
scope :ordered_by_comments, -> { order comments_count: :desc }
end
def comment_created(comment)
increment! :comments_count
watch_by comment.creator
track_event :commented, comment_id: comment.id
rescore
end
def comment_destroyed
decrement! :comments_count
rescore
end
end
+6 -6
View File
@@ -10,14 +10,14 @@ module Card::Eventable
touch :last_active_at
end
private
def track_event(action, creator: Current.user, **particulars)
if published?
event = find_or_capture_event_summary.events.create! action: action, creator: creator, card: self, particulars: particulars
event.generate_notifications_later
end
def track_event(action, creator: Current.user, **particulars)
if published?
event = find_or_capture_event_summary.events.create! action: action, creator: creator, card: self, particulars: particulars
event.generate_notifications_later
end
end
private
def find_or_capture_event_summary
transaction do
messages.last&.event_summary || capture(EventSummary.new).event_summary
+17
View File
@@ -18,6 +18,23 @@ class Comment < ApplicationRecord
card_comments.many? && card_comments_prior.last&.creator != creator
end
def created_via(message)
message.card.tap do |card|
card.increment! :comments_count
card.watch_by creator
card.track_event :commented, comment_id: id
card.rescore
end
end
def destroyed_via(message)
message.card.tap do |card|
card.decrement! :comments_count
card.rescore
end
end
def to_partial_path
"cards/#{super}"
end
+8
View File
@@ -7,4 +7,12 @@ module Messageable
has_one :message, as: :messageable, touch: true, dependent: :destroy
has_one :card, through: :message
end
def created_via(message)
# Overwrite in Messageable class
end
def destroyed_via(message)
# Overwrite in Messageable class
end
end
+2 -11
View File
@@ -5,15 +5,6 @@ class Message < ApplicationRecord
scope :chronologically, -> { order created_at: :asc, id: :desc }
after_create :created
after_destroy :destroyed
private
def created
card.comment_created(comment) if comment?
end
def destroyed
card.comment_destroyed if comment?
end
after_create -> { messageable.created_via(self) }
after_destroy -> { messageable.destroyed_via(self) }
end