From b11150c50bf97ff70435723b18c24532bed71492 Mon Sep 17 00:00:00 2001 From: Aymeric Le Dorze Date: Wed, 1 Aug 2018 12:31:49 +0200 Subject: [PATCH] Sets @destroyed to true only when the record is fully deleted from the database (hard destroy). destroyed?/deleted? returns true iff an object is deleted (soft or hard). destroyed_fully?/deleted_fully? returns true iff an object is deleted from the database (hard destroy). --- lib/acts_as_paranoid/core.rb | 10 ++++++++-- test/test_core.rb | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/acts_as_paranoid/core.rb b/lib/acts_as_paranoid/core.rb index 282a2118..4b262268 100644 --- a/lib/acts_as_paranoid/core.rb +++ b/lib/acts_as_paranoid/core.rb @@ -109,7 +109,6 @@ def paranoid_value def delete self.class.delete(id) if persisted? stale_paranoid_value - @destroyed = true freeze end @@ -126,6 +125,7 @@ def destroy_fully! end stale_paranoid_value + @destroyed = true freeze end end @@ -231,7 +231,7 @@ def destroy_dependent_associations! end def deleted? - !if self.class.string_type_with_deleted_value? + @destroyed || !if self.class.string_type_with_deleted_value? paranoid_value != self.class.delete_now_value || paranoid_value.nil? elsif self.class.boolean_type_not_nullable? paranoid_value == false @@ -242,6 +242,12 @@ def deleted? alias_method :destroyed?, :deleted? + def deleted_fully? + @destroyed + end + + alias_method :destroyed_fully?, :deleted_fully? + private def get_reflection_class(reflection) diff --git a/test/test_core.rb b/test/test_core.rb index 692e5665..79d7f5e8 100644 --- a/test/test_core.rb +++ b/test/test_core.rb @@ -307,6 +307,43 @@ def test_deleted? assert ParanoidString.with_deleted.first.deleted? end + def test_delete_deleted? + ParanoidTime.first.delete + assert ParanoidTime.with_deleted.first.deleted? + + ParanoidString.first.delete + assert ParanoidString.with_deleted.first.deleted? + end + + def test_destroy_fully_deleted? + object = ParanoidTime.first + object.destroy_fully! + assert object.deleted? + + object = ParanoidString.first + object.destroy_fully! + assert object.deleted? + end + + def test_deleted_fully? + ParanoidTime.first.destroy + assert_not ParanoidTime.with_deleted.first.deleted_fully? + + ParanoidString.first.destroy + assert ParanoidString.with_deleted.first.deleted? + end + + def test_delete_deleted_fully? + ParanoidTime.first.delete + assert_not ParanoidTime.with_deleted.first.deleted_fully? + end + + def test_destroy_fully_deleted_fully? + object = ParanoidTime.first + object.destroy_fully! + assert object.deleted_fully? + end + def test_paranoid_destroy_callbacks @paranoid_with_callback = ParanoidWithCallback.first ParanoidWithCallback.transaction do