Skip to content

Commit 75e8e77

Browse files
committed
Merge pull request rails#6367 from frodsan/fix_validators_docs
Fix validators docs [3-2-stable]
2 parents 0f43592 + 4bbd05c commit 75e8e77

File tree

9 files changed

+154
-116
lines changed

9 files changed

+154
-116
lines changed

activemodel/lib/active_model/validations/acceptance.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module ActiveModel
2-
32
# == Active Model Acceptance Validator
43
module Validations
54
class AcceptanceValidator < EachValidator
@@ -59,7 +58,7 @@ module HelperMethods
5958
# The method, proc or string should return or evaluate to a true or
6059
# false value.
6160
# * <tt>:strict</tt> - Specifies whether validation should be strict.
62-
# See <tt>ActiveModel::Validation#validates!</tt> for more information
61+
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
6362
def validates_acceptance_of(*attr_names)
6463
validates_with AcceptanceValidator, _merge_attributes(attr_names)
6564
end

activemodel/lib/active_model/validations/confirmation.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module ActiveModel
2-
32
# == Active Model Confirmation Validator
43
module Validations
54
class ConfirmationValidator < EachValidator
@@ -59,7 +58,7 @@ module HelperMethods
5958
# <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
6059
# method, proc or string should return or evaluate to a true or false value.
6160
# * <tt>:strict</tt> - Specifies whether validation should be strict.
62-
# See <tt>ActiveModel::Validation#validates!</tt> for more information
61+
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
6362
def validates_confirmation_of(*attr_names)
6463
validates_with ConfirmationValidator, _merge_attributes(attr_names)
6564
end

activemodel/lib/active_model/validations/exclusion.rb

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'active_support/core_ext/range'
22

33
module ActiveModel
4-
54
# == Active Model Exclusion Validator
65
module Validations
76
class ExclusionValidator < EachValidator
@@ -24,43 +23,50 @@ def validate_each(record, attribute, value)
2423

2524
private
2625

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

3535
module HelperMethods
36-
# Validates that the value of the specified attribute is not in a particular enumerable object.
36+
# Validates that the value of the specified attribute is not in a particular
37+
# enumerable object.
3738
#
3839
# class Person < ActiveRecord::Base
3940
# validates_exclusion_of :username, :in => %w( admin superuser ), :message => "You don't belong here"
4041
# validates_exclusion_of :age, :in => 30..60, :message => "This site is only for under 30 and over 60"
4142
# validates_exclusion_of :format, :in => %w( mov avi ), :message => "extension %{value} is not allowed"
42-
# validates_exclusion_of :password, :in => lambda { |p| [p.username, p.first_name] }, :message => "should not be the same as your username or first name"
43+
# validates_exclusion_of :password, :in => lambda { |p| [p.username, p.first_name] },
44+
# :message => "should not be the same as your username or first name"
4345
# end
4446
#
4547
# Configuration options:
46-
# * <tt>:in</tt> - An enumerable object of items that the value shouldn't be part of.
47-
# This can be supplied as a proc or lambda which returns an enumerable. If the enumerable
48-
# is a range the test is performed with <tt>Range#cover?</tt>
48+
# * <tt>:in</tt> - An enumerable object of items that the value shouldn't be
49+
# part of. This can be supplied as a proc or lambda which returns an enumerable.
50+
# If the enumerable is a range the test is performed with <tt>Range#cover?</tt>
4951
# (backported in Active Support for 1.8), otherwise with <tt>include?</tt>.
5052
# * <tt>:message</tt> - Specifies a custom error message (default is: "is reserved").
51-
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
52-
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
53+
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute
54+
# is +nil+ (default is +false+).
55+
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the
56+
# attribute is blank (default is +false+).
5357
# * <tt>:on</tt> - Specifies when this validation is active. Runs in all
5458
# validation contexts by default (+nil+), other options are <tt>:create</tt>
5559
# and <tt>:update</tt>.
56-
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
57-
# occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
58-
# method, proc or string should return or evaluate to a true or false value.
59-
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
60-
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
61-
# method, proc or string should return or evaluate to a true or false value.
60+
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the
61+
# validation should occur (e.g. <tt>:if => :allow_validation</tt>, or
62+
# <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
63+
# or string should return or evaluate to a true or false value.
64+
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if
65+
# the validation should not occur (e.g. <tt>:unless => :skip_validation</tt>,
66+
# or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The method,
67+
# proc or string should return or evaluate to a true or false value.
6268
# * <tt>:strict</tt> - Specifies whether validation should be strict.
63-
# See <tt>ActiveModel::Validation#validates!</tt> for more information
69+
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
6470
def validates_exclusion_of(*attr_names)
6571
validates_with ExclusionValidator, _merge_attributes(attr_names)
6672
end

activemodel/lib/active_model/validations/format.rb

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module ActiveModel
2-
32
# == Active Model Format Validator
43
module Validations
54
class FormatValidator < EachValidator
@@ -42,50 +41,62 @@ def check_options_validity(options, name)
4241
end
4342

