Permalink
Browse files

clean the errors if an object that includes validations errors is dup…

…ed,for 3-2-stable

It Fixes #5953 in 3-2-stable, it's the same pull request of #6284
  • Loading branch information...
acapilleri committed May 15, 2012
1 parent 007539d commit 396e383286ccc368b54e980e06a506d339843594
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
@@ -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)
@@ -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

0 comments on commit 396e383

Please sign in to comment.