Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix after_commit for Rails 6 #482

Merged
merged 2 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def paranoia_destroy
next unless send(association.reflection.name)
association.decrement_counters
end
@_trigger_destroy_callback = true
@_disable_counter_cache = false
result
end
Expand All @@ -81,6 +82,10 @@ def paranoia_destroy!
raise(ActiveRecord::RecordNotDestroyed.new("Failed to destroy the record", self))
end

def trigger_transactional_callbacks?
super || @_trigger_destroy_callback && paranoia_destroyed?
end

def paranoia_delete
raise ActiveRecord::ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
if persisted?
Expand Down
15 changes: 15 additions & 0 deletions test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ def test_destroy_behavior_for_plain_models_callbacks
assert model.instance_variable_get(:@after_commit_callback_called)
end

def test_destroy_behavior_for_freshly_loaded_plain_models_callbacks
model = CallbackModel.new
model.save

model = CallbackModel.find(model.id)
model.destroy

assert_nil model.instance_variable_get(:@update_callback_called)
assert_nil model.instance_variable_get(:@save_callback_called)
assert_nil model.instance_variable_get(:@validate_called)

assert model.instance_variable_get(:@destroy_callback_called)
assert model.instance_variable_get(:@after_destroy_callback_called)
assert model.instance_variable_get(:@after_commit_callback_called)
end

def test_delete_behavior_for_plain_models_callbacks
model = CallbackModel.new
Expand Down