Skip to content

Commit

Permalink
ActiveModel::Dirty#changes should return a HashWithIndifferentAccess [#…
Browse files Browse the repository at this point in the history
…4157 state:resolved]

Keep the Rails style of inject

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
Jacob Atzen authored and josevalim committed Mar 27, 2010
1 parent 140cc9c commit a5d637d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/dirty.rb
Expand Up @@ -107,7 +107,7 @@ def changed
# person.name = 'bob' # person.name = 'bob'
# person.changes # => { 'name' => ['bill', 'bob'] } # person.changes # => { 'name' => ['bill', 'bob'] }
def changes def changes
changed.inject({}) { |h, attr| h[attr] = attribute_change(attr); h } changed.inject(HashWithIndifferentAccess.new){ |h, attr| h[attr] = attribute_change(attr); h }
end end


# Map of attributes that were changed when the model was saved. # Map of attributes that were changed when the model was saved.
Expand Down
29 changes: 29 additions & 0 deletions 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

0 comments on commit a5d637d

Please sign in to comment.