diff options
author | Angelo capilleri <capilleri@yahoo.com> | 2012-05-15 18:16:28 +0200 |
---|---|---|
committer | Angelo capilleri <capilleri@yahoo.com> | 2012-05-15 18:20:53 +0200 |
commit | 396e383286ccc368b54e980e06a506d339843594 (patch) | |
tree | 54de3b7bc8724f47537949ef60d995ec1c2af670 /activemodel | |
parent | 007539d12d64764db3f7fd13f134804e233ac99d (diff) | |
download | rails-396e383286ccc368b54e980e06a506d339843594.tar.gz rails-396e383286ccc368b54e980e06a506d339843594.tar.bz2 rails-396e383286ccc368b54e980e06a506d339843594.zip |
clean the errors if an object that includes validations errors is duped,for 3-2-stable
It Fixes #5953 in 3-2-stable, it's the same pull request of #6284
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/validations.rb | 6 | ||||
-rw-r--r-- | activemodel/test/cases/validations_test.rb | 15 |
2 files changed, 21 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 8ed392abca..1d39f2357f 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -165,6 +165,12 @@ module ActiveModel end end + # Clean the +Errors+ object if instance is duped + def initialize_dup(other) # :nodoc: + @errors = nil + super + end + # Returns the +Errors+ object that holds all information about attribute error messages. def errors @errors ||= Errors.new(self) diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index 1941031e3c..56c9a5592c 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -339,4 +339,19 @@ class ValidationsTest < ActiveModel::TestCase end assert_equal "Title can't be blank", exception.message end + + def test_dup_validity_is_independent + Topic.validates_presence_of :title + topic = Topic.new("title" => "Litterature") + topic.valid? + + duped = topic.dup + duped.title = nil + assert duped.invalid? + + topic.title = nil + duped.title = 'Mathematics' + assert topic.invalid? + assert duped.valid? + end end |