diff options
9 files changed, 21 insertions, 20 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index c0bbad0396..59e919a364 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -62,7 +62,7 @@ module ActiveModel attrs = attrs.flatten # Declare the validation. - send(validation_method(options[:on] || :save), options) do |record| + send(validation_method(options[:on]), options) do |record| attrs.each do |attr| value = record.get_attribute_value(attr) next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank]) @@ -74,11 +74,7 @@ module ActiveModel private def validation_method(on) - case on - when :save then :validate - when :create then :validate_on_create - when :update then :validate_on_update - end + :validate end end diff --git a/activemodel/lib/active_model/validations/acceptance.rb b/activemodel/lib/active_model/validations/acceptance.rb index db1fd04bb5..0c9ef51726 100644 --- a/activemodel/lib/active_model/validations/acceptance.rb +++ b/activemodel/lib/active_model/validations/acceptance.rb @@ -25,7 +25,7 @@ module ActiveModel # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The # method, proc or string should return or evaluate to a true or false value. def validates_acceptance_of(*attr_names) - configuration = { :on => :save, :allow_nil => true, :accept => "1" } + configuration = { :allow_nil => true, :accept => "1" } configuration.update(attr_names.extract_options!) db_cols = begin diff --git a/activemodel/lib/active_model/validations/confirmation.rb b/activemodel/lib/active_model/validations/confirmation.rb index 5f18da58e3..b9823172f7 100644 --- a/activemodel/lib/active_model/validations/confirmation.rb +++ b/activemodel/lib/active_model/validations/confirmation.rb @@ -30,8 +30,7 @@ module ActiveModel # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The # method, proc or string should return or evaluate to a true or false value. def validates_confirmation_of(*attr_names) - configuration = { :on => :save } - configuration.update(attr_names.extract_options!) + configuration = attr_names.extract_options! attr_accessor(*(attr_names.map { |n| "#{n}_confirmation" })) diff --git a/activemodel/lib/active_model/validations/exclusion.rb b/activemodel/lib/active_model/validations/exclusion.rb index 435ba5cab6..0aa9848ee1 100644 --- a/activemodel/lib/active_model/validations/exclusion.rb +++ b/activemodel/lib/active_model/validations/exclusion.rb @@ -21,8 +21,7 @@ module ActiveModel # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The # method, proc or string should return or evaluate to a true or false value. def validates_exclusion_of(*attr_names) - configuration = { :on => :save } - configuration.update(attr_names.extract_options!) + configuration = attr_names.extract_options! enum = configuration[:in] || configuration[:within] diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb index 0cb8e44770..a4bc8fe035 100644 --- a/activemodel/lib/active_model/validations/inclusion.rb +++ b/activemodel/lib/active_model/validations/inclusion.rb @@ -21,8 +21,7 @@ module ActiveModel # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The # method, proc or string should return or evaluate to a true or false value. def validates_inclusion_of(*attr_names) - configuration = { :on => :save } - configuration.update(attr_names.extract_options!) + configuration = attr_names.extract_options! enum = configuration[:in] || configuration[:within] diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index 99035b8af8..79fca2f1ea 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -32,7 +32,7 @@ module ActiveModel # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The # 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 = { :only_integer => false, :allow_nil => false } configuration.update(attr_names.extract_options!) numericality_options = ALL_NUMERICALITY_CHECKS.keys & configuration.keys diff --git a/activemodel/lib/active_model/validations/presence.rb b/activemodel/lib/active_model/validations/presence.rb index 4fd60621cd..518bc8a952 100644 --- a/activemodel/lib/active_model/validations/presence.rb +++ b/activemodel/lib/active_model/validations/presence.rb @@ -26,8 +26,7 @@ module ActiveModel # The method, proc or string should return or evaluate to a true or false value. # def validates_presence_of(*attr_names) - configuration = { :on => :save } - configuration.update(attr_names.extract_options!) + configuration = attr_names.extract_options! # can't use validates_each here, because it cannot cope with nonexistent attributes, # while errors.add_on_empty can diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index a9e6654aec..9a7b2a47bc 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -99,9 +99,8 @@ module ActiveRecord module Validations def self.included(base) # :nodoc: - base.extend ClassMethods - base.send :include, ActiveModel::Validations + base.extend ClassMethods base.send :include, InstanceMethods base.define_callbacks :validate_on_create, :validate_on_update @@ -125,6 +124,17 @@ module ActiveRecord object end end + + def validation_method(on) + case on + when :create + :validate_on_create + when :update + :validate_on_update + else + :validate + end + end end module InstanceMethods diff --git a/activerecord/lib/active_record/validations/associated.rb b/activerecord/lib/active_record/validations/associated.rb index 1d7df6b771..92f47d770f 100644 --- a/activerecord/lib/active_record/validations/associated.rb +++ b/activerecord/lib/active_record/validations/associated.rb @@ -33,8 +33,7 @@ module ActiveRecord # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The # method, proc or string should return or evaluate to a true or false value. def validates_associated(*attr_names) - configuration = { :on => :save } - configuration.update(attr_names.extract_options!) + configuration = attr_names.extract_options! validates_each(attr_names, configuration) do |record, attr_name, value| unless (value.is_a?(Array) ? value : [value]).collect { |r| r.nil? || r.valid? }.all? |