Skip to content

Commit

Permalink
Merge pull request rails#6367 from frodsan/fix_validators_docs
Browse files Browse the repository at this point in the history
Fix validators docs [3-2-stable]
  • Loading branch information
vijaydev committed May 17, 2012
2 parents 0f43592 + 4bbd05c commit 75e8e77
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 116 deletions.
3 changes: 1 addition & 2 deletions activemodel/lib/active_model/validations/acceptance.rb
@@ -1,5 +1,4 @@
module ActiveModel

# == Active Model Acceptance Validator
module Validations
class AcceptanceValidator < EachValidator
Expand Down Expand Up @@ -59,7 +58,7 @@ module HelperMethods
# The method, proc or string should return or evaluate to a true or
# false value.
# * <tt>:strict</tt> - Specifies whether validation should be strict.
# See <tt>ActiveModel::Validation#validates!</tt> for more information
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
def validates_acceptance_of(*attr_names)
validates_with AcceptanceValidator, _merge_attributes(attr_names)
end
Expand Down
3 changes: 1 addition & 2 deletions activemodel/lib/active_model/validations/confirmation.rb
@@ -1,5 +1,4 @@
module ActiveModel

# == Active Model Confirmation Validator
module Validations
class ConfirmationValidator < EachValidator
Expand Down Expand Up @@ -59,7 +58,7 @@ module HelperMethods
# <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false value.
# * <tt>:strict</tt> - Specifies whether validation should be strict.
# See <tt>ActiveModel::Validation#validates!</tt> for more information
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
def validates_confirmation_of(*attr_names)
validates_with ConfirmationValidator, _merge_attributes(attr_names)
end
Expand Down
42 changes: 24 additions & 18 deletions activemodel/lib/active_model/validations/exclusion.rb
@@ -1,7 +1,6 @@
require 'active_support/core_ext/range'

module ActiveModel

# == Active Model Exclusion Validator
module Validations
class ExclusionValidator < EachValidator
Expand All @@ -24,43 +23,50 @@ def validate_each(record, attribute, value)

private

# In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the
# range for equality, so it may be slow for large ranges. The new <tt>Range#cover?</tt>
# uses the previous logic of comparing a value with the range endpoints.
# In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible
# values in the range for equality, so it may be slow for large ranges. The new
# <tt>Range#cover?</tt> uses the previous logic of comparing a value with the
# range endpoints.
def inclusion_method(enumerable)
enumerable.is_a?(Range) ? :cover? : :include?
end
end

module HelperMethods
# Validates that the value of the specified attribute is not in a particular enumerable object.
# Validates that the value of the specified attribute is not in a particular
# enumerable object.
#
# class Person < ActiveRecord::Base
# validates_exclusion_of :username, :in => %w( admin superuser ), :message => "You don't belong here"
# validates_exclusion_of :age, :in => 30..60, :message => "This site is only for under 30 and over 60"
# validates_exclusion_of :format, :in => %w( mov avi ), :message => "extension %{value} is not allowed"
# validates_exclusion_of :password, :in => lambda { |p| [p.username, p.first_name] }, :message => "should not be the same as your username or first name"
# validates_exclusion_of :password, :in => lambda { |p| [p.username, p.first_name] },
# :message => "should not be the same as your username or first name"
# end
#
# Configuration options:
# * <tt>:in</tt> - An enumerable object of items that the value shouldn't be part of.
# This can be supplied as a proc or lambda which returns an enumerable. If the enumerable
# is a range the test is performed with <tt>Range#cover?</tt>
# * <tt>:in</tt> - An enumerable object of items that the value shouldn't be
# part of. This can be supplied as a proc or lambda which returns an enumerable.
# If the enumerable is a range the test is performed with <tt>Range#cover?</tt>
# (backported in Active Support for 1.8), otherwise with <tt>include?</tt>.
# * <tt>:message</tt> - Specifies a custom error message (default is: "is reserved").
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute
# is +nil+ (default is +false+).
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the
# attribute is blank (default is +false+).
# * <tt>:on</tt> - Specifies when this validation is active. Runs in all
# validation contexts by default (+nil+), other options are <tt>:create</tt>
# and <tt>:update</tt>.
# * <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
# method, proc or string should return or evaluate to a true or false value.
# * <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 method,
# proc or string should return or evaluate to a true or false value.
# * <tt>:strict</tt> - Specifies whether validation should be strict.
# See <tt>ActiveModel::Validation#validates!</tt> for more information
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
def validates_exclusion_of(*attr_names)
validates_with ExclusionValidator, _merge_attributes(attr_names)
end
Expand Down
55 changes: 33 additions & 22 deletions activemodel/lib/active_model/validations/format.rb
@@ -1,5 +1,4 @@
module ActiveModel

# == Active Model Format Validator
module Validations
class FormatValidator < EachValidator
Expand Down Expand Up @@ -42,50 +41,62 @@ def check_options_validity(options, name)
end

