From a0bb19fbfa590a3a773a24bc018b92fd5ba93b4d Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 1 Aug 2019 15:38:03 -0700 Subject: Add *_previously_was attribute methods when dirty tracking (#36836) --- activemodel/lib/active_model/dirty.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'activemodel/lib/active_model/dirty.rb') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index aaefe00c83..245616502d 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -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 *_previously_was 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) } -- cgit v1.2.3