0
- VALIDATIONS = %w( validate validate_on_create validate_on_update )
0
def self.included(base) # :nodoc:
0
base.extend(ClassMethods)
0
base.send!(:include, ActiveSupport::Callbacks)
0
-
VALIDATIONS.each do |validation_method|
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
methods = CallbackChain.build(:#{validation_method}, *methods, &block)
0
- # All of the following validations are defined in the class scope of the model that you're interested in validating.
0
- # They offer a more declarative way of specifying when the model is valid and when it is not. It is recommended to use
0
- # these over the low-level calls to validate and validate_on_create when possible.
0
- DEFAULT_VALIDATION_OPTIONS = {
0
- :allow_blank => false,
0
+ DEFAULT_VALIDATION_OPTIONS = { :on => :save, :allow_nil => false, :allow_blank => false, :message => nil }.freeze
0
+ # Adds a validation method or block to the class. This is useful when
0
+ # overriding the #validate instance method becomes too unwieldly and
0
+ # you're looking for more descriptive declaration of your validations.
0
+ # This can be done with a symbol pointing to a method:
0
+ # class Comment < ActiveRecord::Base
0
+ # validate :must_be_friends
0
+ # errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
0
+ # Or with a block which is passed the current record to be validated:
0
+ # class Comment < ActiveRecord::Base
0
+ # validate do |comment|
0
+ # comment.must_be_friends
0
+ # errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
0
+ # This usage applies to #validate_on_create and #validate_on_update as well.
0
+ # Validates each attribute against a block.
0
+ # class Person < ActiveRecord::Base
0
+ # validates_each :first_name, :last_name do |record, attr, value|
0
+ # record.errors.add attr, 'starts with z.' if value[0] == ?z
0
+ # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update)
0
+ # * <tt>allow_nil</tt> - Skip validation if attribute is nil.
0
+ # * <tt>allow_blank</tt> - Skip validation if attribute is blank.
0
+ # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
0
+ # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
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
+ def validates_each(*attrs)
0
+ options = attrs.extract_options!.symbolize_keys
0
+ # Declare the validation.
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
def validation_method(on)
0
if responds_to?(:validate_on_create)
0
ActiveSupport::Deprecations.warn(
0
- "Base#validate_on_create has been deprecated, please use Base.validate
:method, :on => :create instead")
0
+ "Base#validate_on_create has been deprecated, please use Base.validate
_on_create :method instead")
0
if responds_to?(:validate_on_update)
0
ActiveSupport::Deprecations.warn(
0
- "Base#validate_on_update has been deprecated, please use Base.validate
:method, :on => :update instead")
0
+ "Base#validate_on_update has been deprecated, please use Base.validate
_on_update :method instead")
Comments
No one has commented yet.