Make eventable a generic concern and create a card's especialization for it
This commit is contained in:
@@ -1,22 +1,33 @@
|
||||
module Card::Eventable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_many :events, as: :eventable, dependent: :destroy
|
||||
include ::Eventable
|
||||
|
||||
included do
|
||||
before_create { self.last_active_at = Time.current }
|
||||
|
||||
after_save :track_due_date_change, if: :saved_change_to_due_on?
|
||||
after_save :track_title_change, if: :saved_change_to_title?
|
||||
end
|
||||
|
||||
def track_event(action, creator: Current.user, collection: self.collection, **particulars)
|
||||
if published?
|
||||
find_or_capture_event_summary.events.create! action:, creator:, collection:, eventable: self, particulars:
|
||||
def event_was_created(event)
|
||||
transaction do
|
||||
find_or_capture_event_summary.events << event
|
||||
touch(:last_active_at)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def should_track_event?
|
||||
published?
|
||||
end
|
||||
|
||||
def find_or_capture_event_summary
|
||||
transaction do
|
||||
messages.last&.event_summary || capture(EventSummary.new).event_summary
|
||||
end
|
||||
end
|
||||
|
||||
def track_due_date_change
|
||||
if due_on.present?
|
||||
if due_on_before_last_save.nil?
|
||||
@@ -34,10 +45,4 @@ module Card::Eventable
|
||||
track_event "title_changed", particulars: { old_title: title_before_last_save, new_title: title }
|
||||
end
|
||||
end
|
||||
|
||||
def find_or_capture_event_summary
|
||||
transaction do
|
||||
messages.last&.event_summary || capture(EventSummary.new).event_summary
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+2
-3
@@ -4,12 +4,11 @@ class Event < ApplicationRecord
|
||||
belongs_to :collection
|
||||
belongs_to :creator, class_name: "User"
|
||||
belongs_to :eventable, polymorphic: true
|
||||
belongs_to :summary, touch: true, class_name: "EventSummary"
|
||||
belongs_to :summary, touch: true, class_name: "EventSummary", optional: true
|
||||
|
||||
scope :chronologically, -> { order created_at: :asc, id: :desc }
|
||||
|
||||
# TODO: Remove dependency with last_active_at via hook
|
||||
after_create -> { eventable.touch(:last_active_at) }
|
||||
after_create -> { eventable.event_was_created(self) }
|
||||
|
||||
def action
|
||||
super.inquiry
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
module Eventable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_many :events, as: :eventable, dependent: :destroy
|
||||
end
|
||||
|
||||
def track_event(action, creator: Current.user, collection: self.collection, **particulars)
|
||||
if should_track_event?
|
||||
collection.events.create!(action:, creator:, collection:, eventable: self, particulars:)
|
||||
end
|
||||
end
|
||||
|
||||
def event_was_created(event)
|
||||
end
|
||||
|
||||
private
|
||||
def should_track_event?
|
||||
true
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
class MakeEventSummaryOptional < ActiveRecord::Migration[8.1]
|
||||
def change
|
||||
change_column_null :events, :summary_id, true
|
||||
end
|
||||
end
|
||||
Generated
+2
-2
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.1].define(version: 2025_04_24_051059) do
|
||||
ActiveRecord::Schema[8.1].define(version: 2025_04_24_105223) do
|
||||
create_table "accesses", force: :cascade do |t|
|
||||
t.integer "collection_id", null: false
|
||||
t.datetime "created_at", null: false
|
||||
@@ -183,7 +183,7 @@ ActiveRecord::Schema[8.1].define(version: 2025_04_24_051059) do
|
||||
t.integer "eventable_id", null: false
|
||||
t.string "eventable_type", null: false
|
||||
t.json "particulars", default: {}
|
||||
t.integer "summary_id", null: false
|
||||
t.integer "summary_id"
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["collection_id"], name: "index_events_on_collection_id"
|
||||
t.index ["creator_id"], name: "index_events_on_creator_id"
|
||||
|
||||
+2
-2
@@ -592,7 +592,7 @@ columns:
|
||||
name: summary_id
|
||||
cast_type: *1
|
||||
sql_type_metadata: *2
|
||||
'null': false
|
||||
'null': true
|
||||
default:
|
||||
default_function:
|
||||
collation:
|
||||
@@ -2026,4 +2026,4 @@ indexes:
|
||||
comment:
|
||||
valid: true
|
||||
workflows: []
|
||||
version: 20250424051059
|
||||
version: 20250424105223
|
||||
|
||||
Reference in New Issue
Block a user