Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
Merge branch 'dmathieu-remove_unsaved_objects' into rails3.2
Browse files Browse the repository at this point in the history
Fixes #94 error when destroying an unpersisted object.
  • Loading branch information
chuckg committed Feb 28, 2013
2 parents cfa4aec + 734775e commit aa2f9c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/acts_as_paranoid/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def destroy!
run_callbacks :destroy do
destroy_dependent_associations!
# Handle composite keys, otherwise we would just use `self.class.primary_key.to_sym => self.id`.
self.class.delete_all!(Hash[[Array(self.class.primary_key), Array(self.id)].transpose])
self.class.delete_all!(Hash[[Array(self.class.primary_key), Array(self.id)].transpose]) if persisted?
self.paranoid_value = self.class.delete_now_value
freeze
end
Expand All @@ -106,7 +106,7 @@ def destroy
with_transaction_returning_status do
run_callbacks :destroy do
# Handle composite keys, otherwise we would just use `self.class.primary_key.to_sym => self.id`.
self.class.delete_all(Hash[[Array(self.class.primary_key), Array(self.id)].transpose])
self.class.delete_all(Hash[[Array(self.class.primary_key), Array(self.id)].transpose]) if persisted?
self.paranoid_value = self.class.delete_now_value
self
end
Expand Down
14 changes: 14 additions & 0 deletions test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ def test_real_removal
assert_empty ParanoidTime.with_deleted.all
end

def test_non_persisted_destroy
pt = ParanoidTime.new
assert_nil pt.paranoid_value
pt.destroy
assert_not_nil pt.paranoid_value
end

def test_non_persisted_destroy!
pt = ParanoidTime.new
assert_nil pt.paranoid_value
pt.destroy!
assert_not_nil pt.paranoid_value
end

def test_recovery
assert_equal 3, ParanoidBoolean.count
ParanoidBoolean.first.destroy
Expand Down

0 comments on commit aa2f9c4

Please sign in to comment.