2009-06-08 21:32:08 -04:00
|
|
|
require 'active_support/core_ext/array/extract_options'
|
2010-03-27 14:50:11 -04:00
|
|
|
require 'active_support/core_ext/array/wrap'
|
2010-02-18 10:28:48 -05:00
|
|
|
require 'active_support/core_ext/class/attribute'
|
2009-06-08 21:32:08 -04:00
|
|
|
require 'active_support/core_ext/hash/keys'
|
2009-12-22 18:39:41 -05:00
|
|
|
require 'active_model/errors'
|
2009-06-08 21:32:08 -04:00
|
|
|
|
2007-11-09 09:59:15 -05:00
|
|
|
module ActiveModel
|
2010-01-31 18:08:20 -05:00
|
|
|
|
|
|
|
# Provides a full validation framework to your objects.
|
|
|
|
#
|
|
|
|
# A minimal implementation could be:
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# attr_accessor :first_name, :last_name
|
|
|
|
#
|
|
|
|
# validates_each :first_name, :last_name do |record, attr, value|
|
|
|
|
# record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Which provides you with the full standard validation stack that you
|
|
|
|
# know from ActiveRecord.
|
|
|
|
#
|
|
|
|
# person = Person.new
|
|
|
|
# person.valid?
|
|
|
|
# #=> true
|
|
|
|
# person.invalid?
|
|
|
|
# #=> false
|
|
|
|
# person.first_name = 'zoolander'
|
2010-05-08 16:21:44 -04:00
|
|
|
# person.valid?
|
2010-01-31 18:08:20 -05:00
|
|
|
# #=> false
|
|
|
|
# person.invalid?
|
|
|
|
# #=> true
|
|
|
|
# person.errors
|
|
|
|
# #=> #<OrderedHash {:first_name=>["starts with z."]}>
|
|
|
|
#
|
|
|
|
# Note that ActiveModel::Validations automatically adds an +errors+ method
|
|
|
|
# to your instances initialized with a new ActiveModel::Errors object, so
|
|
|
|
# there is no need for you to add this manually.
|
|
|
|
#
|
2007-11-09 09:59:15 -05:00
|
|
|
module Validations
|
2009-05-29 18:23:38 -04:00
|
|
|
extend ActiveSupport::Concern
|
2009-10-12 23:15:43 -04:00
|
|
|
include ActiveSupport::Callbacks
|
2009-05-29 18:23:38 -04:00
|
|
|
|
|
|
|
included do
|
2009-10-20 20:20:01 -04:00
|
|
|
extend ActiveModel::Translation
|
2010-05-11 06:28:42 -04:00
|
|
|
|
2010-05-11 09:36:09 -04:00
|
|
|
extend HelperMethods
|
|
|
|
include HelperMethods
|
2010-05-11 06:28:42 -04:00
|
|
|
|
2009-09-08 11:22:45 -04:00
|
|
|
define_callbacks :validate, :scope => :name
|
2010-02-18 10:28:48 -05:00
|
|
|
|
2010-05-08 16:21:44 -04:00
|
|
|
attr_accessor :validation_context
|
|
|
|
|
2010-02-18 10:28:48 -05:00
|
|
|
class_attribute :_validators
|
|
|
|
self._validators = Hash.new { |h,k| h[k] = [] }
|
2008-03-31 19:40:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2009-12-22 19:37:19 -05:00
|
|
|
# Validates each attribute against a block.
|
|
|
|
#
|
2010-01-07 12:44:35 -05:00
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# attr_accessor :first_name, :last_name
|
|
|
|
#
|
2009-12-22 19:37:19 -05:00
|
|
|
# validates_each :first_name, :last_name do |record, attr, value|
|
2010-01-31 18:08:20 -05:00
|
|
|
# record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
|
2009-12-22 19:37:19 -05:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Options:
|
2010-01-07 12:44:35 -05:00
|
|
|
# * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>,
|
|
|
|
# other options <tt>:create</tt>, <tt>:update</tt>).
|
2009-12-22 19:37:19 -05:00
|
|
|
# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
|
|
|
|
# * <tt>:allow_blank</tt> - Skip validation if attribute is blank.
|
|
|
|
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
|
2010-01-07 12:44:35 -05:00
|
|
|
# occur (e.g. <tt>:if => :allow_validation</tt>, or
|
|
|
|
# <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
|
2009-12-22 19:37:19 -05:00
|
|
|
# method, proc or string should return or evaluate to a true or false value.
|
|
|
|
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
|
2010-01-07 12:44:35 -05:00
|
|
|
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or
|
|
|
|
# <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
|
2009-12-22 19:37:19 -05:00
|
|
|
# method, proc or string should return or evaluate to a true or false value.
|
|
|
|
def validates_each(*attr_names, &block)
|
|
|
|
options = attr_names.extract_options!.symbolize_keys
|
|
|
|
validates_with BlockValidator, options.merge(:attributes => attr_names.flatten), &block
|
|
|
|
end
|
|
|
|
|
2008-03-31 20:13:39 -04:00
|
|
|
# Adds a validation method or block to the class. This is useful when
|
2008-05-02 09:45:23 -04:00
|
|
|
# overriding the +validate+ instance method becomes too unwieldly and
|
2008-03-31 20:13:39 -04:00
|
|
|
# you're looking for more descriptive declaration of your validations.
|
|
|
|
#
|
|
|
|
# This can be done with a symbol pointing to a method:
|
|
|
|
#
|
2010-01-07 12:44:35 -05:00
|
|
|
# class Comment
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
2008-03-31 20:13:39 -04:00
|
|
|
# validate :must_be_friends
|
|
|
|
#
|
|
|
|
# def must_be_friends
|
|
|
|
# errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Or with a block which is passed the current record to be validated:
|
|
|
|
#
|
2010-01-07 12:44:35 -05:00
|
|
|
# class Comment
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
2008-03-31 20:13:39 -04:00
|
|
|
# validate do |comment|
|
|
|
|
# comment.must_be_friends
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def must_be_friends
|
|
|
|
# errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2009-09-08 11:10:14 -04:00
|
|
|
def validate(*args, &block)
|
|
|
|
options = args.last
|
|
|
|
if options.is_a?(Hash) && options.key?(:on)
|
2010-03-27 14:50:11 -04:00
|
|
|
options[:if] = Array.wrap(options[:if])
|
2010-05-08 16:21:44 -04:00
|
|
|
options[:if] << "validation_context == :#{options[:on]}"
|
2009-06-08 21:32:08 -04:00
|
|
|
end
|
2009-09-08 11:22:45 -04:00
|
|
|
set_callback(:validate, *args, &block)
|
2009-09-08 11:10:14 -04:00
|
|
|
end
|
2010-02-18 10:28:48 -05:00
|
|
|
|
|
|
|
# List all validators that being used to validate the model using +validates_with+
|
|
|
|
# method.
|
|
|
|
def validators
|
|
|
|
_validators.values.flatten.uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
# List all validators that being used to validate a specific attribute.
|
|
|
|
def validators_on(attribute)
|
|
|
|
_validators[attribute.to_sym]
|
|
|
|
end
|
|
|
|
|
2010-04-28 15:24:47 -04:00
|
|
|
def attribute_method?(attribute)
|
|
|
|
method_defined?(attribute)
|
|
|
|
end
|
2008-03-31 19:40:34 -04:00
|
|
|
end
|
|
|
|
|
2008-03-31 20:05:48 -04:00
|
|
|
# Returns the Errors object that holds all information about attribute error messages.
|
|
|
|
def errors
|
2009-03-19 19:28:59 -04:00
|
|
|
@errors ||= Errors.new(self)
|
2008-03-31 20:05:48 -04:00
|
|
|
end
|
2008-03-31 19:40:34 -04:00
|
|
|
|
2008-03-31 20:05:48 -04:00
|
|
|
# Runs all the specified validations and returns true if no errors were added otherwise false.
|
2010-05-08 16:21:44 -04:00
|
|
|
# Context can optionally be supplied to define which callbacks to test against (the context is
|
|
|
|
# defined on the validations using :on).
|
|
|
|
def valid?(context = nil)
|
|
|
|
current_context, self.validation_context = validation_context, context
|
2008-03-31 19:40:34 -04:00
|
|
|
errors.clear
|
2009-09-08 11:10:14 -04:00
|
|
|
_run_validate_callbacks
|
2008-03-31 19:40:34 -04:00
|
|
|
errors.empty?
|
2010-05-08 16:21:44 -04:00
|
|
|
ensure
|
|
|
|
self.validation_context = current_context
|
2008-03-31 19:40:34 -04:00
|
|
|
end
|
2009-03-19 19:28:59 -04:00
|
|
|
|
|
|
|
# Performs the opposite of <tt>valid?</tt>. Returns true if errors were added, false otherwise.
|
2010-05-08 16:21:44 -04:00
|
|
|
def invalid?(context = nil)
|
|
|
|
!valid?(context)
|
2009-03-19 19:28:59 -04:00
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2010-01-07 12:44:35 -05:00
|
|
|
# Hook method defining how an attribute value should be retieved. By default this is assumed
|
|
|
|
# to be an instance named after the attribute. Override this method in subclasses should you
|
|
|
|
# need to retrieve the value for a given attribute differently e.g.
|
|
|
|
# class MyClass
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# def initialize(data = {})
|
|
|
|
# @data = data
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def read_attribute_for_validation(key)
|
|
|
|
# @data[key]
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2010-01-07 13:22:32 -05:00
|
|
|
alias :read_attribute_for_validation :send
|
2007-11-09 09:59:15 -05:00
|
|
|
end
|
2008-03-31 20:05:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Dir[File.dirname(__FILE__) + "/validations/*.rb"].sort.each do |path|
|
|
|
|
filename = File.basename(path)
|
|
|
|
require "active_model/validations/#{filename}"
|
2009-03-19 19:28:59 -04:00
|
|
|
end
|