From a7db02e9f2d56e3cd440ea901c47b202b291397a Mon Sep 17 00:00:00 2001 From: Jacob Atzen Date: Fri, 12 Mar 2010 10:22:29 +0100 Subject: [PATCH] ActiveModel::Dirty#changes should return a HashWithIndifferentAccess --- activemodel/lib/active_model/dirty.rb | 4 +++- activemodel/test/cases/dirty_test.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletions(-) create mode 100644 activemodel/test/cases/dirty_test.rb diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 5f02929..3eafb1c 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -107,7 +107,9 @@ module ActiveModel # person.name = 'bob' # person.changes # => { 'name' => ['bill', 'bob'] } def changes - changed.inject({}) { |h, attr| h[attr] = attribute_change(attr); h } + changed.inject(HashWithIndifferentAccess.new) do |h, attr| + h.merge(attr => attribute_change(attr)) + end end # Map of attributes that were changed when the model was saved. diff --git a/activemodel/test/cases/dirty_test.rb b/activemodel/test/cases/dirty_test.rb new file mode 100644 index 0000000..c910cb4 --- /dev/null +++ b/activemodel/test/cases/dirty_test.rb @@ -0,0 +1,29 @@ +require "cases/helper" + +class DirtyTest < ActiveModel::TestCase + class DirtyModel + include ActiveModel::Dirty + define_attribute_methods [:name] + + def initialize + @name = nil + end + + def name + @name + end + + def name=(val) + name_will_change! + @name = val + end + end + + test "changes accessible through both strings and symbols" do + model = DirtyModel.new + model.name = "David" + assert !model.changes[:name].nil? + assert !model.changes['name'].nil? + end + +end -- 1.6.6