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 on: :destroy not being triggered #404

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 26 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
association.decrement_counters
end
@_disable_counter_cache = false
@_trigger_destroy_callback = true
result
end
end
Expand Down Expand Up @@ -183,6 +184,31 @@ def timestamp_attributes_with_current_time
timestamp_attributes_for_update_in_model.each_with_object({}) { |attr,hash| hash[attr] = current_time_from_proper_timezone }
end

def transaction_include_any_action?(actions)
actions.any? do |action|
case action
when :create
transaction_record_state(:new_record)
when :destroy
if ActiveRecord::VERSION::STRING < "5.1.0"
transaction_include_destroy?
else
defined?(@_trigger_destroy_callback) && @_trigger_destroy_callback
end
when :update
update_trigger = !(transaction_record_state(:new_record) || transaction_include_destroy?)
if ActiveRecord::VERSION::STRING >= "5.1.0"
update_trigger &&= (defined?(@_trigger_update_callback) && @_trigger_update_callback)
end
update_trigger
end
end
end

def transaction_include_destroy?
destroyed? || try(:paranoia_destroyed?)
end

# restore associated records that have been soft deleted when
# we called #destroy
def restore_associated_records(recovery_window_range = nil)
Expand Down
29 changes: 29 additions & 0 deletions test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ def test_destroy_behavior_for_plain_models
assert_equal 0, model.class.unscoped.count
end

def test_after_commit_on_destroy_callbacks
model = AfterCommitDestroyModel.new
model.save
model.reset_after_commit_callback_called # clear called callback flags
refute model.after_commit_callback_called
model.destroy

assert model.after_commit_callback_called
end

# Anti-regression test for #81, which would've introduced a bug to break this test.
def test_destroy_behavior_for_plain_models_callbacks
model = CallbackModel.new
Expand Down Expand Up @@ -1371,3 +1381,22 @@ class ParanoidBelongsTo < ActiveRecord::Base
belongs_to :paranoid_has_one
end
end

class AfterCommitDestroyModel < ActiveRecord::Base
self.table_name = "callback_models"

attr_reader :after_commit_callback_called

acts_as_paranoid
after_commit :callback_triggered, on: :destroy

def reset_after_commit_callback_called
@after_commit_callback_called = false
end

private

def callback_triggered
@after_commit_callback_called = true
end
end