Browse files

Restore changed_attributes method in ActiveModel::Dirty and loosen ex…

…pectation on including class' initialize method.

[#4308 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
1 parent dc5300a commit 3adaef8ae73a3061a9fe4c5e0256d80bc09b1cf4 @spohlenz spohlenz committed with jeremy Apr 1, 2010
Showing with 12 additions and 13 deletions.
  1. +12 −12 activemodel/lib/active_model/dirty.rb
  2. +0 −1 activemodel/test/cases/dirty_test.rb
View
24 activemodel/lib/active_model/dirty.rb
@@ -91,25 +91,20 @@ module Dirty
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].
@@ -130,19 +125,24 @@ def previous_changes
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+.
@@ -153,12 +153,12 @@ def attribute_will_change!(attr)
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
View
1 activemodel/test/cases/dirty_test.rb
@@ -6,7 +6,6 @@ class DirtyModel
define_attribute_methods [:name]
def initialize
- super
@name = nil
end

1 comment on commit 3adaef8

@jpartogi

The changes method still does not return the value as expected. It still returns empty Hash.

Please sign in to comment.