388fe2de73
The "closing soon" bubbles were introduced in #406, and the "falling back" bubbles in #500. However, these bubbles are part of the cached card and so as time passes, the relative time doesn't change unless the card is touched. This PR introduces a stimulus controller for bubbles, which takes care of: - making the bubble visible during the reminder period - calculating the relative number of days until entropy kicks in - rendering appropriate text around the day count ref: https://37s.fizzy.37signals.com/collections/693169850/cards/999009091
57 lines
1.2 KiB
Ruby
57 lines
1.2 KiB
Ruby
module Card::Engageable
|
|
extend ActiveSupport::Concern
|
|
|
|
STAGNATED_AFTER = 30.days
|
|
AUTO_RECONSIDER_REMINDER_BEFORE = 7.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
|