0
@@ -279,6 +279,25 @@ module ActiveRecord
0
alias_method_chain :save!, :validation
0
alias_method_chain :update_attribute, :validation_skipping
0
+ base.send :include, ActiveSupport::Callbacks
0
+ # TODO: Use helper ActiveSupport::Callbacks#define_callbacks instead
0
+ %w( validate validate_on_create validate_on_update ).each do |validation_method|
0
+ base.class_eval <<-"end_eval"
0
+ def self.#{validation_method}(*methods, &block)
0
+ options = methods.extract_options!
0
+ methods << block if block_given?
0
+ methods.map! { |method| Callback.new(:#{validation_method}, method, options) }
0
+ existing_methods = read_inheritable_attribute(:#{validation_method}) || []
0
+ write_inheritable_attribute(:#{validation_method}, existing_methods | methods)
0
+ def self.#{validation_method}_callback_chain
0
+ read_inheritable_attribute(:#{validation_method}) || []
0
# All of the following validations are defined in the class scope of the model that you're interested in validating.
0
@@ -324,43 +343,6 @@ module ActiveRecord
0
# This usage applies to #validate_on_create and #validate_on_update as well.
0
- def validate(*methods, &block)
0
- methods << block if block_given?
0
- write_inheritable_set(:validate, methods)
0
- def validate_on_create(*methods, &block)
0
- methods << block if block_given?
0
- write_inheritable_set(:validate_on_create, methods)
0
- def validate_on_update(*methods, &block)
0
- methods << block if block_given?
0
- write_inheritable_set(:validate_on_update, methods)
0
- def condition_block?(condition)
0
- condition.respond_to?("call") && (condition.arity == 1 || condition.arity == -1)
0
- # Determine from the given condition (whether a block, procedure, method or string)
0
- # whether or not to validate the record. See #validates_each.
0
- def evaluate_condition(condition, record)
0
- when Symbol; record.send(condition)
0
- when String; eval(condition, record.instance_eval { binding })
0
- if condition_block?(condition)
0
- condition.call(record)
0
- "Validations need to be either a symbol, string (to be eval'ed), proc/method, or " +
0
- "class implementing a static validation method"
0
# Validates each attribute against a block.
0
@@ -379,20 +361,17 @@ module ActiveRecord
0
# method, proc or string should return or evaluate to a true or false value.
0
# * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should
0
# not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The
0
- # method, proc or string should return or evaluate to a true or false value.
0
+ # method, proc or string should return or evaluate to a true or false value.
0
def validates_each(*attrs)
0
options = attrs.extract_options!.symbolize_keys
0
# Declare the validation.
0
- send(validation_method(options[:on] || :save)) do |record|
0
- # Don't validate when there is an :if condition and that condition is false or there is an :unless condition and that condition is true
0
- unless (options[:if] && !evaluate_condition(options[:if], record)) || (options[:unless] && evaluate_condition(options[:unless], record))
0
- value = record.send(attr)
0
- next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
0
- yield record, attr, value
0
+ send(validation_method(options[:on] || :save), options) do |record|
0
+ value = record.send(attr)
0
+ next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
0
+ yield record, attr, value
0
@@ -515,11 +494,9 @@ module ActiveRecord
0
# can't use validates_each here, because it cannot cope with nonexistent attributes,
0
# while errors.add_on_empty can
0
- send(validation_method(configuration[:on])) do |record|
0
- unless (configuration[:if] && !evaluate_condition(configuration[:if], record)) || (configuration[:unless] && evaluate_condition(configuration[:unless], record))
0
- record.errors.add_on_blank(attr_names, configuration[:message])
0
+ send(validation_method(configuration[:on]), configuration) do |record|
0
+ record.errors.add_on_blank(attr_names, configuration[:message])
0
# Validates that the specified attribute matches the length restrictions supplied. Only one option can be used at a time:
0
@@ -911,13 +888,7 @@ module ActiveRecord
0
- def write_inheritable_set(key, methods)
0
- existing_methods = read_inheritable_attribute(key) || []
0
- write_inheritable_attribute(key, existing_methods | methods)
0
def validation_method(on)
0
when :save then :validate
0
@@ -959,14 +930,14 @@ module ActiveRecord
0
- run_
validations(:validate)
0
+ run_
callbacks(:validate)
0
- run_
validations(:validate_on_create)
0
+ run_
callbacks(:validate_on_create)
0
- run_
validations(:validate_on_update)
0
+ run_
callbacks(:validate_on_update)
0
@@ -990,36 +961,5 @@ module ActiveRecord
0
# Overwrite this method for validation checks used only on updates.
0
def validate_on_update # :doc:
0
- def run_validations(validation_method)
0
- validations = self.class.read_inheritable_attribute(validation_method.to_sym)
0
- if validations.nil? then return end
0
- validations.each do |validation|
0
- if validation.is_a?(Symbol)
0
- elsif validation.is_a?(String)
0
- eval(validation, binding)
0
- elsif validation_block?(validation)
0
- elsif validation_class?(validation, validation_method)
0
- validation.send(validation_method, self)
0
- "Validations need to be either a symbol, string (to be eval'ed), proc/method, or " +
0
- "class implementing a static validation method"
0
- def validation_block?(validation)
0
- validation.respond_to?("call") && (validation.arity == 1 || validation.arity == -1)
0
- def validation_class?(validation, validation_method)
0
- validation.respond_to?(validation_method)