Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Dup validation 3 2 #6324

Merged
merged 1 commit into from

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

@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
Commits on May 15, 2012
  1. Angelo Capilleri

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

    acapilleri authored
    …ed,for 3-2-stable
    
    It Fixes #5953 in 3-2-stable, it's the same pull request of #6284
This page is out of date. Refresh to see the latest.
6 activemodel/lib/active_model/validations.rb
View
@@ -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)
15 activemodel/test/cases/validations_test.rb
View
@@ -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
Something went wrong with that request. Please try again.