Merge pull request #39530 from kamipo/clear_attribute_change_as_attribute_method

Promote `clear_attribute_change` as attribute methods
This commit is contained in:
Ryuta Kamizono 2020-06-04 07:00:23 +09:00 committed by GitHub
commit 95154c93ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 3 deletions

@ -126,6 +126,7 @@ module Dirty
attribute_method_suffix "_changed?", "_change", "_will_change!", "_was"
attribute_method_suffix "_previously_changed?", "_previous_change", "_previously_was"
attribute_method_affix prefix: "restore_", suffix: "!"
attribute_method_affix prefix: "clear_", suffix: "_change"
end
def initialize_dup(other) # :nodoc:

@ -707,9 +707,9 @@ def increment(attribute, by = 1)
# Returns +self+.
def increment!(attribute, by = 1, touch: nil)
increment(attribute, by)
change = public_send(attribute) - (attribute_in_database(attribute.to_s) || 0)
change = public_send(attribute) - (public_send(:"#{attribute}_in_database") || 0)
self.class.update_counters(id, attribute => change, touch: touch)
clear_attribute_change(attribute) # eww
public_send(:"clear_#{attribute}_change")
self
end

@ -159,7 +159,7 @@ def max_updated_column_timestamp
def clear_timestamp_attributes
all_timestamp_attributes_in_model.each do |attribute_name|
self[attribute_name] = nil
clear_attribute_changes([attribute_name])
clear_attribute_change(attribute_name)
end
end
end

@ -154,12 +154,32 @@ def test_restore_attribute!
pirate = Pirate.create!(catchphrase: "Yar!")
pirate.catchphrase = "Ahoy!"
assert_equal "Ahoy!", pirate.catchphrase
assert_equal ["Yar!", "Ahoy!"], pirate.catchphrase_change
pirate.restore_catchphrase!
assert_nil pirate.catchphrase_change
assert_equal "Yar!", pirate.catchphrase
assert_equal Hash.new, pirate.changes
assert_not_predicate pirate, :catchphrase_changed?
end
def test_clear_attribute_change
pirate = Pirate.create!(catchphrase: "Yar!")
pirate.catchphrase = "Ahoy!"
assert_equal "Ahoy!", pirate.catchphrase
assert_equal ["Yar!", "Ahoy!"], pirate.catchphrase_change
pirate.clear_catchphrase_change
assert_nil pirate.catchphrase_change
assert_equal "Ahoy!", pirate.catchphrase
assert_equal Hash.new, pirate.changes
assert_not_predicate pirate, :catchphrase_changed?
end
def test_nullable_number_not_marked_as_changed_if_new_value_is_blank
pirate = Pirate.new

@ -86,6 +86,7 @@ def test_delete_all
def test_increment_attribute
assert_equal 50, accounts(:signals37).credit_limit
accounts(:signals37).increment! :credit_limit
assert_equal 51, accounts(:signals37, :reload).credit_limit
@ -93,6 +94,16 @@ def test_increment_attribute
assert_equal 53, accounts(:signals37, :reload).credit_limit
end
def test_increment_aliased_attribute
assert_equal 50, accounts(:signals37).available_credit
accounts(:signals37).increment!(:available_credit)
assert_equal 51, accounts(:signals37, :reload).available_credit
accounts(:signals37).increment(:available_credit).increment!(:available_credit)
assert_equal 53, accounts(:signals37, :reload).available_credit
end
def test_increment_nil_attribute
assert_nil topics(:first).parent_id
topics(:first).increment! :parent_id