module HelperMethods
# Validates whether the value of the specified attribute is of the correct form, going by the regular expression provided.
# You can require that the attribute matches the regular expression:
# Validates whether the value of the specified attribute is of the correct form,
# going by the regular expression provided. You can require that the attribute
# matches the regular expression:
#
# class Person < ActiveRecord::Base
# validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
# end
#
# Alternatively, you can require that the specified attribute does _not_ match the regular expression:
# Alternatively, you can require that the specified attribute does _not_ match
# the regular expression:
#
# class Person < ActiveRecord::Base
# validates_format_of :email, :without => /NOSPAM/
# end
#
# You can also provide a proc or lambda which will determine the regular expression that will be used to validate the attribute
# You can also provide a proc or lambda which will determine the regular
# expression that will be used to validate the attribute.
#
# class Person < ActiveRecord::Base
# # Admin can have number as a first letter in their screen name
# validates_format_of :screen_name, :with => lambda{ |person| person.admin? ? /\A[a-z0-9][a-z0-9_\-]*\Z/i : /\A[a-z][a-z0-9_\-]*\Z/i }
# validates_format_of :screen_name,
# :with => lambda{ |person| person.admin? ? /\A[a-z0-9][a-z0-9_\-]*\Z/i : /\A[a-z][a-z0-9_\-]*\Z/i }
# end
#
# Note: use <tt>\A</tt> and <tt>\Z</tt> to match the start and end of the string, <tt>^</tt> and <tt>$</tt> match the start/end of a line.
# Note: use <tt>\A</tt> and <tt>\Z</tt> to match the start and end of the string,
# <tt>^</tt> and <tt>$</tt> match the start/end of a line.
#
# You must pass either <tt>:with</tt> or <tt>:without</tt> as an option. In addition, both must be a regular expression
# or a proc or lambda, or else an exception will be raised.
# You must pass either <tt>:with</tt> or <tt>:without</tt> as an option. In
# addition, both must be a regular expression or a proc or lambda, or else an
# exception will be raised.
#
# Configuration options:
# * <tt>:message</tt> - A custom error message (default is: "is invalid").
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
# * <tt>:with</tt> - Regular expression that if the attribute matches will result in a successful validation.
# This can be provided as a proc or lambda returning regular expression which will be called at runtime.
# * <tt>:without</tt> - Regular expression that if the attribute does not match will result in a successful validation.
# This can be provided as a proc or lambda returning regular expression which will be called at runtime.
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute
# is +nil+ (default is +false+).
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the
# attribute is blank (default is +false+).
# * <tt>:with</tt> - Regular expression that if the attribute matches will
# result in a successful validation. This can be provided as a proc or lambda
# returning regular expression which will be called at runtime.
# * <tt>:without</tt> - Regular expression that if the attribute does not match
# will result in a successful validation. This can be provided as a proc or
# lambda returning regular expression which will be called at runtime.
# * <tt>:on</tt> - Specifies when this validation is active. Runs in all
# validation contexts by default (+nil+), other options are <tt>:create</tt>
# and <tt>:update</tt>.
# * <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
# method, proc or string should return or evaluate to a true or false value.
# * <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 method,
# proc or string should return or evaluate to a true or false value.
# * <tt>:strict</tt> - Specifies whether validation should be strict.
# See <tt>ActiveModel::Validation#validates!</tt> for more information
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
def validates_format_of(*attr_names)
validates_with FormatValidator, _merge_attributes(attr_names)
end
Expand Down
36 changes: 21 additions & 15 deletions activemodel/lib/active_model/validations/inclusion.rb
@@ -1,7 +1,6 @@
require 'active_support/core_ext/range'

module ActiveModel

# == Active Model Inclusion Validator
module Validations
class InclusionValidator < EachValidator
Expand All @@ -24,16 +23,18 @@ def validate_each(record, attribute, value)

private

# In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the
# range for equality, so it may be slow for large ranges. The new <tt>Range#cover?</tt>
# uses the previous logic of comparing a value with the range endpoints.
# In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible
# values in the range for equality, so it may be slow for large ranges. The new
# <tt>Range#cover?</tt> uses the previous logic of comparing a value with the
# range endpoints.
def inclusion_method(enumerable)
enumerable.is_a?(Range) ? :cover? : :include?
end
end

module HelperMethods
# Validates whether the value of the specified attribute is available in a particular enumerable object.
# Validates whether the value of the specified attribute is available in a
# particular enumerable object.
#
# class Person < ActiveRecord::Base
# validates_inclusion_of :gender, :in => %w( m f )
Expand All @@ -47,20 +48,25 @@ module HelperMethods
# supplied as a proc or lambda which returns an enumerable. If the enumerable
# is a range the test is performed with <tt>Range#cover?</tt>
# (backported in Active Support for 1.8), otherwise with <tt>include?</tt>.
# * <tt>:message</tt> - Specifies a custom error message (default is: "is not included in the list").
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
# * <tt>:message</tt> - Specifies a custom error message (default is: "is not
# included in the list").
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute
# is +nil+ (default is +false+).
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the
# attribute is blank (default is +false+).
# * <tt>:on</tt> - Specifies when this validation is active. Runs in all
# validation contexts by default (+nil+), other options are <tt>:create</tt>
# and <tt>:update</tt>.
# * <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
# method, proc or string should return or evaluate to a true or false value.
# * <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 method,
# proc or string should return or evaluate to a true or false value.
# * <tt>:strict</tt> - Specifies whether validation should be strict.
# See <tt>ActiveModel::Validation#validates!</tt> for more information
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
def validates_inclusion_of(*attr_names)
validates_with InclusionValidator, _merge_attributes(attr_names)
end
Expand Down

0 comments on commit 75e8e77

Please sign in to comment.