Skip to content

Commit

Permalink
Added some consistency on paranoid_value between destroy, delete and …
Browse files Browse the repository at this point in the history
…destroy_fully!

Fix a record being dirty after destroying it.
  • Loading branch information
aymeric-ledorze committed Aug 1, 2018
1 parent 785e83b commit 969856b
Show file tree
Hide file tree
Showing 2 changed files with 33 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 @@ -108,6 +108,7 @@ def paranoid_value
# Straight from ActiveRecord 5.1!
def delete
self.class.delete(id) if persisted?
stale_paranoid_value
@destroyed = true
freeze
end
Expand All @@ -118,7 +119,7 @@ def destroy_fully!
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]) if persisted?
self.paranoid_value = self.class.delete_now_value
stale_paranoid_value
freeze
end
end
Expand All @@ -135,7 +136,7 @@ def destroy!
true
end

self.paranoid_value = self.class.delete_now_value
stale_paranoid_value
self
end
end
Expand Down Expand Up @@ -223,5 +224,10 @@ def get_reflection_class(reflection)
def paranoid_value=(value)
self.send("#{self.class.paranoid_column}=", value)
end

def stale_paranoid_value
self.paranoid_value = self.class.delete_now_value
clear_attribute_changes([self.class.paranoid_column])
end
end
end
25 changes: 25 additions & 0 deletions test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def test_non_persisted_destroy
assert_not_nil pt.paranoid_value
end

def test_non_persisted_delete
pt = ParanoidTime.new
assert_nil pt.paranoid_value
pt.delete
assert_not_nil pt.paranoid_value
end

def test_non_persisted_destroy!
pt = ParanoidTime.new
assert_nil pt.paranoid_value
Expand Down Expand Up @@ -265,6 +272,24 @@ def test_non_recursive_recovery
assert_equal 0, HasOneNotParanoid.count
end

def test_dirty
pt = ParanoidTime.create
pt.destroy
assert_not pt.changed?
end

def test_delete_dirty
pt = ParanoidTime.create
pt.delete
assert_not pt.changed?
end

def test_destroy_fully_dirty
pt = ParanoidTime.create
pt.destroy_fully!
assert_not pt.changed?
end

def test_deleted?
ParanoidTime.first.destroy
assert ParanoidTime.with_deleted.first.deleted?
Expand Down

0 comments on commit 969856b

Please sign in to comment.