2008-03-31 20:05:48 -04:00
module ActiveModel
2010-06-15 13:50:19 -04:00
# == Active Model Length Validator
2008-03-31 20:05:48 -04:00
module Validations
2009-12-22 18:36:51 -05:00
class LengthValidator < EachValidator
2009-12-22 19:08:27 -05:00
MESSAGES = { :is = > :wrong_length , :minimum = > :too_short , :maximum = > :too_long } . freeze
CHECKS = { :is = > :== , :minimum = > :>= , :maximum = > :< = } . freeze
2009-12-22 18:36:51 -05:00
2009-12-22 19:08:27 -05:00
DEFAULT_TOKENIZER = lambda { | value | value . split ( / / ) }
2010-06-21 06:17:24 -04:00
RESERVED_OPTIONS = [ :minimum , :maximum , :within , :is , :tokenizer , :too_short , :too_long ]
2009-12-22 18:36:51 -05:00
def initialize ( options )
2010-01-08 02:34:44 -05:00
if range = ( options . delete ( :in ) || options . delete ( :within ) )
raise ArgumentError , " :in and :within must be a Range " unless range . is_a? ( Range )
options [ :minimum ] , options [ :maximum ] = range . begin , range . end
options [ :maximum ] -= 1 if range . exclude_end?
end
2009-12-23 06:14:00 -05:00
super ( options . reverse_merge ( :tokenizer = > DEFAULT_TOKENIZER ) )
2009-12-22 18:36:51 -05:00
end
2009-12-22 19:08:27 -05:00
def check_validity!
2010-01-08 02:34:44 -05:00
keys = CHECKS . keys & options . keys
if keys . empty?
raise ArgumentError , 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.'
end
keys . each do | key |
value = options [ key ]
unless value . is_a? ( Integer ) && value > = 0
raise ArgumentError , " : #{ key } must be a nonnegative Integer "
end
end
2009-12-22 19:08:27 -05:00
end
2010-08-14 01:13:00 -04:00
2009-12-22 18:36:51 -05:00
def validate_each ( record , attribute , value )
2010-01-08 02:34:44 -05:00
value = options [ :tokenizer ] . call ( value ) if value . kind_of? ( String )
2009-12-22 18:36:51 -05:00
2010-01-08 02:34:44 -05:00
CHECKS . each do | key , validity_check |
next unless check_value = options [ key ]
2009-12-22 18:36:51 -05:00
2010-08-04 11:04:41 -04:00
value || = [ ] if key == :maximum
2009-12-22 18:36:51 -05:00
2010-08-04 11:04:41 -04:00
next if value && value . size . send ( validity_check , check_value )
2010-05-19 10:25:46 -04:00
2010-08-03 09:22:54 -04:00
errors_options = options . except ( * RESERVED_OPTIONS )
errors_options [ :count ] = check_value
default_message = options [ MESSAGES [ key ] ]
errors_options [ :message ] || = default_message if default_message
record . errors . add ( attribute , MESSAGES [ key ] , errors_options )
2009-12-22 19:08:27 -05:00
end
end
2009-12-22 18:36:51 -05:00
end
2010-05-11 06:28:42 -04:00
module HelperMethods
2009-03-19 19:28:59 -04:00
2008-03-31 20:05:48 -04:00
# Validates that the specified attribute matches the length restrictions supplied. Only one option can be used at a time:
#
# class Person < ActiveRecord::Base
2009-03-19 19:28:59 -04:00
# validates_length_of :first_name, :maximum=>30
2009-09-15 08:11:20 -04:00
# validates_length_of :last_name, :maximum=>30, :message=>"less than 30 if you don't mind"
2008-03-31 20:05:48 -04:00
# validates_length_of :fax, :in => 7..32, :allow_nil => true
# validates_length_of :phone, :in => 7..32, :allow_blank => true
# validates_length_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name"
2009-09-15 08:11:20 -04:00
# validates_length_of :zip_code, :minimum => 5, :too_short => "please enter at least 5 characters"
# validates_length_of :smurf_leader, :is => 4, :message => "papa is spelled with 4 characters... don't play me."
# validates_length_of :essay, :minimum => 100, :too_short => "Your essay must be at least 100 words."), :tokenizer => lambda {|str| str.scan(/\w+/) }
2008-03-31 20:05:48 -04:00
# end
#
# Configuration options:
2009-03-19 19:28:59 -04:00
# * <tt>:minimum</tt> - The minimum size of the attribute.
# * <tt>:maximum</tt> - The maximum size of the attribute.
# * <tt>:is</tt> - The exact size of the attribute.
# * <tt>:within</tt> - A range specifying the minimum and maximum size of the attribute.
# * <tt>:in</tt> - A synonym(or alias) for <tt>:within</tt>.
2008-05-02 09:45:23 -04:00
# * <tt>:allow_nil</tt> - Attribute may be +nil+; skip validation.
# * <tt>:allow_blank</tt> - Attribute may be blank; skip validation.
2010-05-03 02:44:32 -04:00
# * <tt>:too_long</tt> - The error message if the attribute goes over the maximum (default is: "is too long (maximum is %{count} characters)").
# * <tt>:too_short</tt> - The error message if the attribute goes under the minimum (default is: "is too short (min is %{count} characters)").
# * <tt>:wrong_length</tt> - The error message if using the <tt>:is</tt> method and the attribute is the wrong size (default is: "is the wrong length (should be %{count} characters)").
2009-03-19 19:28:59 -04:00
# * <tt>:message</tt> - The error message to use for a <tt>:minimum</tt>, <tt>:maximum</tt>, or <tt>:is</tt> violation. An alias of the appropriate <tt>too_long</tt>/<tt>too_short</tt>/<tt>wrong_length</tt> message.
# * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).
2008-05-02 09:45:23 -04:00
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
# occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
# 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
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
2008-03-31 20:05:48 -04:00
# method, proc or string should return or evaluate to a true or false value.
2009-03-19 19:28:59 -04:00
# * <tt>:tokenizer</tt> - Specifies how to split up the attribute string. (e.g. <tt>:tokenizer => lambda {|str| str.scan(/\w+/)}</tt> to
# count words as in above example.)
# Defaults to <tt>lambda{ |value| value.split(//) }</tt> which counts individual characters.
2009-12-22 18:36:51 -05:00
def validates_length_of ( * attr_names )
2010-01-07 12:44:35 -05:00
validates_with LengthValidator , _merge_attributes ( attr_names )
2008-03-31 20:05:48 -04:00
end
alias_method :validates_size_of , :validates_length_of
end
end
2009-06-08 21:32:08 -04:00
end