Files
fizzy/app/models/card/entropy.rb
T
Mike Dalessio 28ed744d0c refactor: Extract a Card::Entropy concern
to encompass behavior related to auto-closing and auto-reconsidering.

Note that putting this code in a single place reveals an asymmetry
between the actions:

- Auto-closing period is an optional attribute of a collection
- Auto-reconsidering period is hard-coded to 30 days for all collections
2025-06-01 16:50:12 -04:00

48 lines
1.3 KiB
Ruby

module Card::Entropy
extend ActiveSupport::Concern
AUTO_RECONSIDER_PERIOD = 30.days
ENTROPY_REMINDER_BEFORE = 7.days
included do
scope :in_auto_closing_collection, -> { joins(:collection).merge(Collection.auto_closing) }
scope :stagnated, -> { doing.where(last_active_at: ..AUTO_RECONSIDER_PERIOD.ago) }
scope :due_to_be_closed, -> { considering.in_auto_closing_collection.where("last_active_at <= DATETIME('now', '-' || auto_close_period || ' seconds')") }
delegate :auto_closing?, :auto_close_period, to: :collection
end
class_methods do
def auto_close_all_due
due_to_be_closed.find_each do |card|
card.close(user: User.system, reason: "Closed")
end
end
def auto_reconsider_all_stagnated
stagnated.find_each(&:reconsider)
end
end
def subject_to_entropy?
doing? || (auto_closing? && considering?)
end
def auto_close_at
last_active_at + auto_close_period if auto_closing? && last_active_at
end
def days_until_close
(auto_close_at.to_date - Date.current).to_i if auto_close_at
end
def auto_reconsider_at
last_active_at + AUTO_RECONSIDER_PERIOD if last_active_at
end
def days_until_reconsider
(auto_reconsider_at.to_date - Date.current).to_i if auto_reconsider_at
end
end