Skip to content

Commit

Permalink
Sets @DESTROYED to true only when the record is fully deleted from th…
Browse files Browse the repository at this point in the history
…e 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).
  • Loading branch information
aymeric-ledorze committed Aug 1, 2018
1 parent 969856b commit b13397a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/acts_as_paranoid/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ def paranoid_value
def delete
self.class.delete(id) if persisted?
stale_paranoid_value
@destroyed = true
freeze
end

Expand All @@ -120,6 +119,7 @@ def destroy_fully!
# 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]) if persisted?
stale_paranoid_value
@destroyed = true
freeze
end
end
Expand Down Expand Up @@ -200,7 +200,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
Expand All @@ -211,6 +211,12 @@ def deleted?

alias_method :destroyed?, :deleted?

def deleted_fully?
@destroyed
end

alias_method :destroyed_fully?, :deleted_fully?

private

def get_reflection_class(reflection)
Expand Down
37 changes: 37 additions & 0 deletions test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,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
Expand Down

0 comments on commit b13397a

Please sign in to comment.