Dup validation 3 2 #6324

Merged
merged 1 commit into from May 15, 2012
View
6 activemodel/lib/active_model/validations.rb
@@ -165,6 +165,12 @@ def inherited(base)
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
15 activemodel/test/cases/validations_test.rb
@@ -339,4 +339,19 @@ def test_strict_validation_error_message
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