Skip to content

Commit

Permalink
Merge pull request rubysherpas#151 from astronz/really-destroy-has-one
Browse files Browse the repository at this point in the history
really_destroy! and has_one associations
  • Loading branch information
radar committed Aug 17, 2014
2 parents d0e603e + acd39d7 commit 099e18d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,14 @@ def really_destroy!
if dependent_reflections.any?
dependent_reflections.each do |name, _|
associated_records = self.send(name)
# 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.each(&:really_destroy!)
self.send(name).reload
# 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
associated_records.with_deleted.each(&:really_destroy!)
self.send(name).reload
elsif associated_records && !associated_records.respond_to?(:each) # single record
associated_records.really_destroy!
end
end
end
touch_paranoia_column if ActiveRecord::VERSION::STRING >= "4.1"
Expand Down
15 changes: 15 additions & 0 deletions test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,21 @@ def test_restore_with_nil_has_one_association

assert hasOne.reload.deleted_at.nil?
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
a = ParanoidModelWithObservers.create
Expand Down

0 comments on commit 099e18d

Please sign in to comment.