4443
module HelperMethods
45-
# Validates whether the value of the specified attribute is of the correct form, going by the regular expression provided.
46-
# You can require that the attribute matches the regular expression:
44+
# Validates whether the value of the specified attribute is of the correct form,
45+
# going by the regular expression provided. You can require that the attribute
46+
# matches the regular expression:
4747
#
4848
# class Person < ActiveRecord::Base
4949
# validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
5050
# end
5151
#
52-
# Alternatively, you can require that the specified attribute does _not_ match the regular expression:
52+
# Alternatively, you can require that the specified attribute does _not_ match
53+
# the regular expression:
5354
#
5455
# class Person < ActiveRecord::Base
5556
# validates_format_of :email, :without => /NOSPAM/
5657
# end
5758
#
58-
# You can also provide a proc or lambda which will determine the regular expression that will be used to validate the attribute
59+
# You can also provide a proc or lambda which will determine the regular
60+
# expression that will be used to validate the attribute.
5961
#
6062
# class Person < ActiveRecord::Base
6163
# # Admin can have number as a first letter in their screen name
62-
# 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 }
64+
# validates_format_of :screen_name,
65+
# :with => lambda{ |person| person.admin? ? /\A[a-z0-9][a-z0-9_\-]*\Z/i : /\A[a-z][a-z0-9_\-]*\Z/i }
6366
# end
6467
#
65-
# 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.
68+
# Note: use <tt>\A</tt> and <tt>\Z</tt> to match the start and end of the string,
69+
# <tt>^</tt> and <tt>$</tt> match the start/end of a line.
6670
#
67-
# You must pass either <tt>:with</tt> or <tt>:without</tt> as an option. In addition, both must be a regular expression
68-
# or a proc or lambda, or else an exception will be raised.
71+
# You must pass either <tt>:with</tt> or <tt>:without</tt> as an option. In
72+
# addition, both must be a regular expression or a proc or lambda, or else an
73+
# exception will be raised.
6974
#
7075
# Configuration options:
7176
# * <tt>:message</tt> - A custom error message (default is: "is invalid").
72-
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
73-
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
74-
# * <tt>:with</tt> - Regular expression that if the attribute matches will result in a successful validation.
75-
# This can be provided as a proc or lambda returning regular expression which will be called at runtime.
76-
# * <tt>:without</tt> - Regular expression that if the attribute does not match will result in a successful validation.
77-
# This can be provided as a proc or lambda returning regular expression which will be called at runtime.
77+
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute
78+
# is +nil+ (default is +false+).
79+
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the
80+
# attribute is blank (default is +false+).
81+
# * <tt>:with</tt> - Regular expression that if the attribute matches will
82+
# result in a successful validation. This can be provided as a proc or lambda
83+
# returning regular expression which will be called at runtime.
84+
# * <tt>:without</tt> - Regular expression that if the attribute does not match
85+
# will result in a successful validation. This can be provided as a proc or
86+
# lambda returning regular expression which will be called at runtime.
7887
# * <tt>:on</tt> - Specifies when this validation is active. Runs in all
7988
# validation contexts by default (+nil+), other options are <tt>:create</tt>
8089
# and <tt>:update</tt>.
81-
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
82-
# occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
83-
# method, proc or string should return or evaluate to a true or false value.
84-
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
85-
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
86-
# method, proc or string should return or evaluate to a true or false value.
90+
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the
91+
# validation should occur (e.g. <tt>:if => :allow_validation</tt>, or
92+
# <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
93+
# or string should return or evaluate to a true or false value.
94+
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if
95+
# the validation should not occur (e.g. <tt>:unless => :skip_validation</tt>,
96+
# or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The method,
97+
# proc or string should return or evaluate to a true or false value.
8798
# * <tt>:strict</tt> - Specifies whether validation should be strict.
88-
# See <tt>ActiveModel::Validation#validates!</tt> for more information
99+
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
89100
def validates_format_of(*attr_names)
90101
validates_with FormatValidator, _merge_attributes(attr_names)
91102
end

activemodel/lib/active_model/validations/inclusion.rb

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'active_support/core_ext/range'
22

33
module ActiveModel
4-
54
# == Active Model Inclusion Validator
65
module Validations
76
class InclusionValidator < EachValidator
@@ -24,16 +23,18 @@ def validate_each(record, attribute, value)
2423

2524
private
2625

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

3535
module HelperMethods
36-
# Validates whether the value of the specified attribute is available in a particular enumerable object.
36+
# Validates whether the value of the specified attribute is available in a
37+
# particular enumerable object.
3738
#
3839
# class Person < ActiveRecord::Base
3940
# validates_inclusion_of :gender, :in => %w( m f )
@@ -47,20 +48,25 @@ module HelperMethods
4748
# supplied as a proc or lambda which returns an enumerable. If the enumerable
4849
# is a range the test is performed with <tt>Range#cover?</tt>
4950
# (backported in Active Support for 1.8), otherwise with <tt>include?</tt>.
50-
# * <tt>:message</tt> - Specifies a custom error message (default is: "is not included in the list").
51-
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
52-
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
51+
# * <tt>:message</tt> - Specifies a custom error message (default is: "is not
52+
# included in the list").
53+
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute
54+
# is +nil+ (default is +false+).
55+
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the
56+
# attribute is blank (default is +false+).
5357
# * <tt>:on</tt> - Specifies when this validation is active. Runs in all
5458
# validation contexts by default (+nil+), other options are <tt>:create</tt>
5559
# and <tt>:update</tt>.
56-
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
57-
# occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
58-
# method, proc or string should return or evaluate to a true or false value.
59-
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
60-
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
61-
# method, proc or string should return or evaluate to a true or false value.
60+
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if
61+
# the validation should occur (e.g. <tt>:if => :allow_validation</tt>, or
62+
# <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
63+
# or string should return or evaluate to a true or false value.
64+
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine
65+
# if the validation should not occur (e.g. <tt>:unless => :skip_validation</tt>,
66+
# or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The method,
67+
# proc or string should return or evaluate to a true or false value.
6268
# * <tt>:strict</tt> - Specifies whether validation should be strict.
63-
# See <tt>ActiveModel::Validation#validates!</tt> for more information
69+
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
6470
def validates_inclusion_of(*attr_names)
6571
validates_with InclusionValidator, _merge_attributes(attr_names)
6672
end

0 commit comments

Comments
 (0)