Merge pull request #4492 from pkondzior/3-2-stable

Wrong behavior of ActiveModel::Errors#dup is causing regressions on applications using Rails 3-2-stable
This commit is contained in:
Piotr Sarnacki
2012-01-17 00:45:24 -08:00
2 changed files with 22 additions and 1 deletions

View File

@@ -79,6 +79,19 @@ module ActiveModel
@messages = ActiveSupport::OrderedHash.new
end
def initialize_dup(other)
@messages = other.messages.dup
end
# Backport dup from 1.9 so that #initialize_dup gets called
unless Object.respond_to?(:initialize_dup)
def dup # :nodoc:
copy = super
copy.initialize_dup(self)
copy
end
end
# Clear the messages
def clear
messages.clear
@@ -119,7 +132,7 @@ module ActiveModel
# p.errors[:name] = "must be set"
# p.errors[:name] # => ['must be set']
def []=(attribute, error)
self[attribute.to_sym] << error
self[attribute] << error
end
# Iterates through each error key, value pair in the error messages hash.

View File

@@ -46,6 +46,14 @@ class ErrorsTest < ActiveModel::TestCase
assert errors.has_key?(:foo), 'errors should have key :foo'
end
def test_dup
errors = ActiveModel::Errors.new(self)
errors[:foo] = 'bar'
errors_dup = errors.dup
errors_dup[:bar] = 'omg'
assert_not_same errors_dup.messages, errors.messages
end
test "should return true if no errors" do
person = Person.new
person.errors[:foo]