Skip to content

Commit

Permalink
Fix infinite loop with ActiveRecord 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aymeric-ledorze committed Oct 28, 2019
1 parent 9bf5507 commit 9434f32
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 2 additions & 0 deletions gemfiles/active_record_60.gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
source 'https://rubygems.org'

gem 'activejob', '~> 6.0.0', require: 'active_job'
gem 'activerecord', '~> 6.0.0', require: 'active_record'
gem 'activesupport', '~> 6.0.0', require: 'active_support'
gem 'activestorage', '~> 6.0.0'

# Development dependencies
group :development do
Expand Down
24 changes: 18 additions & 6 deletions lib/acts_as_paranoid/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ def destroy_dependent_associations!
end
end

def deleted?
def deleted?(value = self.paranoid_value)
@destroyed || !if self.class.string_type_with_deleted_value?
paranoid_value != self.class.delete_now_value || paranoid_value.nil?
value != self.class.delete_now_value || value.nil?
elsif self.class.boolean_type_not_nullable?
paranoid_value == false
value == false
else
paranoid_value.nil?
value.nil?
end
end

Expand Down Expand Up @@ -307,15 +307,27 @@ def remember_transaction_record_state_with_paranoid
end

def remember_trigger_destroy_callback_before_last_commit
if _committed_already_called && defined?(@_trigger_destroy_callback) && !@destroyed && deleted?
if _committed_already_called && defined?(@_trigger_destroy_callback) && !@_trigger_destroy_callback.nil? && !@destroyed && deleted_without_sync_with_transaction_state?
@_trigger_destroy_callback = nil
end
end
end

def force_clear_transaction_record_state_with_paranoid
@_trigger_destroy_callback = nil if defined?(@_trigger_destroy_callback) && !@destroyed && deleted?
if defined?(@_trigger_destroy_callback) && !@_trigger_destroy_callback.nil? && !@destroyed && deleted_without_sync_with_transaction_state?
@_trigger_destroy_callback = nil
end
force_clear_transaction_record_state_without_paranoid
end

if ActiveRecord::VERSION::MAJOR > 5
def deleted_without_sync_with_transaction_state?
deleted?(@attributes.fetch_value(self.class.paranoid_column.to_s))
end
else
def deleted_without_sync_with_transaction_state?
deleted?
end
end
end
end
2 changes: 1 addition & 1 deletion test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def test_paranoid_destroy_with_update_callbacks
@paranoid_with_callback.destroy
end
ParanoidWithCallback.transaction do
@paranoid_with_callback.update_attributes(:name => "still paranoid")
@paranoid_with_callback.update(:name => "still paranoid")
end

assert_equal 1, @paranoid_with_callback.called_before_destroy
Expand Down

0 comments on commit 9434f32

Please sign in to comment.