From 93eaaef43c5614a61bde3eace9a539b5703f3168 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Tue, 28 Aug 2007 23:16:49 +0000 Subject: Use extract_options instead of ad-hoc partial implementations. [norbert] Closes #9313 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7364 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/action_controller/session_management.rb | 2 +- activerecord/lib/active_record/validations.rb | 23 +++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/actionpack/lib/action_controller/session_management.rb b/actionpack/lib/action_controller/session_management.rb index 5b39118001..fdf75b1b94 100644 --- a/actionpack/lib/action_controller/session_management.rb +++ b/actionpack/lib/action_controller/session_management.rb @@ -69,7 +69,7 @@ module ActionController #:nodoc: # All session options described for ActionController::Base.process_cgi # are valid arguments. def session(*args) - options = Hash === args.last ? args.pop : {} + options = args.extract_options! options[:disabled] = true if !args.empty? options[:only] = [*options[:only]].map { |o| o.to_s } if options[:only] diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index adf6c290c7..dcc8caa84d 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -366,7 +366,7 @@ module ActiveRecord # 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_each(*attrs) - options = attrs.last.is_a?(Hash) ? attrs.pop.symbolize_keys : {} + options = attrs.extract_options!.symbolize_keys attrs = attrs.flatten # Declare the validation. @@ -409,7 +409,7 @@ module ActiveRecord # method, proc or string should return or evaluate to a true or false value. def validates_confirmation_of(*attr_names) configuration = { :message => ActiveRecord::Errors.default_error_messages[:confirmation], :on => :save } - configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) + configuration.update(attr_names.extract_options!) attr_accessor *(attr_names.map { |n| "#{n}_confirmation" }) @@ -442,7 +442,7 @@ module ActiveRecord # method, proc or string should return or evaluate to a true or false value. def validates_acceptance_of(*attr_names) configuration = { :message => ActiveRecord::Errors.default_error_messages[:accepted], :on => :save, :allow_nil => true, :accept => "1" } - configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) + configuration.update(attr_names.extract_options!) attr_accessor *attr_names @@ -486,7 +486,7 @@ module ActiveRecord # new. def validates_presence_of(*attr_names) configuration = { :message => ActiveRecord::Errors.default_error_messages[:blank], :on => :save } - configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) + configuration.update(attr_names.extract_options!) # can't use validates_each here, because it cannot cope with nonexistent attributes, # while errors.add_on_empty can @@ -534,7 +534,7 @@ module ActiveRecord :too_short => ActiveRecord::Errors.default_error_messages[:too_short], :wrong_length => ActiveRecord::Errors.default_error_messages[:wrong_length] }.merge(DEFAULT_VALIDATION_OPTIONS) - options.update(attrs.pop.symbolize_keys) if attrs.last.is_a?(Hash) + options.update(attrs.extract_options!.symbolize_keys) # Ensure that one and only one range option is specified. range_options = ALL_RANGE_OPTIONS & options.keys @@ -617,7 +617,7 @@ module ActiveRecord # method, proc or string should return or evaluate to a true or false value. def validates_uniqueness_of(*attr_names) configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken], :case_sensitive => true } - configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) + configuration.update(attr_names.extract_options!) validates_each(attr_names,configuration) do |record, attr_name, value| if value.nil? || (configuration[:case_sensitive] || !columns_hash[attr_name.to_s].text?) @@ -668,7 +668,7 @@ module ActiveRecord # method, proc or string should return or evaluate to a true or false value. def validates_format_of(*attr_names) configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid], :on => :save, :with => nil } - configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) + configuration.update(attr_names.extract_options!) raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp) @@ -696,7 +696,7 @@ module ActiveRecord # method, proc or string should return or evaluate to a true or false value. def validates_inclusion_of(*attr_names) configuration = { :message => ActiveRecord::Errors.default_error_messages[:inclusion], :on => :save } - configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) + configuration.update(attr_names.extract_options!) enum = configuration[:in] || configuration[:within] @@ -726,7 +726,7 @@ module ActiveRecord # method, proc or string should return or evaluate to a true or false value. def validates_exclusion_of(*attr_names) configuration = { :message => ActiveRecord::Errors.default_error_messages[:exclusion], :on => :save } - configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) + configuration.update(attr_names.extract_options!) enum = configuration[:in] || configuration[:within] @@ -769,7 +769,7 @@ module ActiveRecord # method, proc or string should return or evaluate to a true or false value. def validates_associated(*attr_names) configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid], :on => :save } - configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) + configuration.update(attr_names.extract_options!) validates_each(attr_names, configuration) do |record, attr_name, value| record.errors.add(attr_name, configuration[:message]) unless @@ -805,7 +805,8 @@ module ActiveRecord # method, proc or string should return or evaluate to a true or false value. def validates_numericality_of(*attr_names) configuration = { :on => :save, :only_integer => false, :allow_nil => false } - configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) + configuration.update(attr_names.extract_options!) + numericality_options = ALL_NUMERICALITY_CHECKS.keys & configuration.keys -- cgit v1.2.3