Skip to content

Fixed the repair_validations helper method. #9363

Merged
merged 1 commit into from Mar 19, 2013

4 participants

@wangjohn

The repair_validations helper was not working correctly before because it only cleared the validations that created :validate callbacks. This didn't include the validations created by validates_with, so I've added a method to clear these validations as well.

The validates_with associations are all associations that are created using: validates_uniqueness_of(), validates_presence_of(), etc. and other custom validations.

Thankfully, the fixing the repair infrastructure didn't make any of the tests fail.

@carlosantoniodasilva carlosantoniodasilva and 1 other commented on an outdated diff Feb 21, 2013
activemodel/lib/active_model/validations.rb
+ #
+ # Person.validators
+ # # => [
+ # # #<MyValidator:0x007fbff403e808 @options={}>,
+ # # #<OtherValidator:0x007fbff403d930 @options={on: :create}>,
+ # # #<StrictValidator:0x007fbff3204a30 @options={strict:true}>
+ # # ]
+ #
+ # If one runs Person.clear_validators! and then checks to see what
+ # validators this class has, you would obtain:
+ #
+ # Person.validators # => []
+ #
+ def clear_validators!
+ _validators.clear
+ end
@carlosantoniodasilva
Ruby on Rails member

Perhaps we should make this method do both? So calling it would actually clear all validators from the class?

@wangjohn
wangjohn added a note Feb 21, 2013

Good point, I'll make that change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@wangjohn

@carlosantoniodasilva I've changed the clear_validators! method so that it also removes any validate callbacks as well as clearing the _validator hash.

@senny senny and 1 other commented on an outdated diff Feb 22, 2013
activemodel/lib/active_model/validations.rb
@@ -169,6 +169,38 @@ def validators
_validators.values.flatten.uniq
end
+ # Removes all of the validators that are being used to validate the
+ # model for both the +validates_with+ and +validate+ methods. It
+ # clears the validators that are created with an invocation of
+ # +validates_with+ and the callbacks that are set by an invocation
+ # of +validate+.
@senny
Ruby on Rails member
senny added a note Feb 22, 2013

The description confuses me somehow. Are there validators that are not cleared? Looking at the example it feels like it just "clears the validators".

@wangjohn
wangjohn added a note Feb 22, 2013

@senny This does clear all the validators, but I'm explaining that it clears the validators that are held in the _validators hash, and I'm also removing the validate callbacks. These are different things because when you do something like

class Comment
   validates_uniqueness_of :foo
end

In the above code, you internally call the validates_with method, which adds a validator to the _validator hash. However, if you do something like

class Comment
  include ActiveModel::Validations

  validate :must_be_friends

  def must_be_friends
    errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee)
  end
end

Then you set a callback (essentially the validate(*args, &block) method calls set_callback(:validate, *args, &block). So I'm just trying to be clear that this method removes both of these types of validations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@senny
Ruby on Rails member
senny commented Feb 22, 2013

We would also need a CHANGELOG entry.

@wangjohn

@senny I've made it more clear in the changelog entry and in the comments themselves what clear_validators! does. Yes, you're right, it's clearing all validations on a model, but I added some comments to show that it clears both the _validator hash which is updated when new Validator objects are created, and resets the :validate callbacks.

@senny
Ruby on Rails member
senny commented Feb 23, 2013

@wangjohn 👍 new comments make it much more clear whats happening.

@wangjohn wangjohn The repair_validations helper was not working correctly before because
it only cleared the validations that created :validate callbacks. This
didn't include the validates created by validates_with, so I've added a
method to clear all validations.
3ee30ca
@tenderlove tenderlove merged commit a46cda1 into rails:master Mar 19, 2013
@wangjohn wangjohn deleted the wangjohn:fix_repair_validations branch Mar 19, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.