Files
fizzy/app/models/card/statuses.rb
T
Mike Dalessio 31007c7bdb Create a "published" event if a card is created published
This isn't the normal workflow in the app, but this problem will show
up if we create cards programmatically (e.g., the seeds)
2025-07-03 01:08:53 -04:00

40 lines
968 B
Ruby

module Card::Statuses
extend ActiveSupport::Concern
included do
enum :status, %w[ creating drafted published ].index_by(&:itself)
after_create -> { track_event :published }, if: :published?
scope :published_or_drafted_by, ->(user) { where(status: :published).or(where(status: :drafted, creator: user)) }
end
class_methods do
def remove_abandoned_creations
Card.creating.where(updated_at: ..1.day.ago).destroy_all
end
end
def can_recover_abandoned_creation?
abandoned_creations.where(updated_at: 1.day.ago..).any?
end
def recover_abandoned_creation
abandoned_creations.last.tap do |card|
Card.creating.where(creator: creator).excluding(card).destroy_all
end
end
def publish
transaction do
published!
track_event :published
end
end
private
def abandoned_creations
Card.creating.where(creator: creator).where("created_at != updated_at").excluding(self)
end
end