lib/active_model/dirty.rb in activemodel-6.0.6.1 vs lib/active_model/dirty.rb in activemodel-6.1.0.rc1
- old
+ new
@@ -81,11 +81,13 @@
#
# Reset the changes:
#
# person.previous_changes # => {"name" => [nil, "Bill"]}
# person.name_previously_changed? # => true
+ # person.name_previously_changed?(from: nil, to: "Bill") # => true
# person.name_previous_change # => [nil, "Bill"]
+ # person.name_previously_was # => nil
# person.reload!
# person.previous_changes # => {}
#
# Rollback the changes:
#
@@ -120,12 +122,13 @@
extend ActiveSupport::Concern
include ActiveModel::AttributeMethods
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: "!"
+ attribute_method_affix prefix: "clear_", suffix: "_change"
end
def initialize_dup(other) # :nodoc:
super
if self.class.respond_to?(:_default_attributes)
@@ -134,11 +137,11 @@
end
end
@mutations_from_database = nil
end
- # Clears dirty data and moves +changes+ to +previously_changed+ and
+ # Clears dirty data and moves +changes+ to +previous_changes+ and
# +mutations_from_database+ to +mutations_before_last_save+ respectively.
def changes_applied
unless defined?(@attributes)
mutations_from_database.finalize_changes
end
@@ -174,11 +177,16 @@
def attribute_was(attr_name) # :nodoc:
mutations_from_database.original_value(attr_name.to_s)
end
# Dispatch target for <tt>*_previously_changed?</tt> attribute methods.
- def attribute_previously_changed?(attr_name) # :nodoc:
- mutations_before_last_save.changed?(attr_name.to_s)
+ def attribute_previously_changed?(attr_name, **options) # :nodoc:
+ mutations_before_last_save.changed?(attr_name.to_s, **options)
+ 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) }