diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2012-01-17 00:45:24 -0800 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2012-01-17 00:45:24 -0800 |
commit | ed35f3747c2acfeac140e6d8a44bc0e491fca1a3 (patch) | |
tree | 034aa5ce3525bcbd223233f467973a998c63a2f0 /activemodel | |
parent | f9275e54e0a9462ccbf8d19639232c5cd0fe1348 (diff) | |
parent | 7021184f9cd385bc7f7c7bedf6ce871806b1981e (diff) | |
download | rails-ed35f3747c2acfeac140e6d8a44bc0e491fca1a3.tar.gz rails-ed35f3747c2acfeac140e6d8a44bc0e491fca1a3.tar.bz2 rails-ed35f3747c2acfeac140e6d8a44bc0e491fca1a3.zip |
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
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 15 | ||||
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 8 |
2 files changed, 22 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 3d814b4870..80621cd7da 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -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. diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 8361069819..e2dea12589 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -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] |