lib/active_model/dirty.rb in activemodel-3.0.0.beta2 vs lib/active_model/dirty.rb in activemodel-3.0.0.beta3

- old
+ new

@@ -89,29 +89,24 @@ included do attribute_method_suffix '_changed?', '_change', '_will_change!', '_was' attribute_method_affix :prefix => 'reset_', :suffix => '!' end - def initialize(*) - @changed_attributes = {} - super - end - # Do any attributes have unsaved changes? # person.changed? # => false # person.name = 'bob' # person.changed? # => true def changed? - !@changed_attributes.empty? + !changed_attributes.empty? end # List of attributes with unsaved changes. # person.changed # => [] # person.name = 'bob' # person.changed # => ['name'] def changed - @changed_attributes.keys + changed_attributes.keys end # Map of changed attrs => [original value, new value]. # person.changes # => {} # person.name = 'bob' @@ -128,37 +123,42 @@ def previous_changes @previously_changed end private + # Map of change <tt>attr => original value</tt>. + def changed_attributes + @changed_attributes ||= {} + end + # Handle <tt>*_changed?</tt> for +method_missing+. def attribute_changed?(attr) - @changed_attributes.include?(attr) + changed_attributes.include?(attr) end # Handle <tt>*_change</tt> for +method_missing+. def attribute_change(attr) - [@changed_attributes[attr], __send__(attr)] if attribute_changed?(attr) + [changed_attributes[attr], __send__(attr)] if attribute_changed?(attr) end # Handle <tt>*_was</tt> for +method_missing+. def attribute_was(attr) - attribute_changed?(attr) ? @changed_attributes[attr] : __send__(attr) + attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr) end # Handle <tt>*_will_change!</tt> for +method_missing+. def attribute_will_change!(attr) begin value = __send__(attr) value = value.duplicable? ? value.clone : value rescue TypeError, NoMethodError end - @changed_attributes[attr] = value + changed_attributes[attr] = value end # Handle <tt>reset_*!</tt> for +method_missing+. def reset_attribute!(attr) - __send__("#{attr}=", @changed_attributes[attr]) if attribute_changed?(attr) + __send__("#{attr}=", changed_attributes[attr]) if attribute_changed?(attr) end end end