diff --git a/lib/paranoia.rb b/lib/paranoia.rb index 0b0d743a..1638bde7 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -94,7 +94,7 @@ def paranoia_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 @@ -112,7 +112,8 @@ def restore!(opts = {}) if within_recovery_window?(recovery_window_range) && ((noop_if_frozen && !@attributes.frozen?) || !noop_if_frozen) @_disable_counter_cache = !paranoia_destroyed? write_attribute paranoia_column, paranoia_sentinel_value - update_columns(paranoia_restore_attributes) + paranoia_update_columns(paranoia_restore_attributes) + touch each_counter_cached_associations do |association| if send(association.reflection.name) association.increment_counters @@ -144,6 +145,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!(update_destroy_attributes: true) with_transaction_returning_status do run_callbacks(:real_destroy) do diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 86cc8748..f26e8260 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -257,6 +257,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 @@ -330,6 +337,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_destroy_behavior_for_custom_column_models_with_recovery_options model = CustomColumnModel.new model.save! @@ -377,6 +391,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 @@ -571,6 +591,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