aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/validator.rb
diff options
context:
space:
mode:
authorNick Sutterer <apotonick@gmail.com>2013-05-13 13:59:28 +1000
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-05-23 10:00:44 -0300
commit7d84c3a2f7ede0e8d04540e9c0640de7378e9b3a (patch)
treeef7f47142b07726fc2c7a954d400a9c1ef7558e5 /activemodel/lib/active_model/validator.rb
parent30d28b19584783218e842ce2fd7bfe2bc1dccf66 (diff)
downloadrails-7d84c3a2f7ede0e8d04540e9c0640de7378e9b3a.tar.gz
rails-7d84c3a2f7ede0e8d04540e9c0640de7378e9b3a.tar.bz2
rails-7d84c3a2f7ede0e8d04540e9c0640de7378e9b3a.zip
deprecate Validator#setup (to get rid of a respond_to call). validators do their setup in their constructor now.
Diffstat (limited to 'activemodel/lib/active_model/validator.rb')
-rw-r--r--activemodel/lib/active_model/validator.rb32
1 files changed, 23 insertions, 9 deletions
diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb
index 037650e5ac..690856aee1 100644
--- a/activemodel/lib/active_model/validator.rb
+++ b/activemodel/lib/active_model/validator.rb
@@ -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.
+ # 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 +options[:class]+ in the constructor.
+ # To setup your validator override the constructor.
#
# class MyValidator < ActiveModel::Validator
- # def setup(klass)
- # klass.send :attr_accessor, :custom_attribute
+ # def initialize(options={})
+ # super
+ # options[:class].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,8 @@ module ActiveModel
# Accepts options that will be made available through the +options+ reader.
def initialize(options = {})
- @options = options.freeze
+ @options = options.except(:class).freeze
+ deprecated_setup(options)
end
# Return the kind for this validator.
@@ -123,6 +122,21 @@ module ActiveModel
def validate(record)
raise NotImplementedError, "Subclasses must implement a validate(record) method."
end
+
+ private
+ 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. Do your setup in the constructor instead:
+
+class MyValidator < ActiveModel::Validator
+ def initialize(options={})
+ super
+ options[:class].send :attr_accessor, :custom_attribute
+ end
+end
+"
+ setup(options[:class])
+ end
end
# +EachValidator+ is a validator which iterates through the attributes given