Files
fizzy/app/models/card/engageable.rb
T
Mike Dalessio 4e815fc423 Ensure that a "reconsidered" card gets an updated timestamp
so that it's not closed by the subsequent Card::AutoCloseAllDueJob

ref: https://37s.fizzy.37signals.com/collections/693169850/cards/999009030
2025-05-07 19:04:13 -04:00

56 lines
1.2 KiB
Ruby

module Card::Engageable
extend ActiveSupport::Concern
STAGNATED_AFTER = 30.days
included do
has_one :engagement, dependent: :destroy, class_name: "Card::Engagement"
scope :considering, -> { published_or_drafted_by(Current.user).open.where.missing(:engagement) }
scope :doing, -> { published.open.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
def auto_reconsider_all_stagnated
stagnated.find_each(&:reconsider)
end
end
def auto_reconsider_at
last_active_at + STAGNATED_AFTER if last_active_at
end
def doing?
open? && published? && engagement.present?
end
def considering?
open? && published? && engagement.blank?
end
def engage
unless doing?
transaction do
reopen
create_engagement!
end
end
end
def reconsider
transaction do
reopen
engagement&.destroy
touch(:last_active_at)
end
end
end