From 44cd9e0e7132abe632664377f13f3edd1106685a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 23 Dec 2009 12:14:00 +0100 Subject: ActiveRecord::Validations are now built on top of Validator as well. --- activemodel/lib/active_model/validations/acceptance.rb | 7 +++++-- activemodel/lib/active_model/validations/length.rb | 3 +-- activemodel/lib/active_model/validations/numericality.rb | 7 +++++-- 3 files changed, 11 insertions(+), 6 deletions(-) (limited to 'activemodel/lib/active_model/validations') diff --git a/activemodel/lib/active_model/validations/acceptance.rb b/activemodel/lib/active_model/validations/acceptance.rb index a5de58cd41..bd9463ed27 100644 --- a/activemodel/lib/active_model/validations/acceptance.rb +++ b/activemodel/lib/active_model/validations/acceptance.rb @@ -1,6 +1,10 @@ module ActiveModel module Validations class AcceptanceValidator < EachValidator + def initialize(options) + super(options.reverse_merge(:allow_nil => true, :accept => "1")) + end + def validate_each(record, attribute, value) unless value == options[:accept] record.errors.add(attribute, :accepted, :default => options[:message]) @@ -33,8 +37,7 @@ module ActiveModel # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The # method, proc or string should return or evaluate to a true or false value. def validates_acceptance_of(*attr_names) - options = { :allow_nil => true, :accept => "1" } - options.update(attr_names.extract_options!) + options = attr_names.extract_options! db_cols = begin column_names diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb index 04280b401b..6e90a75c17 100644 --- a/activemodel/lib/active_model/validations/length.rb +++ b/activemodel/lib/active_model/validations/length.rb @@ -9,9 +9,8 @@ module ActiveModel attr_reader :type def initialize(options) - options[:tokenizer] ||= DEFAULT_TOKENIZER @type = (OPTIONS & options.keys).first - super + super(options.reverse_merge(:tokenizer => DEFAULT_TOKENIZER)) end def check_validity! diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index 914a3133cf..f2aab8c5b8 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -5,6 +5,10 @@ module ActiveModel :equal_to => :==, :less_than => :<, :less_than_or_equal_to => :<=, :odd => :odd?, :even => :even? }.freeze + def initialize(options) + super(options.reverse_merge(:only_integer => false, :allow_nil => false)) + end + def check_validity! options.slice(*CHECKS.keys) do |option, value| next if [:odd, :even].include?(option) @@ -99,8 +103,7 @@ module ActiveModel # end # def validates_numericality_of(*attr_names) - options = { :only_integer => false, :allow_nil => false } - options.update(attr_names.extract_options!) + options = attr_names.extract_options! validates_with NumericalityValidator, options.merge(:attributes => attr_names) end end -- cgit v1.2.3