Skip to content

Commit

Permalink
Merge pull request rails#6284 from acapilleri/dup_validation
Browse files Browse the repository at this point in the history
clean the erros if an object that includes validation  is duped.
  • Loading branch information
carlosantoniodasilva committed May 15, 2012
2 parents dd42e89 + f9ae1ba commit 3d1b078
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions activemodel/lib/active_model/validations.rb
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions activemodel/test/cases/validations_test.rb
Expand Up @@ -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 3d1b078

Please sign in to comment.