Permalink
Browse files

Fix ActiveModel::Errors#dup

Since ActiveModel::Errors instance keeps all error messages as hash
we should duplicate this object as well.

Previously ActiveModel::Errors was a subclass of ActiveSupport::OrderedHash,
which results in different behavior on `dup`, this may result in regression for
people relying on it.
  • Loading branch information...
1 parent 21afd9b commit 5313eab6954aff1235aca9126d662aac5b6c300d @pkondzior pkondzior committed Jan 17, 2012
Showing with 13 additions and 0 deletions.
  1. +5 −0 activemodel/lib/active_model/errors.rb
  2. +8 −0 activemodel/test/cases/errors_test.rb
@@ -78,6 +78,11 @@ def initialize(base)
@messages = ActiveSupport::OrderedHash.new
end
+ def initialize_dup(other)
+ @messages = other.messages.dup
+ super
+ end
+
# Clear the messages
def clear
messages.clear
@@ -40,6 +40,14 @@ def test_include?
assert errors.include?(:foo), 'errors should include :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
+
def test_has_key?
errors = ActiveModel::Errors.new(self)
errors[:foo] = 'omg'

0 comments on commit 5313eab

Please sign in to comment.