Commit 099e18dd by Ryan Bigg

Merge pull request #151 from astronz/really-destroy-has-one

really_destroy! and has_one associations
parents d0e603e9 acd39d7b
...@@ -154,10 +154,14 @@ class ActiveRecord::Base ...@@ -154,10 +154,14 @@ class ActiveRecord::Base
if dependent_reflections.any? if dependent_reflections.any?
dependent_reflections.each do |name, _| dependent_reflections.each do |name, _|
associated_records = self.send(name) associated_records = self.send(name)
# has_one association can return nil
if associated_records && associated_records.respond_to?(:with_deleted)
# Paranoid models will have this method, non-paranoid models will not # Paranoid models will have this method, non-paranoid models will not
associated_records = associated_records.with_deleted if associated_records.respond_to?(:with_deleted) associated_records.with_deleted.each(&:really_destroy!)
associated_records.each(&:really_destroy!)
self.send(name).reload self.send(name).reload
elsif associated_records && !associated_records.respond_to?(:each) # single record
associated_records.really_destroy!
end
end end
end end
touch_paranoia_column if ActiveRecord::VERSION::STRING >= "4.1" touch_paranoia_column if ActiveRecord::VERSION::STRING >= "4.1"
......
...@@ -436,6 +436,21 @@ class ParanoiaTest < test_framework ...@@ -436,6 +436,21 @@ class ParanoiaTest < test_framework
assert hasOne.reload.deleted_at.nil? assert hasOne.reload.deleted_at.nil?
end end
# covers #131
def test_has_one_really_destroy_with_nil
model = ParanoidModelWithHasOne.create
model.really_destroy!
refute ParanoidModelWithBelong.unscoped.exists?(model.id)
end
def test_has_one_really_destroy_with_record
model = ParanoidModelWithHasOne.create { |record| record.build_paranoid_model_with_belong }
model.really_destroy!
refute ParanoidModelWithBelong.unscoped.exists?(model.id)
end
def test_observers_notified def test_observers_notified
a = ParanoidModelWithObservers.create a = ParanoidModelWithObservers.create
a.destroy a.destroy
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment