Skip to content
This repository

Dup validation 3 2 #6324

Merged
merged 1 commit into from about 2 years ago

4 participants

Angelo Capilleri Carlos Antonio da Silva Rafael Mendonça França Rajat Vig
Angelo Capilleri

...for 3-2-stable

It Fixes #5953 the 3-2-stable, it's the same pull request of #6284

Angelo Capilleri acapilleri 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
396e383
Rafael Mendonça França rafaelfranca merged commit 9a868a0 into from
Carlos Antonio da Silva

Great, thanks!

Rafael Mendonça França
Owner

@acapilleri This pull request broke the build for Ruby 1.8.7. Please investigate it. http://travis-ci.org/#!/rails/rails/jobs/1337434

Angelo Capilleri

ok..

Angelo Capilleri

@rafaelfranca, I tested this on my local machine and it goes well

Carlos Antonio da Silva

It can't, initialize_dup does not exist on Ruby 1.8. See this for a backport example from AMo::Errors: https://github.com/rails/rails/pull/4492/files.

Angelo Capilleri acapilleri referenced this pull request from a commit in acapilleri/rails
Angelo Capilleri add backport for the pull request #6324 e885b18
Rajat Vig

The backport fix has broken code on our end when using ActiveModel::Validations inside a Hashie::Dash.

Scenario

class A < Hashie::Dash
  property :a, :default => "b"
end

A.new.to_s
#<A a="b">
A.new.dup.keys
["a"]

class B < Hashie::Dash
  include ActiveModel::Validations
  property :a, :default => "b"
end

B.new.to_s
#<B>
B.new.dup.keys
[]

The fix is to call super at the end of initialized_dup.
We are using Ruby 1.9.3p194. The hashie gem is 1.x.

Angelo Capilleri

thanks.

Angelo Capilleri

yes but this fix not works with 1.8.x , it works with 1.8.x and 1.9.x with something like that :

 def initialize_dup(other) # :nodoc:
      @errors = nil
      super if respond_to?(:super)
 end

cc / @carlosantoniodasilva

Rajat Vig

That'll work

Angelo Capilleri acapilleri referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Angelo Capilleri acapilleri referenced this pull request from a commit in acapilleri/rails
Angelo Capilleri acapilleri Fix the the backport of the object dup with the ruby 1.9.3p194.
At the end of initialize_dup was added the call to super if it existsi,
so it also works with 1.8.7 where initialize_dup doesn't exist.
It was introduced with the pull request #6324
80b8e3f
Angelo Capilleri acapilleri referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Angelo Capilleri acapilleri referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Angelo Capilleri acapilleri referenced this pull request from a commit in acapilleri/rails
Angelo Capilleri acapilleri Fix the the backport of the object dup with the ruby 1.9.3p194.
At the end of initialize_dup was added the call to super if it exists,
so it also works with 1.8.7 where initialize_dup doesn't exist.
This issu was introduced with the pull request #6324
b97e0a1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

May 15, 2012
Angelo Capilleri acapilleri 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
396e383
This page is out of date. Refresh to see the latest.
6 activemodel/lib/active_model/validations.rb
@@ -165,6 +165,12 @@ def inherited(base)
165 165 end
166 166 end
167 167
  168 + # Clean the +Errors+ object if instance is duped
  169 + def initialize_dup(other) # :nodoc:
  170 + @errors = nil
  171 + super
  172 + end
  173 +
168 174 # Returns the +Errors+ object that holds all information about attribute error messages.
169 175 def errors
170 176 @errors ||= Errors.new(self)
15 activemodel/test/cases/validations_test.rb
@@ -339,4 +339,19 @@ def test_strict_validation_error_message
339 339 end
340 340 assert_equal "Title can't be blank", exception.message
341 341 end
  342 +
  343 + def test_dup_validity_is_independent
  344 + Topic.validates_presence_of :title
  345 + topic = Topic.new("title" => "Litterature")
  346 + topic.valid?
  347 +
  348 + duped = topic.dup
  349 + duped.title = nil
  350 + assert duped.invalid?
  351 +
  352 + topic.title = nil
  353 + duped.title = 'Mathematics'
  354 + assert topic.invalid?
  355 + assert duped.valid?
  356 + end
342 357 end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.