Commit 7a5bd3b5 by John Hawthorn

Fix behaviour for new and destroyed records

parent 356b1357
......@@ -98,13 +98,10 @@ module Paranoia
# @param with_transaction [Boolean] exec with ActiveRecord Transactions.
def touch_paranoia_column(with_transaction=false)
raise ActiveRecord::ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
return self if really_destroyed? || new_record?
# This method is (potentially) called from really_destroy
# The object the method is being called on may be frozen
# Let's not touch it if it's frozen.
unless self.frozen?
if persisted?
touch(paranoia_column)
elsif !frozen?
write_attribute(paranoia_column, current_time_from_proper_timezone)
end
self
end
......@@ -175,7 +172,7 @@ class ActiveRecord::Base
end
end
end
touch_paranoia_column if ActiveRecord::VERSION::STRING >= "4.1"
write_attribute(paranoia_column, current_time_from_proper_timezone)
destroy_without_paranoia
end
......
......@@ -307,6 +307,32 @@ class ParanoiaTest < test_framework
end
end
def test_destroy_on_really_destroyed_record
model = ParanoidModel.create!
model.really_destroy!
assert model.really_destroyed?
assert model.paranoia_destroyed?
model.destroy
assert model.really_destroyed?
assert model.paranoia_destroyed?
end
def test_destroy_on_unsaved_record
# Just to demonstrate the AR behaviour
model = NonParanoidModel.new
model.destroy!
assert model.really_destroyed?
model.destroy!
assert model.really_destroyed?
# Mirrors behaviour above
model = ParanoidModel.new
model.destroy!
assert model.paranoia_destroyed?
model.destroy!
assert model.paranoia_destroyed?
end
def test_restore
model = ParanoidModel.new
model.save
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment