-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
Changes from 6 commits
543c59d
f78d828
a351bd0
11bd86a
56fc83a
c84cdca
48512b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,18 +82,16 @@ module ActiveModel | |
# validates :title, presence: true | ||
# end | ||
# | ||
# Validator may also define a +setup+ instance method which will get called | ||
# with the class that using that validator as its argument. This can be | ||
# useful when there are prerequisites such as an +attr_accessor+ being present. | ||
# Validator may also define a +setup!+ instance method which will get called | ||
# automatically in the constructor. It can be useful to access the class that is | ||
# using that validator when there are prerequisites such as an +attr_accessor+ being present. | ||
# This class is accessable via +@klass+. | ||
# | ||
# class MyValidator < ActiveModel::Validator | ||
# def setup(klass) | ||
# klass.send :attr_accessor, :custom_attribute | ||
# def setup! | ||
# @klass.send :attr_accessor, :custom_attribute | ||
# end | ||
# end | ||
# | ||
# This setup method is only called when used with validation macros or the | ||
# class level <tt>validates_with</tt> method. | ||
class Validator | ||
attr_reader :options | ||
|
||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. I don't see the need for always storing the |
||
@options = options.except(:class).freeze | ||
deprecated_setup or setup! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if we stil have Rails guidelines around but part of them was about never using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, can't we just let developers override initialize? Why still call it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had this in an earlier version, for me it felt ok but I wasn't sure if you guys like the additional call. So, override def initialize(options)
super
@klass = options[:class] |
||
end | ||
|
||
# Return the kind for this validator. | ||
|
@@ -123,6 +123,27 @@ def kind | |
def validate(record) | ||
raise NotImplementedError, "Subclasses must implement a validate(record) method." | ||
end | ||
|
||
private | ||
# Override this private method to setup the validator. | ||
def setup! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be indented one level mode than the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should change the documentation of this method on the Validator class |
||
end | ||
|
||
def deprecated_setup # 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 commentThe 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? |
||
class MyValidator < ActiveModel::Validator | ||
private | ||
|
||
def setup! | ||
# you have access to @klass here: | ||
prepare_something_with(@klass) | ||
end | ||
end | ||
" | ||
setup(@klass) | ||
end | ||
end | ||
|
||
# +EachValidator+ is a validator which iterates through the attributes given | ||
|
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.
Should not this method be private now?