Skip to content
Browse files

Merge pull request #8791 from griffinmyers/master

Updated DirtyModel's @changed_attributes hash to be symbol/string agnostic

Conflicts:
	activemodel/CHANGELOG.md
  • Loading branch information...
2 parents 2dabee2 + 0e65587 commit c48c111bb2aa13d72f5ae6cf7d51a921766c7dfe @rafaelfranca rafaelfranca committed Oct 3, 2013
Showing with 22 additions and 2 deletions.
  1. +5 −0 activemodel/CHANGELOG.md
  2. +1 −1 activemodel/lib/active_model/dirty.rb
  3. +16 −1 activemodel/test/cases/dirty_test.rb
View
5 activemodel/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Updated the `ActiveModel::Dirty#changed_attributes` method to be indifferent between using
+ symbols and strings as keys.
+
+ *William Myers*
+
* Added new API methods `reset_changes` and `changed_applied` to `ActiveModel::Dirty`
that control changes state. Previsously you needed to update internal
instance variables, but now API methods are available.
View
2 activemodel/lib/active_model/dirty.rb
@@ -145,7 +145,7 @@ def previous_changes
# person.name = 'robert'
# person.changed_attributes # => {"name" => "bob"}
def changed_attributes
- @changed_attributes ||= {}
+ @changed_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
end
# Handle <tt>*_changed?</tt> for +method_missing+.
View
17 activemodel/test/cases/dirty_test.rb
@@ -3,11 +3,12 @@
class DirtyTest < ActiveModel::TestCase
class DirtyModel
include ActiveModel::Dirty
- define_attribute_methods :name, :color
+ define_attribute_methods :name, :color, :size
def initialize
@name = nil
@color = nil
+ @size = nil
end
def name
@@ -28,6 +29,15 @@ def color=(val)
@color = val
end
+ def size
+ @size
+ end
+
+ def size=(val)
+ attribute_will_change!(:size) unless val == @size
+ @size = val
+ end
+
def save
changes_applied
end
@@ -124,4 +134,9 @@ def save
assert_equal ["Otto", "Mr. Manfredgensonton"], @model.name_change
assert_equal @model.name_was, "Otto"
end
+
+ test "using attribute_will_change! with a symbol" do
+ @model.size = 1
+ assert @model.size_changed?
+ end
end

0 comments on commit c48c111

Please sign in to comment.
Something went wrong with that request. Please try again.