diff options
author | William Myers <griffin.myers@gmail.com> | 2013-01-07 00:04:35 -0500 |
---|---|---|
committer | William Myers <griffin.myers@gmail.com> | 2013-05-27 23:41:26 -0400 |
commit | 0e655873d4f41922fa2614919342482b1dbd2343 (patch) | |
tree | 40556ae1d63ee53e093db4d8c2606d79b6ecff24 /activemodel | |
parent | 19b52d3f81080d8eacb78c94bd5957ef7c637d07 (diff) | |
download | rails-0e655873d4f41922fa2614919342482b1dbd2343.tar.gz rails-0e655873d4f41922fa2614919342482b1dbd2343.tar.bz2 rails-0e655873d4f41922fa2614919342482b1dbd2343.zip |
DirtyModel uses a hash to keep track of any changes made to attributes
of an instance. When using the attribute_will_change! method, you must
supply a string and not a symbol or the *_changed? method will break
(because it is looking for the attribute name as a string in the keys
of the underlying hash). To remedy this, I simply made the underlying
hash a HashWithIndifferentAccess so it won't matter if you supply
the attribute name as a symbol or string to attribute_will_change!.
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activemodel/lib/active_model/dirty.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/dirty_test.rb | 17 |
3 files changed, 22 insertions, 2 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 09e6ede064..5cba1959f5 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,5 +1,10 @@ ## Rails 4.0.0 (unreleased) ## +* Updated the DirtyModel *_changed? method to be indifferent between using + symbols and strings as keys. + + *William Myers* + * Add `ActiveModel::Validations::AbsenceValidator`, a validator to check the absence of attributes. diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 6e67cd2285..b89b98feed 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -139,7 +139,7 @@ module ActiveModel # person.name = 'robert' # person.changed_attributes # => {"name" => "bob"} def changed_attributes - @changed_attributes ||= {} + @changed_attributes ||= ActiveSupport::HashWithIndifferentAccess.new end private diff --git a/activemodel/test/cases/dirty_test.rb b/activemodel/test/cases/dirty_test.rb index 0b9f9537e2..78cbf0d62d 100644 --- a/activemodel/test/cases/dirty_test.rb +++ b/activemodel/test/cases/dirty_test.rb @@ -3,11 +3,12 @@ require "cases/helper" 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 @@ class DirtyTest < ActiveModel::TestCase @color = val end + def size + @size + end + + def size=(val) + attribute_will_change!(:size) unless val == @size + @size = val + end + def save @previously_changed = changes @changed_attributes.clear @@ -114,4 +124,9 @@ class DirtyTest < ActiveModel::TestCase 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 |