-
Notifications
You must be signed in to change notification settings - Fork 21.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove explicit respond_to? and Validator#setup call #10716
Remove explicit respond_to? and Validator#setup call #10716
Conversation
…. instead, respective validators take care of their setup.
Could you give us example of what you want to accomplish with this change? Maybe an example how was the validator subclass before and after it. |
This is the "bottom line" of my PR: apotonick@543c59d#L2L88 I could get my stuff working using the existing code. However, my main intend is to remove the Are you worried about the change? Is it because the options contain a |
@@ -123,6 +125,10 @@ def kind | |||
def validate(record) | |||
raise NotImplementedError, "Subclasses must implement a validate(record) method." | |||
end | |||
|
|||
private |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be in the same indentation of the def
.
No. I just want to know more about the motivation of this pull request.
It is awkward, but it is fine to me.
I'm not sure of this. |
You're right! def setup(*)
deprecation_warning
setup!
end
private
def setup!
end |
👍 |
What is the prefered way of deprecating (and testing this)? Sorry to ask :-) |
Maybe: if validator.respond_to?(:setup)
ActiveSupport::Deprecation.warn 'The `setup` instance method is deprecated and will be removed on Rails 4.2. Define `setup!` without arguments instead.'
validator.setup(self)
end You can even provide a code snippet in the deprecation message to make explicit what the users have to do:
To test you can define a Validator class with the def test_setup_is_deprecated
assert_deprecated do
Class.new do
include ActiveModel::Validations
validates_with MyDeprecatedValidator
end
end
end |
Now the last quest: Why is @rafaelfranca I wanted to say I really appreciate your responsiveness - amazing! 😸 |
@@ -107,7 +105,9 @@ def self.kind | |||
|
|||
# Accepts options that will be made available through the +options+ reader. | |||
def initialize(options = {}) | |||
@options = options.freeze | |||
@klass = options[:class] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the need for always storing the @klass
if we only need it in some validators and temporarily.
I have added some comments, :+1 for getting rid of the |
Cool! I removed the automatic |
def deprecated_setup(options) # TODO: remove me in 4.2. | ||
return unless respond_to?(:setup) | ||
ActiveSupport::Deprecation.warn "The `Validator#setup` instance method is deprecated and will be removed on Rails 4.2. Change your `setup` method to something like: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@apotonick should not the deprecation warning be updated to show the initialize override example?
I like it too. Just a minor comment on the deprecation message. |
Since it is a user facing change we will need a CHANGELOG entry, and make sure your commits are squashed. |
Dunno how to change a PR to another branch, here it goes: https://github.com/apotonick/rails/tree/deprecate-validator-setup rebased to current rails/rails:master and including updated exception and CHANGELOG entry. |
@apotonick merged. Thank you for contributing. |
Thank you @josevalim and especially @rafaelfranca for that quick and uncomplicated feedback and processing! ❤️ |
This moves the setup process of validators into the constructor, pushing all knowledge about this into the validator class itself. Makes it easier to override internals in subclassed validators.