diff --git a/app/helpers/events_helper.rb b/app/helpers/events_helper.rb index 6a8ebe03f..772665f79 100644 --- a/app/helpers/events_helper.rb +++ b/app/helpers/events_helper.rb @@ -53,8 +53,8 @@ module EventsHelper end when "card_unassigned" "#{ event.creator == Current.user ? "You" : event.creator.name } unassigned #{ event.assignees.include?(Current.user) ? "yourself" : event.assignees.pluck(:name).to_sentence } from #{ event.eventable.title }".html_safe - when "card_commented" - "#{ event.creator == Current.user ? "You" : event.creator.name } commented on #{ event.eventable.title }".html_safe + when "comment_created" + "#{ event.creator == Current.user ? "You" : event.creator.name } commented on #{ event.eventable.card.title }".html_safe when "card_published" "#{ event.creator == Current.user ? "You" : event.creator.name } added #{ event.eventable.title }".html_safe when "card_closed" @@ -82,7 +82,7 @@ module EventsHelper "bolt" when "card_unstaged" "bolt" - when "card_commented" + when "comment_created" "comment" when "card_title_changed" "rename" diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index 344e5c0f4..3e7044376 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -1,7 +1,7 @@ module NotificationsHelper def event_notification_title(event) case event_notification_action(event) - when "card_commented" then "RE: " + event.eventable.title + when "comment_created" then "RE: " + event.eventable.card.title when "card_assigned" then "Assigned to you: " + event.eventable.title else event.eventable.title end @@ -13,7 +13,7 @@ module NotificationsHelper case event_notification_action(event) when "card_closed" then "Closed by #{name}" when "card_published" then "Added by #{name}" - when "card_commented" then comment_notification_body(event) + when "comment_created" then comment_notification_body(event) else name end end @@ -56,6 +56,7 @@ module NotificationsHelper end def comment_notification_body(event) - "#{strip_tags(event.comment.body_html).blank? ? "#{event.creator.name} replied" : "#{event.creator.name}:" } #{strip_tags(event.comment.body_html).truncate(200)}" + comment = event.eventable + "#{strip_tags(comment.body_html).blank? ? "#{event.creator.name} replied" : "#{event.creator.name}:" } #{strip_tags(comment.body_html).truncate(200)}" end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 9b3fd2704..bdc674a9c 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,5 +1,5 @@ class Comment < ApplicationRecord - include Mentions, Messageable, Searchable + include Eventable, Mentions, Messageable, Searchable belongs_to :creator, class_name: "User", default: -> { Current.user } has_many :reactions, dependent: :delete_all @@ -10,7 +10,7 @@ class Comment < ApplicationRecord # FIXME: Not a fan of this. Think all references to comment should come directly from the message. scope :belonging_to_card, ->(card) { joins(:message).where(messages: { card_id: card.id }) } - after_create_commit :watch_card_by_creator, :track_commented_card + after_create_commit :watch_card_by_creator after_destroy_commit :cleanup_events delegate :watch_by, to: :card @@ -24,10 +24,6 @@ class Comment < ApplicationRecord card.watch_by creator end - def track_commented_card - card.track_event :commented, comment_id: id - end - # FIXME: This isn't right. We need to introduce an eventable polymorphic association for this. def cleanup_events # Delete events that reference directly in particulars diff --git a/app/models/comment/eventable.rb b/app/models/comment/eventable.rb new file mode 100644 index 000000000..4e90777e0 --- /dev/null +++ b/app/models/comment/eventable.rb @@ -0,0 +1,18 @@ +module Comment::Eventable + extend ActiveSupport::Concern + + include ::Eventable + + included do + after_create_commit :track_creation + end + + def event_was_created(event) + card.touch(:last_active_at) + end + + private + def track_creation + track_event("created", collection: card.collection) + end +end diff --git a/app/models/event.rb b/app/models/event.rb index 1cf31aa13..17f9006c4 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -15,11 +15,7 @@ class Event < ApplicationRecord end def notifiable_target - if action.commented? - comment - else - eventable - end + eventable end # TODO: This doesn't belong here anymore diff --git a/app/models/event/particulars.rb b/app/models/event/particulars.rb index 389eda8ea..dd92fa4da 100644 --- a/app/models/event/particulars.rb +++ b/app/models/event/particulars.rb @@ -2,14 +2,10 @@ module Event::Particulars extend ActiveSupport::Concern included do - store_accessor :particulars, :assignee_ids, :comment_id, :stage_id, :stage_name + store_accessor :particulars, :assignee_ids, :stage_id, :stage_name end def assignees @assignees ||= User.where id: assignee_ids end - - def comment - @comment ||= Comment.find_by id: comment_id - end end diff --git a/app/models/notifier/event_notifier.rb b/app/models/notifier/event_notifier.rb index eb7e77cb5..9a42af611 100644 --- a/app/models/notifier/event_notifier.rb +++ b/app/models/notifier/event_notifier.rb @@ -9,14 +9,18 @@ class Notifier::EventNotifier < Notifier source.assignees.excluding(source.collection.access_only_users) when "card_published" watchers_and_subscribers(include_only_watching: true).without(creator, *card.mentionees) - when "card_commented" - watchers_and_subscribers.without(creator, *source.comment.mentionees) + when "comment_created" + watchers_and_subscribers.without(creator, *source.eventable.mentionees) else watchers_and_subscribers.without(creator) end end def card - source.eventable + if source.eventable.is_a?(Card) + source.eventable + else + source.eventable.card + end end end diff --git a/app/views/events/_event.html.erb b/app/views/events/_event.html.erb index 867324c66..7e176b9c9 100644 --- a/app/views/events/_event.html.erb +++ b/app/views/events/_event.html.erb @@ -1,9 +1,11 @@ +<% card = event.eventable.is_a?(Card) ? event.eventable : event.eventable.card %> + <%= link_to event.eventable, class: "event event--#{ event.action } panel center center-block flex-inline align-start justify-start gap position-relative", - style: "--card-color: #{ event.eventable.color }; background-color: color-mix(in srgb, var(--card-color) 10%, var(--color-bg)); + style: "--card-color: #{ card.color }; background-color: color-mix(in srgb, var(--card-color) 10%, var(--color-bg)); color: color-mix(in srgb, var(--card-color) 40%, var(--color-ink));", data: { related_element_target: "related", - related_element_group_value: event.eventable.id, + related_element_group_value: card.id, action: "mouseover->related-element#highlight mouseout->related-element#unhighlight" } do %>
<%= avatar_image_tag(event.creator) %> @@ -22,9 +24,9 @@ <% end %>
- <% if event.action == "card_commented" && !strip_tags(event&.comment&.body_html).blank? %> + <% if event.action == "comment_created" && !strip_tags(event&.eventable&.body_html).blank? %> - <%= strip_tags(event.comment.body_html).truncate(200) -%> + <%= strip_tags(event&.eventable.body_html).truncate(200) -%> <% end %> diff --git a/test/fixtures/events.yml b/test/fixtures/events.yml index bf65e5486..4bc273ca3 100644 --- a/test/fixtures/events.yml +++ b/test/fixtures/events.yml @@ -35,10 +35,9 @@ layout_published: layout_commented: creator: david collection: writebook - eventable: layout (Card) - action: card_commented + eventable: layout_overflowing_david (Comment) + action: comment_created summary: layout_initial_activity - particulars: <%= { comment_id: ActiveRecord::FixtureSet.identify(:layout_overflowing_david) }.to_json %> created_at: <%= 1.week.ago %> layout_assignment_jz: