From 7eaff42748c692dc702f2865b0d5fb28dbdf542b Mon Sep 17 00:00:00 2001 From: rbr Date: Sat, 28 Jan 2017 20:15:02 +0100 Subject: [PATCH] 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. --- lib/paranoia.rb | 9 ++++++--- test/paranoia_test.rb | 7 +++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index 489d2d62..fe0e67c8 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -110,7 +110,6 @@ def restore!(opts = {}) if (noop_if_frozen && !@attributes.frozen?) || !noop_if_frozen write_attribute paranoia_column, paranoia_sentinel_value update_columns(paranoia_restore_attributes) - touch end restore_associated_records if opts[:recursive] end @@ -154,13 +153,17 @@ def really_destroy! def paranoia_restore_attributes { paranoia_column => paranoia_sentinel_value - } + }.merge(timestamp_attributes_with_current_time) end def paranoia_destroy_attributes { paranoia_column => current_time_from_proper_timezone - } + }.merge(timestamp_attributes_with_current_time) + end + + def timestamp_attributes_with_current_time + timestamp_attributes_for_update_in_model.each_with_object({}) { |attr,hash| hash[attr] = current_time_from_proper_timezone } end # restore associated records that have been soft deleted when diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index a1bceeee..3951aac5 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -774,6 +774,13 @@ def test_validates_uniqueness_still_works_on_non_deleted_records refute b.valid? end + def test_updated_at_modification_on_destroy + paranoid_model = ParanoidModelWithTimestamp.create(:parent_model => ParentModel.create, :updated_at => 1.day.ago) + assert paranoid_model.updated_at < 10.minutes.ago + paranoid_model.destroy + assert paranoid_model.updated_at > 10.minutes.ago + end + def test_updated_at_modification_on_restore parent1 = ParentModel.create pt1 = ParanoidModelWithTimestamp.create(:parent_model => parent1)