diff options
author | Nick Sutterer <apotonick@gmail.com> | 2013-05-13 13:59:28 +1000 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-05-23 10:00:44 -0300 |
commit | 7d84c3a2f7ede0e8d04540e9c0640de7378e9b3a (patch) | |
tree | ef7f47142b07726fc2c7a954d400a9c1ef7558e5 /activemodel/lib/active_model/validations | |
parent | 30d28b19584783218e842ce2fd7bfe2bc1dccf66 (diff) | |
download | rails-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/validations')
-rw-r--r-- | activemodel/lib/active_model/validations/acceptance.rb | 4 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations/confirmation.rb | 8 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations/with.rb | 3 |
3 files changed, 12 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/validations/acceptance.rb b/activemodel/lib/active_model/validations/acceptance.rb index 78e6f67a47..139de16326 100644 --- a/activemodel/lib/active_model/validations/acceptance.rb +++ b/activemodel/lib/active_model/validations/acceptance.rb @@ -4,6 +4,7 @@ module ActiveModel class AcceptanceValidator < EachValidator # :nodoc: def initialize(options) super({ allow_nil: true, accept: "1" }.merge!(options)) + setup!(options[:class]) end def validate_each(record, attribute, value) @@ -12,7 +13,8 @@ module ActiveModel end end - def setup(klass) + private + def setup!(klass) attr_readers = attributes.reject { |name| klass.attribute_method?(name) } attr_writers = attributes.reject { |name| klass.attribute_method?("#{name}=") } klass.send(:attr_reader, *attr_readers) diff --git a/activemodel/lib/active_model/validations/confirmation.rb b/activemodel/lib/active_model/validations/confirmation.rb index 1d85378892..b0542661af 100644 --- a/activemodel/lib/active_model/validations/confirmation.rb +++ b/activemodel/lib/active_model/validations/confirmation.rb @@ -2,6 +2,11 @@ module ActiveModel module Validations class ConfirmationValidator < EachValidator # :nodoc: + def initialize(options) + super + setup!(options[:class]) + end + def validate_each(record, attribute, value) if (confirmed = record.send("#{attribute}_confirmation")) && (value != confirmed) human_attribute_name = record.class.human_attribute_name(attribute) @@ -9,7 +14,8 @@ module ActiveModel end end - def setup(klass) + private + def setup!(klass) klass.send(:attr_reader, *attributes.map do |attribute| :"#{attribute}_confirmation" unless klass.method_defined?(:"#{attribute}_confirmation") end.compact) diff --git a/activemodel/lib/active_model/validations/with.rb b/activemodel/lib/active_model/validations/with.rb index 2ae335d0f4..16bd6670d1 100644 --- a/activemodel/lib/active_model/validations/with.rb +++ b/activemodel/lib/active_model/validations/with.rb @@ -83,9 +83,10 @@ module ActiveModel # end def validates_with(*args, &block) options = args.extract_options! + options[:class] = self + args.each do |klass| validator = klass.new(options, &block) - validator.setup(self) if validator.respond_to?(:setup) if validator.respond_to?(:attributes) && !validator.attributes.empty? validator.attributes.each do |attribute| |