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

Commit

Permalink
Add persisted? check to destroy!
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckg committed Feb 28, 2013
1 parent 899c187 commit 734775e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/acts_as_paranoid/core.rb
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 Down
14 changes: 12 additions & 2 deletions test/test_core.rb
Expand Up @@ -68,8 +68,18 @@ def test_real_removal
assert_empty ParanoidTime.with_deleted.all
end

def test_removal_not_persisted
assert ParanoidTime.new.destroy
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
Expand Down

0 comments on commit 734775e

Please sign in to comment.