Do not trigger after_commit :destroy callback again on destroy if record previously was destroyed

This commit is contained in:
Ben Sheldon 2022-10-04 17:28:19 -07:00
parent 0c4d89c2b5
commit e32c7043d7
No known key found for this signature in database
GPG Key ID: 862102672FFD9973
3 changed files with 24 additions and 8 deletions

@ -1,3 +1,10 @@
* Only trigger `after_commit :destroy` callbacks when a database row is deleted.
This prevents `after_commit :destroy` callbacks from being triggered again
when `destroy` is called multiple times on the same record.
*Ben Sheldon*
* Fix `ciphertext_for` for yet-to-be-encrypted values.
Previously, `ciphertext_for` returned the cleartext of values that had not

@ -676,11 +676,7 @@ def delete
def destroy
_raise_readonly_record_error if readonly?
destroy_associations
@_trigger_destroy_callback = if persisted?
destroy_row > 0
else
true
end
@_trigger_destroy_callback = persisted? && destroy_row > 0
@destroyed = true
freeze
end

@ -204,12 +204,12 @@ def test_only_call_after_commit_on_create_after_transaction_commits_for_new_reco
assert_equal [], reply.history
end
def test_only_call_after_commit_on_destroy_after_transaction_commits_for_destroyed_new_record
def test_no_after_commit_on_destroy_after_transaction_commits_for_destroyed_new_record
new_record = TopicWithCallbacks.new(title: "New topic", written_on: Date.today)
add_transaction_execution_blocks new_record
new_record.destroy
assert_equal [:commit_on_destroy], new_record.history
assert_equal [], new_record.history
end
def test_save_in_after_create_commit_wont_invoke_extra_after_create_commit
@ -674,7 +674,7 @@ class TopicWithCallbacksOnUpdate < TopicWithHistory
def before_save_for_transaction; end
end
def test_trigger_once_on_multiple_deletions
def test_trigger_once_on_multiple_deletion_within_transaction
TopicWithCallbacksOnDestroy.clear_history
topic = TopicWithCallbacksOnDestroy.new
topic.save
@ -689,6 +689,19 @@ def test_trigger_once_on_multiple_deletions
assert_equal [:commit_on_destroy], TopicWithCallbacksOnDestroy.history
end
def test_trigger_once_on_multiple_deletions
TopicWithCallbacksOnDestroy.clear_history
topic = TopicWithCallbacksOnDestroy.new
topic.save
topic_clone = TopicWithCallbacksOnDestroy.find(topic.id)
topic.destroy
topic.destroy
topic_clone.destroy
assert_equal [:commit_on_destroy], TopicWithCallbacksOnDestroy.history
end
def test_rollback_on_multiple_deletions
TopicWithCallbacksOnDestroy.clear_history
topic = TopicWithCallbacksOnDestroy.new