Files
fizzy/app/models/card/stallable.rb
T
Ítalo Matos fbc586646f Refactor: improve query scope composition with merge syntax (#2131)
* Refactor: improve query scope composition with merge syntax

Replace manual WHERE clause concatenation with Rails' merge method
for more elegant and maintainable scope composition across Card,
Comment, and Filter models. This approach better follows Rails
conventions and improves code readability.

* Extend scope composition improvements to Card::Closeable

Apply the same nested hash syntax pattern to closures table references
in order and where clauses.

* Remove unnecessary outer braces from where clause

---------

Co-authored-by: Jeremy Daer <jeremy@37signals.com>
2025-12-13 10:10:13 -08:00

43 lines
1.3 KiB
Ruby

module Card::Stallable
extend ActiveSupport::Concern
STALLED_AFTER_LAST_SPIKE_PERIOD = 14.days
included do
has_one :activity_spike, class_name: "Card::ActivitySpike", dependent: :destroy
scope :with_activity_spikes, -> { joins(:activity_spike) }
scope :stalled, -> { open.active.with_activity_spikes.where(card_activity_spikes: { updated_at: ..STALLED_AFTER_LAST_SPIKE_PERIOD.ago }, updated_at: ..STALLED_AFTER_LAST_SPIKE_PERIOD.ago) }
before_update :remember_to_detect_activity_spikes
after_update_commit :detect_activity_spikes_later, if: :should_detect_activity_spikes?
end
def stalled?
if activity_spike.present?
open? && last_activity_spike_at < STALLED_AFTER_LAST_SPIKE_PERIOD.ago && updated_at < STALLED_AFTER_LAST_SPIKE_PERIOD.ago
end
end
def last_activity_spike_at
activity_spike&.updated_at
end
def detect_activity_spikes
Card::ActivitySpike::Detector.new(self).detect
end
private
def remember_to_detect_activity_spikes
@should_detect_activity_spikes = published? && last_active_at_changed?
end
def should_detect_activity_spikes?
@should_detect_activity_spikes
end
def detect_activity_spikes_later
Card::ActivitySpike::DetectionJob.perform_later(self)
end
end