Skip to content

Commit

Permalink
Touch record on paranoia-destroy. Fixes #296
Browse files Browse the repository at this point in the history
Touch record on destroy by leveraging the paranoia_destroy_attributes.
Applied the same to the restore-method as this eliminates the extra query.
  • Loading branch information
rbr committed Jan 28, 2017
1 parent b5b3823 commit 7eaff42
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/paranoia.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/paranoia_test.rb
Expand Up @@ -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)
Expand Down

0 comments on commit 7eaff42

Please sign in to comment.