mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Move validate_on_create and validate_on_update from ActiveModel to ActiveRecord
This commit is contained in:
parent
6173e5bfae
commit
22ad30ed60
9 changed files with 21 additions and 20 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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" }))
|
||||
|
||||
|
|
|
@ -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]
|
||||
|
||||
|
|
|
@ -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]
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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?
|
||||
|
|
Loading…
Reference in a new issue