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
This commit is contained in:
Angelo capilleri
2012-05-15 18:16:28 +02:00
parent 007539d12d
commit 396e383286
2 changed files with 21 additions and 0 deletions

View File

@@ -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)

View File

@@ -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