diff --git a/.test b/.test new file mode 100644 index 00000000..e69de29b diff --git a/lib/paranoia.rb b/lib/paranoia.rb index a2298953..d10e71ff 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) + update_columns(attributes) + attributes.keys.each do |key| + send("#{key}_will_change!") + end + 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