From 012fdb0b34b70a92a6198760c9c4c868204b3d8d Mon Sep 17 00:00:00 2001 From: Markus Doits Date: Tue, 26 Mar 2024 19:44:13 +0100 Subject: [PATCH] make `will_save_change_to_*?` and `saved_change_to_*?` accept kwargs, too like the Rails original methods do since Rails 6.1, see: - https://github.com/rails/rails/blob/6-1-stable/activerecord/lib/active_record/attribute_methods/dirty.rb#L51 - https://github.com/rails/rails/blob/6-1-stable/activerecord/lib/active_record/attribute_methods/dirty.rb#L101 --- lib/mobility/plugins/active_model/dirty.rb | 10 +++++++- .../plugins/active_record/dirty_spec.rb | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/mobility/plugins/active_model/dirty.rb b/lib/mobility/plugins/active_model/dirty.rb index 0f869747..5160b306 100644 --- a/lib/mobility/plugins/active_model/dirty.rb +++ b/lib/mobility/plugins/active_model/dirty.rb @@ -119,6 +119,14 @@ def self.append_locale(attr_name) class HandlerMethodsBuilder < Module attr_reader :klass + PATTERNS_WITH_KWARGS = + %w[ + %s_changed? + %s_previously_changed? + will_save_change_to_%s? + saved_change_to_%s? + ].freeze + # @param [Class] klass Dirty class to mimic def initialize(klass) @klass = klass @@ -135,7 +143,7 @@ def define_handler_methods public_patterns.each do |pattern| method_name = pattern % 'attribute' - kwargs = (pattern == '%s_changed?' || pattern == '%s_previously_changed?') ? ', **kwargs' : '' + kwargs = PATTERNS_WITH_KWARGS.include?(pattern) ? ', **kwargs' : '' module_eval <<-EOM, __FILE__, __LINE__ + 1 def #{method_name}(attr_name, *rest#{kwargs}) if (mutations_from_mobility.attribute_changed?(attr_name) || diff --git a/spec/mobility/plugins/active_record/dirty_spec.rb b/spec/mobility/plugins/active_record/dirty_spec.rb index cb788940..52d3381b 100644 --- a/spec/mobility/plugins/active_record/dirty_spec.rb +++ b/spec/mobility/plugins/active_record/dirty_spec.rb @@ -220,12 +220,16 @@ def values; @values ||= {}; end expect(instance.title_previous_change).to eq([nil, "foo"]) expect(instance.saved_change_to_title?).to eq(true) + expect(instance.saved_change_to_title?(from: nil, to: 'foo')).to eq(true) + expect(instance.saved_change_to_title?(from: nil, to: 'foz')).to eq(false) expect(instance.saved_change_to_title).to eq([nil, "foo"]) expect(instance.title_before_last_save).to eq(nil) expect(instance.title_in_database).to eq("foo") # attribute handlers expect(instance.saved_change_to_attribute?(:title_en)).to eq(true) + expect(instance.saved_change_to_attribute?(:title_en, from: nil, to: 'foo')).to eq(true) + expect(instance.saved_change_to_attribute?(:title_en, from: nil, to: 'foz')).to eq(false) expect(instance.saved_change_to_attribute(:title_en)).to eq([nil, 'foo']) expect(instance.attribute_before_last_save(:title_en)).to eq(nil) expect(instance.attribute_in_database(:title_en)).to eq('foo') @@ -239,6 +243,9 @@ def values; @values ||= {}; end expect(instance.title_changed?(from: 'foo', to: 'baz')).to eq(false) expect(instance.title_change).to eq(["foo", "bar"]) expect(instance.title_was).to eq("foo") + expect(instance.will_save_change_to_title?).to eq(true) + expect(instance.will_save_change_to_title?(from: 'foo', to: 'bar')).to eq(true) + expect(instance.will_save_change_to_title?(from: 'foo', to: 'baz')).to eq(false) instance.save @@ -249,6 +256,8 @@ def values; @values ||= {}; end expect(instance.title_changed?).to eq(false) expect(instance.saved_change_to_title?).to eq(true) + expect(instance.saved_change_to_title?(from: 'foo', to: 'bar')).to eq(true) + expect(instance.saved_change_to_title?(from: 'foo', to: 'baz')).to eq(false) expect(instance.saved_change_to_title).to eq(["foo", "bar"]) expect(instance.title_before_last_save).to eq("foo") expect(instance.will_save_change_to_title?).to eq(false) @@ -257,6 +266,8 @@ def values; @values ||= {}; end # attribute handlers expect(instance.saved_change_to_attribute?(:title_en)).to eq(true) + expect(instance.saved_change_to_attribute?(:title_en, from: 'foo', to: 'bar')).to eq(true) + expect(instance.saved_change_to_attribute?(:title_en, from: 'foo', to: 'baz')).to eq(false) expect(instance.saved_change_to_attribute(:title_en)).to eq(['foo', 'bar']) expect(instance.attribute_before_last_save(:title_en)).to eq('foo') expect(instance.will_save_change_to_attribute?(:title_en)).to eq(false) @@ -271,16 +282,24 @@ def values; @values ||= {}; end expect(instance.title_changed?).to eq(true) expect(instance.saved_change_to_title?).to eq(true) + expect(instance.saved_change_to_title?(from: 'foo', to: 'bar')).to eq(true) + expect(instance.saved_change_to_title?(from: 'foo', to: 'baz')).to eq(false) expect(instance.saved_change_to_title).to eq(["foo", "bar"]) expect(instance.title_before_last_save).to eq("foo") expect(instance.will_save_change_to_title?).to eq(true) + expect(instance.will_save_change_to_title?(from: 'bar', to: 'bar')).to eq(true) + expect(instance.will_save_change_to_title?(from: 'bar', to: 'baz')).to eq(false) expect(instance.title_change_to_be_saved).to eq(["bar", "bar"]) expect(instance.title_in_database).to eq("bar") expect(instance.saved_change_to_attribute?(:title_en)).to eq(true) + expect(instance.saved_change_to_attribute?(:title_en, from: 'foo', to: 'bar')).to eq(true) + expect(instance.saved_change_to_attribute?(:title_en, from: 'foo', to: 'baz')).to eq(false) expect(instance.saved_change_to_attribute(:title_en)).to eq(['foo', 'bar']) expect(instance.attribute_before_last_save(:title_en)).to eq('foo') expect(instance.will_save_change_to_attribute?(:title_en)).to eq(true) + expect(instance.will_save_change_to_attribute?(:title_en, from: 'bar', to: 'bar')).to eq(true) + expect(instance.will_save_change_to_attribute?(:title_en, from: 'bar', to: 'baz')).to eq(false) expect(instance.attribute_change_to_be_saved(:title_en)).to eq(['bar', 'bar']) expect(instance.attribute_in_database(:title_en)).to eq('bar') end @@ -291,6 +310,8 @@ def values; @values ||= {}; end expect(instance.title_changed?).to eq(false) expect(instance.saved_change_to_title?).to eq(true) + expect(instance.saved_change_to_title?(from: 'bar', to: 'bar')).to eq(true) + expect(instance.saved_change_to_title?(from: 'bar', to: 'baz')).to eq(false) expect(instance.saved_change_to_title).to eq(["bar", "bar"]) expect(instance.title_before_last_save).to eq("bar") expect(instance.will_save_change_to_title?).to eq(false) @@ -298,6 +319,8 @@ def values; @values ||= {}; end expect(instance.title_in_database).to eq("bar") expect(instance.saved_change_to_attribute?(:title_en)).to eq(true) + expect(instance.saved_change_to_attribute?(:title_en, from: 'bar', to: 'bar')).to eq(true) + expect(instance.saved_change_to_attribute?(:title_en, from: 'bar', to: 'baz')).to eq(false) expect(instance.saved_change_to_attribute(:title_en)).to eq(['bar', 'bar']) expect(instance.attribute_before_last_save(:title_en)).to eq('bar') expect(instance.will_save_change_to_attribute?(:title_en)).to eq(false)