Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9174

Show
Ignore:
Timestamp:
04/01/08 00:13:39 (3 months ago)
Author:
david
Message:

Move it around a bit

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activemodel/lib/active_model/validations.rb

    r9173 r9174  
    11module ActiveModel 
    22  module Validations 
    3     VALIDATIONS = %w( validate validate_on_create validate_on_update ) 
    4  
    53    def self.included(base) # :nodoc: 
    64      base.extend(ClassMethods) 
    75      base.send!(:include, ActiveSupport::Callbacks) 
    86 
    9       VALIDATIONS.each do |validation_method| 
     7      %w( validate validate_on_create validate_on_update ).each do |validation_method| 
    108        base.class_eval <<-"end_eval" 
    119          def self.#{validation_method}(*methods, &block) 
     
    2624    end 
    2725 
    28     # All of the following validations are defined in the class scope of the model that you're interested in validating. 
    29     # They offer a more declarative way of specifying when the model is valid and when it is not. It is recommended to use 
    30     # these over the low-level calls to validate and validate_on_create when possible. 
    3126    module ClassMethods 
    32       DEFAULT_VALIDATION_OPTIONS = { 
    33         :on          => :save, 
    34         :allow_nil   => false, 
    35         :allow_blank => false, 
    36         :message     => nil 
    37       }.freeze 
     27      DEFAULT_VALIDATION_OPTIONS = { :on => :save, :allow_nil => false, :allow_blank => false, :message => nil }.freeze 
     28 
     29      # Adds a validation method or block to the class. This is useful when 
     30      # overriding the #validate instance method becomes too unwieldly and 
     31      # you're looking for more descriptive declaration of your validations. 
     32      # 
     33      # This can be done with a symbol pointing to a method: 
     34      # 
     35      #   class Comment < ActiveRecord::Base 
     36      #     validate :must_be_friends 
     37      # 
     38      #     def must_be_friends 
     39      #       errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee) 
     40      #     end 
     41      #   end 
     42      # 
     43      # Or with a block which is passed the current record to be validated: 
     44      # 
     45      #   class Comment < ActiveRecord::Base 
     46      #     validate do |comment| 
     47      #       comment.must_be_friends 
     48      #     end 
     49      # 
     50      #     def must_be_friends 
     51      #       errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee) 
     52      #     end 
     53      #   end 
     54      # 
     55      # This usage applies to #validate_on_create and #validate_on_update as well. 
     56 
     57      # Validates each attribute against a block. 
     58      # 
     59      #   class Person < ActiveRecord::Base 
     60      #     validates_each :first_name, :last_name do |record, attr, value| 
     61      #       record.errors.add attr, 'starts with z.' if value[0] == ?z 
     62      #     end 
     63      #   end 
     64      # 
     65      # Options: 
     66      # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update) 
     67      # * <tt>allow_nil</tt> - Skip validation if attribute is nil. 
     68      # * <tt>allow_blank</tt> - Skip validation if attribute is blank. 
     69      # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 
     70      #   occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
     71      #   method, proc or string should return or evaluate to a true or false value. 
     72      # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 
     73      #   not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }).  The 
     74      #   method, proc or string should return or evaluate to a true or false value. 
     75      def validates_each(*attrs) 
     76        options = attrs.extract_options!.symbolize_keys 
     77        attrs   = attrs.flatten 
     78 
     79        # Declare the validation. 
     80        send(validation_method(options[:on] || :save), options) do |record| 
     81          attrs.each do |attr| 
     82            value = record.send(attr) 
     83            next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank]) 
     84            yield record, attr, value 
     85          end 
     86        end 
     87      end 
    3888 
    3989      private 
     
    68118        if responds_to?(:validate_on_create) 
    69119          ActiveSupport::Deprecations.warn( 
    70             "Base#validate_on_create has been deprecated, please use Base.validate :method, :on => :create instead") 
     120            "Base#validate_on_create has been deprecated, please use Base.validate_on_create :method instead") 
    71121          validate_on_create 
    72122        end 
     
    76126        if responds_to?(:validate_on_update) 
    77127          ActiveSupport::Deprecations.warn( 
    78             "Base#validate_on_update has been deprecated, please use Base.validate :method, :on => :update instead") 
     128            "Base#validate_on_update has been deprecated, please use Base.validate_on_update :method instead") 
    79129          validate_on_update 
    80130        end