From c62bf7674c9722d65a7bc4e13229825768bd5f2b Mon Sep 17 00:00:00 2001 From: Mattias Pfeiffer Date: Thu, 19 Jan 2017 11:03:51 +0100 Subject: [PATCH 1/3] 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 a2298953..b2a0cfcf 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -97,7 +97,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 @@ -114,7 +114,7 @@ def restore!(opts = {}) if within_recovery_window?(recovery_window_range) && ((noop_if_frozen && !@attributes.frozen?) || !noop_if_frozen) @_disable_counter_cache = !deleted? write_attribute paranoia_column, paranoia_sentinel_value - update_columns(paranoia_restore_attributes) + paranoia_update_columns(paranoia_restore_attributes) each_counter_cached_associations do |association| if send(association.reflection.name) association.increment_counters @@ -146,6 +146,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 8c4c2d3f..04b83b06 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -178,6 +178,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 @@ -226,6 +233,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_nil ParanoidModel.paranoia_sentinel_value end @@ -257,6 +271,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 @@ -443,6 +463,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 From 5ab3c980f95e004cbd55e1db258fe8ec24c2e9bf Mon Sep 17 00:00:00 2001 From: David Garcia Date: Tue, 8 Aug 2017 17:38:47 +0200 Subject: [PATCH 2/3] Reorder update_columns --- lib/paranoia.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index b2a0cfcf..d10e71ff 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -147,10 +147,10 @@ def paranoia_destroyed? alias :deleted? :paranoia_destroyed? def paranoia_update_columns(attributes) + update_columns(attributes) attributes.keys.each do |key| send("#{key}_will_change!") end - update_columns(attributes) changes_applied end From 67da6c95de94394d4c9031734de80692479dcb1d Mon Sep 17 00:00:00 2001 From: David Garcia Date: Tue, 8 Aug 2017 17:42:14 +0200 Subject: [PATCH 3/3] test --- .test | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 .test diff --git a/.test b/.test new file mode 100644 index 00000000..e69de29b