mirror of https://github.com/rails/rails
Merge pull request #8791 from griffinmyers/master
Updated DirtyModel's @changed_attributes hash to be symbol/string agnostic Conflicts: activemodel/CHANGELOG.md
This commit is contained in:
commit
c48c111bb2
|
@ -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`
|
* Added new API methods `reset_changes` and `changed_applied` to `ActiveModel::Dirty`
|
||||||
that control changes state. Previsously you needed to update internal
|
that control changes state. Previsously you needed to update internal
|
||||||
instance variables, but now API methods are available.
|
instance variables, but now API methods are available.
|
||||||
|
|
|
@ -145,7 +145,7 @@ module ActiveModel
|
||||||
# person.name = 'robert'
|
# person.name = 'robert'
|
||||||
# person.changed_attributes # => {"name" => "bob"}
|
# person.changed_attributes # => {"name" => "bob"}
|
||||||
def changed_attributes
|
def changed_attributes
|
||||||
@changed_attributes ||= {}
|
@changed_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
|
||||||
end
|
end
|
||||||
|
|
||||||
# Handle <tt>*_changed?</tt> for +method_missing+.
|
# Handle <tt>*_changed?</tt> for +method_missing+.
|
||||||
|
|
|
@ -3,11 +3,12 @@ require "cases/helper"
|
||||||
class DirtyTest < ActiveModel::TestCase
|
class DirtyTest < ActiveModel::TestCase
|
||||||
class DirtyModel
|
class DirtyModel
|
||||||
include ActiveModel::Dirty
|
include ActiveModel::Dirty
|
||||||
define_attribute_methods :name, :color
|
define_attribute_methods :name, :color, :size
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@name = nil
|
@name = nil
|
||||||
@color = nil
|
@color = nil
|
||||||
|
@size = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def name
|
def name
|
||||||
|
@ -28,6 +29,15 @@ class DirtyTest < ActiveModel::TestCase
|
||||||
@color = val
|
@color = val
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def size
|
||||||
|
@size
|
||||||
|
end
|
||||||
|
|
||||||
|
def size=(val)
|
||||||
|
attribute_will_change!(:size) unless val == @size
|
||||||
|
@size = val
|
||||||
|
end
|
||||||
|
|
||||||
def save
|
def save
|
||||||
changes_applied
|
changes_applied
|
||||||
end
|
end
|
||||||
|
@ -124,4 +134,9 @@ class DirtyTest < ActiveModel::TestCase
|
||||||
assert_equal ["Otto", "Mr. Manfredgensonton"], @model.name_change
|
assert_equal ["Otto", "Mr. Manfredgensonton"], @model.name_change
|
||||||
assert_equal @model.name_was, "Otto"
|
assert_equal @model.name_was, "Otto"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "using attribute_will_change! with a symbol" do
|
||||||
|
@model.size = 1
|
||||||
|
assert @model.size_changed?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue