Move card concern logic into those concerns
This commit is contained in:
@@ -10,10 +10,6 @@ class Card < ApplicationRecord
|
||||
|
||||
has_one_attached :image, dependent: :purge_later
|
||||
|
||||
after_save :track_due_date_change, if: :saved_change_to_due_on?
|
||||
after_save :track_title_change, if: :saved_change_to_title?
|
||||
after_create :assign_initial_stage
|
||||
|
||||
scope :reverse_chronologically, -> { order created_at: :desc, id: :desc }
|
||||
scope :chronologically, -> { order created_at: :asc, id: :asc }
|
||||
scope :latest, -> { order updated_at: :desc, id: :desc }
|
||||
@@ -28,13 +24,6 @@ class Card < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
scope :by_engagement_status, ->(status) do
|
||||
case status.to_s
|
||||
when "considering" then considering
|
||||
when "doing" then doing.with_golden_first
|
||||
end
|
||||
end
|
||||
|
||||
def title=(new_title)
|
||||
self[:title] = new_title.presence || "Untitled"
|
||||
end
|
||||
@@ -42,34 +31,4 @@ class Card < ApplicationRecord
|
||||
def cache_key
|
||||
[ super, collection&.name ].compact.join("/")
|
||||
end
|
||||
|
||||
private
|
||||
def track_due_date_change
|
||||
if due_on.present?
|
||||
if due_on_before_last_save.nil?
|
||||
track_event("due_date_added", particulars: { due_date: due_on })
|
||||
else
|
||||
track_event("due_date_changed", particulars: { due_date: due_on })
|
||||
end
|
||||
elsif due_on_before_last_save.present?
|
||||
track_event("due_date_removed")
|
||||
end
|
||||
end
|
||||
|
||||
def track_title_change
|
||||
if title_before_last_save.present?
|
||||
track_event("title_changed", particulars: {
|
||||
old_title: title_before_last_save,
|
||||
new_title: title
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
def assign_initial_stage
|
||||
if workflow_stage = collection.workflow&.stages&.first
|
||||
self.stage = workflow_stage
|
||||
save! touch: false
|
||||
track_event :staged, stage_id: workflow_stage.id, stage_name: workflow_stage.name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,6 +9,13 @@ module Card::Engageable
|
||||
scope :considering, -> { published_or_drafted_by(Current.user).active.where.missing(:engagement) }
|
||||
scope :doing, -> { published.active.joins(:engagement) }
|
||||
scope :stagnated, -> { doing.where(last_active_at: ..STAGNATED_AFTER.ago) }
|
||||
|
||||
scope :by_engagement_status, ->(status) do
|
||||
case status.to_s
|
||||
when "considering" then considering
|
||||
when "doing" then doing.with_golden_first
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class_methods do
|
||||
|
||||
@@ -3,7 +3,11 @@ module Card::Eventable
|
||||
|
||||
included do
|
||||
has_many :events, dependent: :destroy
|
||||
|
||||
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, **particulars)
|
||||
@@ -14,6 +18,24 @@ module Card::Eventable
|
||||
end
|
||||
|
||||
private
|
||||
def track_due_date_change
|
||||
if due_on.present?
|
||||
if due_on_before_last_save.nil?
|
||||
track_event("due_date_added", particulars: { due_date: due_on })
|
||||
else
|
||||
track_event("due_date_changed", particulars: { due_date: due_on })
|
||||
end
|
||||
elsif due_on_before_last_save.present?
|
||||
track_event("due_date_removed")
|
||||
end
|
||||
end
|
||||
|
||||
def track_title_change
|
||||
if title_before_last_save.present?
|
||||
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
|
||||
|
||||
@@ -4,6 +4,8 @@ module Card::Staged
|
||||
included do
|
||||
belongs_to :stage, class_name: "Workflow::Stage", optional: true
|
||||
|
||||
after_create :assign_initial_stage
|
||||
|
||||
scope :in_stage, ->(stage) { where stage: stage }
|
||||
end
|
||||
|
||||
@@ -19,4 +21,13 @@ module Card::Staged
|
||||
track_event event, stage_id: stage.id, stage_name: stage.name
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def assign_initial_stage
|
||||
if workflow_stage = collection.workflow&.stages&.first
|
||||
self.stage = workflow_stage
|
||||
save! touch: false
|
||||
track_event :staged, stage_id: workflow_stage.id, stage_name: workflow_stage.name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user