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
This commit is contained in:
@@ -9,7 +9,7 @@ module Card::Mentions
|
||||
end
|
||||
|
||||
def should_check_mentions?
|
||||
saved_change_to_status? && published?
|
||||
was_just_published?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user