Files
fizzy/app/models/card/statuses.rb
T
Jorge Manrubia 29d07e7326 Fix: not detecting mentions when publishing saved drafts
The problem was that publishing a card with `#publish` was tracking the event
after updating the status, which was clearing the saved changes and preventing the
code from detecting the mention.

See:
https://app.fizzy.do/5986089/cards/2835
2025-12-01 15:39:45 +01:00

41 lines
1.1 KiB
Ruby

module Card::Statuses
extend ActiveSupport::Concern
included do
enum :status, %w[ drafted published ].index_by(&:itself)
attr_reader :initial_status
before_save :update_created_at_on_publication
before_save :remember_initial_status
after_create -> { track_event :published }, if: :published?
scope :published_or_drafted_by, ->(user) { where(status: :published).or(where(status: :drafted, creator: user)) }
end
def publish
transaction do
published!
track_event :published
end
end
def was_just_published?
initial_status&.drafted? && status_in_database.inquiry.published?
end
private
def update_created_at_on_publication
if will_save_change_to_status? && status_in_database.inquiry.drafted?
self.created_at = Time.current
end
end
# So that we can check it in callbacks when other operations in the transaction clean the changes.
def remember_initial_status
if will_save_change_to_status?
@initial_status ||= status_in_database.to_s.inquiry
end
end
end