Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix active model *_previously_changed? and active record will_save_change_to_*? and saved_change_to_*? dirty methods to accept kwargs #639

Merged
merged 2 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions 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?' ? ', **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 Expand Up @@ -298,8 +306,10 @@ def attribute_changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN)
(OPTION_NOT_GIVEN == to || fetch_value(attr_name) == to)
end

def attribute_previously_changed?(attr_name)
previous_changes.include?(attr_name)
def attribute_previously_changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN)
previous_changes.include?(attr_name) &&
(OPTION_NOT_GIVEN == from || attribute_previous_change(attr_name).first == from) &&
(OPTION_NOT_GIVEN == to || attribute_previous_change(attr_name).second == to)
end

def attribute_was(attr_name)
Expand Down
4 changes: 4 additions & 0 deletions spec/mobility/plugins/active_model/dirty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,14 @@ def save
expect(instance.attribute_changed?(:title_en)).to eq(false)

expect(instance.title_previously_changed?).to eq(true)
expect(instance.title_previously_changed?(from: 'foo', to: 'bar')).to eq(true)
expect(instance.title_previously_changed?(from: 'foo', to: 'baz')).to eq(false)
expect(instance.title_previous_change).to eq(["foo", "bar"])
expect(instance.title_changed?).to eq(false)

expect(instance.attribute_previously_changed?(:title_en)).to eq(true)
expect(instance.attribute_previously_changed?(:title_en, from: 'foo', to: 'bar')).to eq(true)
expect(instance.attribute_previously_changed?(:title_en, from: 'foo', to: 'baz')).to eq(false)
expect(instance.attribute_changed?(:title_en)).to eq(false)

expect(instance.title_previously_was).to eq('foo')
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