Permalink
Browse files

clean the erros if an object that includes validations errors is dupe…

…d. Fixes #5953
  • Loading branch information...
1 parent 98355fe commit f9ae1baa0ae47064d5100e48ffda1fe8b4fa34e7 Angelo Capilleri committed May 12, 2012
Showing with 21 additions and 0 deletions.
  1. +6 −0 activemodel/lib/active_model/validations.rb
  2. +15 −0 activemodel/test/cases/validations_test.rb
View
6 activemodel/lib/active_model/validations.rb
@@ -177,6 +177,12 @@ def inherited(base)
super
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
View
15 activemodel/test/cases/validations_test.rb
@@ -344,4 +344,19 @@ def test_does_not_modify_options_argument
Topic.validates :title, options
assert_equal({ :presence => true }, options)
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

0 comments on commit f9ae1ba

Please sign in to comment.