Skip to content

Commit

Permalink
Fix many callback problems such as callbacks not being called or bein…
Browse files Browse the repository at this point in the history
…g called too many times.
  • Loading branch information
aymeric-ledorze committed Oct 10, 2023
1 parent 8ee8664 commit a2a6a83
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 24 deletions.
2 changes: 2 additions & 0 deletions lib/acts_as_paranoid/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ def destroy_fully!
decrement_counters_on_associations
end

@_trigger_destroy_callback = true

@destroyed = true
freeze
end
Expand Down
104 changes: 80 additions & 24 deletions test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,31 +123,32 @@ class ParanoidWithCallback < ActiveRecord::Base
before_recover :call_me_before_recover
after_recover :call_me_after_recover

def initialize(*attrs)
@called_before_destroy = false
@called_after_destroy = false
@called_after_commit_on_destroy = false
super(*attrs)
end
set_callback :initialize, lambda {
@called_before_destroy = 0
@called_after_destroy = 0
@called_after_commit_on_destroy = 0
@called_before_recover = 0
@called_after_recover = 0
}

def call_me_before_destroy
@called_before_destroy = true
@called_before_destroy += 1
end

def call_me_after_destroy
@called_after_destroy = true
@called_after_destroy += 1
end

def call_me_after_commit_on_destroy
@called_after_commit_on_destroy = true
@called_after_commit_on_destroy += 1
end

def call_me_before_recover
@called_before_recover = true
@called_before_recover += 1
end

def call_me_after_recover
@called_after_recover = true
@called_after_recover += 1
end
end

Expand Down Expand Up @@ -779,21 +780,32 @@ def test_paranoid_destroy_callbacks
@paranoid_with_callback.destroy
end

assert @paranoid_with_callback.called_before_destroy
assert @paranoid_with_callback.called_after_destroy
assert @paranoid_with_callback.called_after_commit_on_destroy
assert_equal 1, @paranoid_with_callback.called_before_destroy
assert_equal 1, @paranoid_with_callback.called_after_destroy
assert_equal 1, @paranoid_with_callback.called_after_commit_on_destroy
end

def test_paranoid_destroy_destroy_callbacks
@paranoid_with_callback = ParanoidWithCallback.first
ParanoidWithCallback.transaction do
@paranoid_with_callback.destroy.destroy
end

assert_equal 2, @paranoid_with_callback.called_before_destroy
assert_equal 2, @paranoid_with_callback.called_after_destroy
assert_equal 1, @paranoid_with_callback.called_after_commit_on_destroy
end

def test_hard_destroy_callbacks
@paranoid_with_callback = ParanoidWithCallback.first

ParanoidWithCallback.transaction do
@paranoid_with_callback.destroy!
@paranoid_with_callback.destroy_fully!
end

assert @paranoid_with_callback.called_before_destroy
assert @paranoid_with_callback.called_after_destroy
assert @paranoid_with_callback.called_after_commit_on_destroy
assert_equal 1, @paranoid_with_callback.called_before_destroy
assert_equal 1, @paranoid_with_callback.called_after_destroy
assert_equal 1, @paranoid_with_callback.called_after_commit_on_destroy
end

def test_recovery_callbacks
Expand All @@ -802,22 +814,66 @@ def test_recovery_callbacks
ParanoidWithCallback.transaction do
@paranoid_with_callback.destroy

assert_nil @paranoid_with_callback.called_before_recover
assert_nil @paranoid_with_callback.called_after_recover
assert_equal 1, @paranoid_with_callback.called_before_destroy
assert_equal 1, @paranoid_with_callback.called_after_destroy
assert_equal 0, @paranoid_with_callback.called_after_commit_on_destroy
assert_equal 0, @paranoid_with_callback.called_before_recover
assert_equal 0, @paranoid_with_callback.called_after_recover

@paranoid_with_callback.recover
end

assert @paranoid_with_callback.called_before_recover
assert @paranoid_with_callback.called_after_recover
assert_equal 1, @paranoid_with_callback.called_before_destroy
assert_equal 1, @paranoid_with_callback.called_after_destroy
assert_includes 0..1, @paranoid_with_callback.called_after_commit_on_destroy
assert_equal 1, @paranoid_with_callback.called_before_recover
assert_equal 1, @paranoid_with_callback.called_after_recover
end

def test_recovery_callbacks_with_2_transactions
@paranoid_with_callback = ParanoidWithCallback.first

ParanoidWithCallback.transaction do
@paranoid_with_callback.destroy
end

assert_equal 1, @paranoid_with_callback.called_before_destroy
assert_equal 1, @paranoid_with_callback.called_after_destroy
assert_equal 1, @paranoid_with_callback.called_after_commit_on_destroy
assert_equal 0, @paranoid_with_callback.called_before_recover
assert_equal 0, @paranoid_with_callback.called_after_recover

ParanoidWithCallback.transaction do
@paranoid_with_callback.recover
end

assert_equal 1, @paranoid_with_callback.called_before_destroy
assert_equal 1, @paranoid_with_callback.called_after_destroy
assert_equal 1, @paranoid_with_callback.called_after_commit_on_destroy
assert_equal 1, @paranoid_with_callback.called_before_recover
assert_equal 1, @paranoid_with_callback.called_after_recover
end

def test_paranoid_destroy_with_update_callbacks
@paranoid_with_callback = ParanoidWithCallback.first
ParanoidWithCallback.transaction do
@paranoid_with_callback.destroy
end
ParanoidWithCallback.transaction do
@paranoid_with_callback.update(name: "still paranoid")
end

assert_equal 1, @paranoid_with_callback.called_before_destroy
assert_equal 1, @paranoid_with_callback.called_after_destroy
assert_equal 1, @paranoid_with_callback.called_after_commit_on_destroy
end

def test_recovery_callbacks_without_destroy
@paranoid_with_callback = ParanoidWithCallback.first
@paranoid_with_callback.recover

assert_nil @paranoid_with_callback.called_before_recover
assert_nil @paranoid_with_callback.called_after_recover
assert_equal 0, @paranoid_with_callback.called_before_recover
assert_equal 0, @paranoid_with_callback.called_after_recover
end

def test_delete_by_multiple_id_is_paranoid
Expand Down

0 comments on commit a2a6a83

Please sign in to comment.