提交 22ad30ed 编写于 作者: P Pratik Naik

Move validate_on_create and validate_on_update from ActiveModel to ActiveRecord

上级 6173e5bf
......@@ -62,7 +62,7 @@ def validates_each(*attrs)
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 @@ def validates_each(*attrs)
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 ClassMethods
# 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 ClassMethods
# 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 ClassMethods
# 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 ClassMethods
# 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 ClassMethods
# 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 ClassMethods
# 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 @@ def generate_message(attribute, message = :invalid, options = {})
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 @@ def create!(attributes = nil, &block)
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 ClassMethods
# 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?
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册