Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'dmathieu-remove_unsaved_objects' into rails3.2

Fixes #94 error when destroying an unpersisted object.
  • Loading branch information...
commit aa2f9c48500bfba322e40469ffe6480c7c510b9f 2 parents cfa4aec + 734775e
@chuckg chuckg authored
Showing with 16 additions and 2 deletions.
  1. +2 −2 lib/acts_as_paranoid/core.rb
  2. +14 −0 test/test_core.rb
View
4 lib/acts_as_paranoid/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
14 test/test_core.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.