Skip to content

Commit

Permalink
make will_save_change_to_*? and saved_change_to_*? accept kwargs,…
Browse files Browse the repository at this point in the history
  • Loading branch information
doits committed Mar 26, 2024
1 parent bb18d5b commit 32d4d97
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/mobility/plugins/active_model/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) ||
Expand Down
23 changes: 23 additions & 0 deletions spec/mobility/plugins/active_record/dirty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -291,13 +310,17 @@ 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)
expect(instance.title_change_to_be_saved).to eq(nil)
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)
Expand Down

0 comments on commit 32d4d97

Please sign in to comment.