Permalink
Browse files

Merge branch 'dmathieu-remove_unsaved_objects' into rails3.2

Fixes #94 error when destroying an unpersisted object.
  • Loading branch information...
2 parents cfa4aec + 734775e commit aa2f9c48500bfba322e40469ffe6480c7c510b9f @chuckg chuckg committed Feb 28, 2013
Showing with 16 additions and 2 deletions.
  1. +2 −2 lib/acts_as_paranoid/core.rb
  2. +14 −0 test/test_core.rb
@@ -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
@@ -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
View
@@ -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

0 comments on commit aa2f9c4

Please sign in to comment.