Skip to content

Commit 2bc73cb

Browse files
rbrrbr
rbr
authored andcommitted
Touch record on paranoia-destroy. Fixes #296
Touch record on destroy by leveraging the paranoia_destroy_attributes. Applied the same to the restore-method as this eliminates the extra query.
1 parent 6972b22 commit 2bc73cb

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/paranoia.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ def restore!(opts = {})
110110
if (noop_if_frozen && !@attributes.frozen?) || !noop_if_frozen
111111
write_attribute paranoia_column, paranoia_sentinel_value
112112
update_columns(paranoia_restore_attributes)
113-
touch
114113
end
115114
restore_associated_records if opts[:recursive]
116115
end
@@ -154,13 +153,17 @@ def really_destroy!
154153
def paranoia_restore_attributes
155154
{
156155
paranoia_column => paranoia_sentinel_value
157-
}
156+
}.merge(timestamp_attributes_with_current_time)
158157
end
159158

160159
def paranoia_destroy_attributes
161160
{
162161
paranoia_column => current_time_from_proper_timezone
163-
}
162+
}.merge(timestamp_attributes_with_current_time)
163+
end
164+
165+
def timestamp_attributes_with_current_time
166+
timestamp_attributes_for_update_in_model.each_with_object({}) { |attr,hash| hash[attr] = current_time_from_proper_timezone }
164167
end
165168

166169
# restore associated records that have been soft deleted when

test/paranoia_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,13 @@ def test_validates_uniqueness_still_works_on_non_deleted_records
774774
refute b.valid?
775775
end
776776

777+
def test_updated_at_modification_on_destroy
778+
paranoid_model = ParanoidModelWithTimestamp.create(:parent_model => ParentModel.create, :updated_at => 1.day.ago)
779+
assert paranoid_model.updated_at < 10.minutes.ago
780+
paranoid_model.destroy
781+
assert paranoid_model.updated_at > 10.minutes.ago
782+
end
783+
777784
def test_updated_at_modification_on_restore
778785
parent1 = ParentModel.create
779786
pt1 = ParanoidModelWithTimestamp.create(:parent_model => parent1)

0 commit comments

Comments
 (0)