diff --git a/app/controllers/concerns/events_timeline.rb b/app/controllers/concerns/events_timeline.rb index 0d50d2093..c051c370d 100644 --- a/app/controllers/concerns/events_timeline.rb +++ b/app/controllers/concerns/events_timeline.rb @@ -29,12 +29,11 @@ module EventsTimeline end def user_events - Event.where(card: user_cards) + Event.where(collection: collection_filter).where(eventable: user_cards) end def user_cards Current.user.accessible_cards .published_or_drafted_by(Current.user) - .where(collection_id: collection_filter) end end diff --git a/app/controllers/terminals_controller.rb b/app/controllers/terminals_controller.rb index 728ff9983..e2840773a 100644 --- a/app/controllers/terminals_controller.rb +++ b/app/controllers/terminals_controller.rb @@ -1,6 +1,6 @@ class TerminalsController < ApplicationController def show - @events = Event.where(card: Current.user.accessible_cards, creator: Current.user).chronologically.reverse_order.limit(20) + @events = Event.where(eventable: Current.user.accessible_cards, creator: Current.user).chronologically.reverse_order.limit(20) end def edit diff --git a/app/helpers/events_helper.rb b/app/helpers/events_helper.rb index 53732b327..1bcacaff5 100644 --- a/app/helpers/events_helper.rb +++ b/app/helpers/events_helper.rb @@ -45,10 +45,13 @@ module EventsHelper start_time = day.beginning_of_day end_time = day.end_of_day - accessible_events = Event.joins(card: :collection) - .merge(Current.user.collections) + collections = Current.user.collections + collections = collections.where(id: params[:collection_ids]) if params[:collection_ids].present? + + # TODO: this needs tidying up + accessible_events = Event.where(eventable_type: "Card") .where(created_at: start_time..end_time) - .where(cards: { collection_id: params[:collection_ids].presence || Current.user.collection_ids }) + .where(collection: collections) headers = { "Added" => accessible_events.where(action: "published").count, @@ -66,30 +69,30 @@ module EventsHelper case event.action when "assigned" if event.assignees.include?(Current.user) - "#{ event.creator == Current.user ? "You" : event.creator.name } will handle #{ event.card.title }".html_safe + "#{ event.creator == Current.user ? "You" : event.creator.name } will handle #{ event.eventable.title }".html_safe else - "#{ event.creator == Current.user ? "You" : event.creator.name } assigned #{ event.assignees.pluck(:name).to_sentence } to #{ event.card.title }".html_safe + "#{ event.creator == Current.user ? "You" : event.creator.name } assigned #{ event.assignees.pluck(:name).to_sentence } to #{ event.eventable.title }".html_safe end when "unassigned" - "#{ event.creator == Current.user ? "You" : event.creator.name } unassigned #{ event.assignees.include?(Current.user) ? "yourself" : event.assignees.pluck(:name).to_sentence } from #{ event.card.title }".html_safe + "#{ 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 "commented" - "#{ event.creator == Current.user ? "You" : event.creator.name } commented on #{ event.card.title }".html_safe + "#{ event.creator == Current.user ? "You" : event.creator.name } commented on #{ event.eventable.title }".html_safe when "published" - "#{ event.creator == Current.user ? "You" : event.creator.name } added #{ event.card.title }".html_safe + "#{ event.creator == Current.user ? "You" : event.creator.name } added #{ event.eventable.title }".html_safe when "closed" - "#{ event.creator == Current.user ? "You" : event.creator.name } closed #{ event.card.title }".html_safe + "#{ event.creator == Current.user ? "You" : event.creator.name } closed #{ event.eventable.title }".html_safe when "staged" - "#{event.creator == Current.user ? "You" : event.creator.name} moved #{ event.card.title } to the #{event.stage_name} stage".html_safe + "#{event.creator == Current.user ? "You" : event.creator.name} moved #{ event.eventable.title } to the #{event.stage_name} stage".html_safe when "unstaged" - "#{event.creator == Current.user ? "You" : event.creator.name} moved #{ event.card.title } out ofthe #{event.stage_name} stage".html_safe + "#{event.creator == Current.user ? "You" : event.creator.name} moved #{ event.eventable.title } out ofthe #{event.stage_name} stage".html_safe when "due_date_added" - "#{event.creator == Current.user ? "You" : event.creator.name} set the date to #{event.particulars.dig('particulars', 'due_date').to_date.strftime('%B %-d')} on #{ event.card.title }".html_safe + "#{event.creator == Current.user ? "You" : event.creator.name} set the date to #{event.particulars.dig('particulars', 'due_date').to_date.strftime('%B %-d')} on #{ event.eventable.title }".html_safe when "due_date_changed" - "#{event.creator == Current.user ? "You" : event.creator.name} changed the date to #{event.particulars.dig('particulars', 'due_date').to_date.strftime('%B %-d')} on #{ event.card.title }".html_safe + "#{event.creator == Current.user ? "You" : event.creator.name} changed the date to #{event.particulars.dig('particulars', 'due_date').to_date.strftime('%B %-d')} on #{ event.eventable.title }".html_safe when "due_date_removed" - "#{event.creator == Current.user ? "You" : event.creator.name} removed the date on #{ event.card.title }" + "#{event.creator == Current.user ? "You" : event.creator.name} removed the date on #{ event.eventable.title }" when "title_changed" - "#{event.creator == Current.user ? "You" : event.creator.name} renamed #{ event.card.title } (was: '#{event.particulars.dig('particulars', 'old_title')})'".html_safe + "#{event.creator == Current.user ? "You" : event.creator.name} renamed #{ event.eventable.title } (was: '#{event.particulars.dig('particulars', 'old_title')})'".html_safe end end diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index 5f61f99e9..cbb1774a0 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -1,9 +1,9 @@ module NotificationsHelper def event_notification_title(event) case event_notification_action(event) - when "commented" then "RE: " + event.card.title - when "assigned" then "Assigned to you: " + event.card.title - else event.card.title + when "commented" then "RE: " + event.eventable.title + when "assigned" then "Assigned to you: " + event.eventable.title + else event.eventable.title end end diff --git a/app/models/card/eventable.rb b/app/models/card/eventable.rb index 55a0245ea..eb06ead4a 100644 --- a/app/models/card/eventable.rb +++ b/app/models/card/eventable.rb @@ -2,7 +2,7 @@ module Card::Eventable extend ActiveSupport::Concern included do - has_many :events, dependent: :destroy + has_many :events, as: :eventable, dependent: :destroy before_create { self.last_active_at = Time.current } @@ -10,9 +10,9 @@ module Card::Eventable after_save :track_title_change, if: :saved_change_to_title? end - def track_event(action, creator: Current.user, **particulars) + def track_event(action, creator: Current.user, collection: self.collection, **particulars) if published? - find_or_capture_event_summary.events.create! action: action, creator: creator, card: self, particulars: particulars + find_or_capture_event_summary.events.create! action:, creator:, collection:, eventable: self, particulars: end end diff --git a/app/models/event.rb b/app/models/event.rb index 1fc2fc5b6..9659ace63 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -1,12 +1,15 @@ class Event < ApplicationRecord include Notifiable, Particulars + belongs_to :collection belongs_to :creator, class_name: "User" - belongs_to :card + belongs_to :eventable, polymorphic: true + belongs_to :summary, touch: true, class_name: "EventSummary" scope :chronologically, -> { order created_at: :asc, id: :desc } - after_create -> { card.touch(:last_active_at) } + # TODO: Remove dependency with last_active_at via hook + after_create -> { eventable.touch(:last_active_at) } def action super.inquiry @@ -16,11 +19,12 @@ class Event < ApplicationRecord if action.commented? comment else - card + eventable end end + # TODO: This doesn't belong here anymore def initial_assignment? - action == "published" && card.assigned_to?(creator) + action == "published" && eventable.assigned_to?(creator) end end diff --git a/app/models/event_summary.rb b/app/models/event_summary.rb index 99af2d0e1..241d3cc3c 100644 --- a/app/models/event_summary.rb +++ b/app/models/event_summary.rb @@ -1,7 +1,7 @@ class EventSummary < ApplicationRecord include Messageable - has_many :events, -> { chronologically }, dependent: :delete_all, foreign_key: :summary_id + has_many :events, -> { chronologically }, dependent: :delete_all, inverse_of: :summary # FIXME: Consider persisting the body and compute at write time. def body diff --git a/app/models/notifier/event_notifier.rb b/app/models/notifier/event_notifier.rb index ea771bc15..e7b11b4d8 100644 --- a/app/models/notifier/event_notifier.rb +++ b/app/models/notifier/event_notifier.rb @@ -1,12 +1,12 @@ class Notifier::EventNotifier < Notifier - delegate :card, :creator, to: :source + delegate :creator, to: :source delegate :watchers_and_subscribers, to: :card private def recipients case source.action when "assigned" - source.assignees.excluding(card.collection.access_only_users) + source.assignees.excluding(source.collection.access_only_users) when "published" watchers_and_subscribers(include_only_watching: true).without(creator, *card.mentionees) when "commented" @@ -15,4 +15,8 @@ class Notifier::EventNotifier < Notifier watchers_and_subscribers.without(creator) end end + + def card + source.eventable + end end diff --git a/app/views/events/_event.html.erb b/app/views/events/_event.html.erb index 2d37c5665..4bde70fbb 100644 --- a/app/views/events/_event.html.erb +++ b/app/views/events/_event.html.erb @@ -1,4 +1,4 @@ -<% card = event.card %> +<% card = event.eventable %> <% link = event.action == "commented" ? collection_card_path(card.collection, card, anchor: dom_id(event.comment)) : collection_card_path(card.collection, card) %> <%= link_to link, diff --git a/app/views/notifications/notification/_event.html.erb b/app/views/notifications/notification/_event.html.erb index 9a02db061..2dd8d16ee 100644 --- a/app/views/notifications/notification/_event.html.erb +++ b/app/views/notifications/notification/_event.html.erb @@ -4,4 +4,4 @@