Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit ed35f3747c2acfeac140e6d8a44bc0e491fca1a3 2 parents f9275e5 + 7021184
@drogus drogus authored
View
15 activemodel/lib/active_model/errors.rb
@@ -79,6 +79,19 @@ def initialize(base)
@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 @@ def [](attribute)
# 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
8 activemodel/test/cases/errors_test.rb
@@ -46,6 +46,14 @@ def test_has_key?
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]
Please sign in to comment.
Something went wrong with that request. Please try again.