From 29d07e732673f4f5ba208614ff35f8294295d9a4 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Mon, 1 Dec 2025 15:39:45 +0100 Subject: [PATCH] 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 --- app/models/card/mentions.rb | 2 +- app/models/card/statuses.rb | 14 ++++++++++++++ test/models/card/statuses_test.rb | 22 ++++++++++++++++++++++ test/models/concerns/mentions_test.rb | 2 +- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/app/models/card/mentions.rb b/app/models/card/mentions.rb index 3ce74726b..11e35b97c 100644 --- a/app/models/card/mentions.rb +++ b/app/models/card/mentions.rb @@ -9,7 +9,7 @@ module Card::Mentions end def should_check_mentions? - saved_change_to_status? && published? + was_just_published? end end end diff --git a/app/models/card/statuses.rb b/app/models/card/statuses.rb index d339acd17..53b243e4e 100644 --- a/app/models/card/statuses.rb +++ b/app/models/card/statuses.rb @@ -4,7 +4,10 @@ module Card::Statuses 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)) } @@ -17,10 +20,21 @@ module Card::Statuses 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 diff --git a/test/models/card/statuses_test.rb b/test/models/card/statuses_test.rb index ba801bfd9..396e1f615 100644 --- a/test/models/card/statuses_test.rb +++ b/test/models/card/statuses_test.rb @@ -67,4 +67,26 @@ class Card::StatusesTest < ActiveSupport::TestCase assert_equal Time.current, card.created_at end + + test "detect drafts that were just published" do + Current.session = sessions(:david) + + card = boards(:writebook).cards.create! creator: users(:kevin), title: "Draft Card" + assert card.drafted? + assert_not card.was_just_published? + + card.publish + + assert card.was_just_published? + assert_not Card.find(card.id).was_just_published? + end + + test "detect cards that were created and published" do + Current.session = sessions(:david) + + card = boards(:writebook).cards.create! creator: users(:kevin), title: "Published Card", status: :published + assert card.was_just_published? + + assert_not Card.find(card.id).was_just_published? + end end diff --git a/test/models/concerns/mentions_test.rb b/test/models/concerns/mentions_test.rb index 833d6df0d..48b94e6f1 100644 --- a/test/models/concerns/mentions_test.rb +++ b/test/models/concerns/mentions_test.rb @@ -23,7 +23,7 @@ class MentionsTest < ActiveSupport::TestCase card = Card.find(card.id) assert_difference -> { Mention.count }, +1 do - card.published! + card.publish end end end