From 2e4938b452af3db855fc2b371b68aaf90359b26a Mon Sep 17 00:00:00 2001 From: Mattias Pfeiffer Date: Thu, 19 Jan 2017 11:03:51 +0100 Subject: [PATCH] Add dirty tracking of paranoia attributes --- lib/paranoia.rb | 12 ++++++++++-- test/paranoia_test.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index 0c0e3657..37b7c829 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -94,7 +94,7 @@ def delete # if a transaction exists, add the record so that after_commit # callbacks can be run add_to_transaction - update_columns(paranoia_destroy_attributes) + paranoia_update_columns(paranoia_destroy_attributes) elsif !frozen? assign_attributes(paranoia_destroy_attributes) end @@ -109,7 +109,7 @@ def restore!(opts = {}) noop_if_frozen = ActiveRecord.version < Gem::Version.new("4.1") if (noop_if_frozen && !@attributes.frozen?) || !noop_if_frozen write_attribute paranoia_column, paranoia_sentinel_value - update_columns(paranoia_restore_attributes) + paranoia_update_columns(paranoia_restore_attributes) touch end restore_associated_records if opts[:recursive] @@ -125,6 +125,14 @@ def paranoia_destroyed? end alias :deleted? :paranoia_destroyed? + def paranoia_update_columns(attributes) + attributes.keys.each do |key| + send("#{key}_will_change!") + end + update_columns(attributes) + changes_applied + end + def really_destroy! transaction do run_callbacks(:real_destroy) do diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index a1bceeee..c422a159 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -175,6 +175,13 @@ def test_update_columns_on_paranoia_destroyed assert record.update_columns deleted_at: Time.now end + def test_dirty_tracking_on_paranoia_destroyed + record = ParentModel.create + record.destroy + + assert_equal record.previous_changes.keys, ["deleted_at"] + end + def test_scoping_behavior_for_paranoid_models parent1 = ParentModel.create parent2 = ParentModel.create @@ -209,6 +216,13 @@ def test_destroy_behavior_for_custom_column_models assert_equal 1, model.class.deleted.count end + def test_dirty_tracking_for_custom_column_models + record = CustomColumnModel.create + record.destroy + + assert_equal record.previous_changes.keys, ["destroyed_at"] + end + def test_default_sentinel_value assert_equal nil, ParanoidModel.paranoia_sentinel_value end @@ -240,6 +254,12 @@ def test_active_column_model assert_equal 1, model.class.deleted.count end + def test_dirty_tracking_for_active_column_model + record = ActiveColumnModel.create + record.destroy + assert_equal record.previous_changes.keys, ["deleted_at", "active"] + end + def test_active_column_model_with_uniqueness_validation_only_checks_non_deleted_records a = ActiveColumnModelWithUniquenessValidation.create!(name: "A") a.destroy @@ -426,6 +446,17 @@ def test_restore assert_equal false, model.paranoia_destroyed? end + def test_dirty_tracking_on_restore + model = ParanoidModel.new + model.save + id = model.id + model.destroy + + model = ParanoidModel.only_deleted.find(id) + model.restore! + assert_equal model.previous_changes.keys, ['deleted_at'] + end + def test_restore_on_object_return_self model = ParanoidModel.create model.destroy