mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add *_previously_was attribute methods when dirty tracking (#36836)
This commit is contained in:
parent
603cd18b0b
commit
a0bb19fbfa
3 changed files with 19 additions and 1 deletions
|
@ -1,3 +1,9 @@
|
|||
* Add *_previously_was attribute methods when dirty tracking. Example:
|
||||
|
||||
pirate.update(catchphrase: "Ahoy!")
|
||||
pirate.previous_changes["catchphrase"] # => ["Thar She Blows!", "Ahoy!"]
|
||||
pirate.catchphrase_previously_was # => "Thar She Blows!"
|
||||
|
||||
*DHH*
|
||||
|
||||
Please check [6-0-stable](https://github.com/rails/rails/blob/6-0-stable/activemodel/CHANGELOG.md) for previous changes.
|
||||
|
|
|
@ -84,6 +84,7 @@ module ActiveModel
|
|||
# person.previous_changes # => {"name" => [nil, "Bill"]}
|
||||
# person.name_previously_changed? # => true
|
||||
# person.name_previous_change # => [nil, "Bill"]
|
||||
# person.name_previously_was # => nil
|
||||
# person.reload!
|
||||
# person.previous_changes # => {}
|
||||
#
|
||||
|
@ -122,7 +123,7 @@ module ActiveModel
|
|||
|
||||
included do
|
||||
attribute_method_suffix "_changed?", "_change", "_will_change!", "_was"
|
||||
attribute_method_suffix "_previously_changed?", "_previous_change"
|
||||
attribute_method_suffix "_previously_changed?", "_previous_change", "_previously_was"
|
||||
attribute_method_affix prefix: "restore_", suffix: "!"
|
||||
end
|
||||
|
||||
|
@ -180,6 +181,11 @@ module ActiveModel
|
|||
mutations_before_last_save.changed?(attr_name.to_s)
|
||||
end
|
||||
|
||||
# Dispatch target for <tt>*_previously_was</tt> attribute methods.
|
||||
def attribute_previously_was(attr_name) # :nodoc:
|
||||
mutations_before_last_save.original_value(attr_name.to_s)
|
||||
end
|
||||
|
||||
# Restore all previous data of the provided attributes.
|
||||
def restore_attributes(attr_names = changed)
|
||||
attr_names.each { |attr_name| restore_attribute!(attr_name) }
|
||||
|
|
|
@ -491,6 +491,7 @@ class DirtyTest < ActiveRecord::TestCase
|
|||
|
||||
assert_equal 4, pirate.previous_changes.size
|
||||
assert_equal [nil, "arrr"], pirate.previous_changes["catchphrase"]
|
||||
assert_equal nil, pirate.catchphrase_previously_was
|
||||
assert_equal [nil, pirate.id], pirate.previous_changes["id"]
|
||||
assert_nil pirate.previous_changes["updated_on"][0]
|
||||
assert_not_nil pirate.previous_changes["updated_on"][1]
|
||||
|
@ -507,6 +508,7 @@ class DirtyTest < ActiveRecord::TestCase
|
|||
|
||||
assert_equal 4, pirate.previous_changes.size
|
||||
assert_equal [nil, "arrr"], pirate.previous_changes["catchphrase"]
|
||||
assert_equal nil, pirate.catchphrase_previously_was
|
||||
assert_equal [nil, pirate.id], pirate.previous_changes["id"]
|
||||
assert_includes pirate.previous_changes, "updated_on"
|
||||
assert_includes pirate.previous_changes, "created_on"
|
||||
|
@ -525,6 +527,7 @@ class DirtyTest < ActiveRecord::TestCase
|
|||
|
||||
assert_equal 2, pirate.previous_changes.size
|
||||
assert_equal ["arrr", "Me Maties!"], pirate.previous_changes["catchphrase"]
|
||||
assert_equal "arrr", pirate.catchphrase_previously_was
|
||||
assert_not_nil pirate.previous_changes["updated_on"][0]
|
||||
assert_not_nil pirate.previous_changes["updated_on"][1]
|
||||
assert_not pirate.previous_changes.key?("parrot_id")
|
||||
|
@ -539,6 +542,7 @@ class DirtyTest < ActiveRecord::TestCase
|
|||
|
||||
assert_equal 2, pirate.previous_changes.size
|
||||
assert_equal ["Me Maties!", "Thar She Blows!"], pirate.previous_changes["catchphrase"]
|
||||
assert_equal "Me Maties!", pirate.catchphrase_previously_was
|
||||
assert_not_nil pirate.previous_changes["updated_on"][0]
|
||||
assert_not_nil pirate.previous_changes["updated_on"][1]
|
||||
assert_not pirate.previous_changes.key?("parrot_id")
|
||||
|
@ -551,6 +555,7 @@ class DirtyTest < ActiveRecord::TestCase
|
|||
|
||||
assert_equal 2, pirate.previous_changes.size
|
||||
assert_equal ["Thar She Blows!", "Ahoy!"], pirate.previous_changes["catchphrase"]
|
||||
assert_equal "Thar She Blows!", pirate.catchphrase_previously_was
|
||||
assert_not_nil pirate.previous_changes["updated_on"][0]
|
||||
assert_not_nil pirate.previous_changes["updated_on"][1]
|
||||
assert_not pirate.previous_changes.key?("parrot_id")
|
||||
|
@ -563,6 +568,7 @@ class DirtyTest < ActiveRecord::TestCase
|
|||
|
||||
assert_equal 2, pirate.previous_changes.size
|
||||
assert_equal ["Ahoy!", "Ninjas suck!"], pirate.previous_changes["catchphrase"]
|
||||
assert_equal "Ahoy!", pirate.catchphrase_previously_was
|
||||
assert_not_nil pirate.previous_changes["updated_on"][0]
|
||||
assert_not_nil pirate.previous_changes["updated_on"][1]
|
||||
assert_not pirate.previous_changes.key?("parrot_id")
|
||||
|
|
Loading…
Reference in a new issue