Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add predicate to check if record was soft deleted or hard deleted #136

Merged
merged 1 commit into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -126,6 +125,7 @@ def destroy_fully!
end

stale_paranoid_value
@destroyed = true
freeze
end
end
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down