diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-07-20 14:12:01 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-07-20 14:19:26 -0300 |
commit | 089371ac23f35550676e6c5ac99e789a97101475 (patch) | |
tree | db23dfe0a0799edb35ee459d629f7bc673ecd338 /activemodel/lib | |
parent | a79bfa92e7bdc31b346d13ee5447d3fdac382bfb (diff) | |
download | rails-089371ac23f35550676e6c5ac99e789a97101475.tar.gz rails-089371ac23f35550676e6c5ac99e789a97101475.tar.bz2 rails-089371ac23f35550676e6c5ac99e789a97101475.zip |
`validates_inclusion_of` and `validates_exclusion_of` now accept
`:within` option as alias of `:in` as documented.
Fix #7118
Diffstat (limited to 'activemodel/lib')
-rw-r--r-- | activemodel/lib/active_model/validations/exclusion.rb | 14 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations/inclusion.rb | 14 |
2 files changed, 18 insertions, 10 deletions
diff --git a/activemodel/lib/active_model/validations/exclusion.rb b/activemodel/lib/active_model/validations/exclusion.rb index 725f810fa7..ebbf4acb66 100644 --- a/activemodel/lib/active_model/validations/exclusion.rb +++ b/activemodel/lib/active_model/validations/exclusion.rb @@ -5,24 +5,27 @@ module ActiveModel module Validations class ExclusionValidator < EachValidator ERROR_MESSAGE = "An object with the method #include? or a proc or lambda is required, " << - "and must be supplied as the :in option of the configuration hash" + "and must be supplied as the :in (or :within) option of the configuration hash" def check_validity! - unless [:include?, :call].any? { |method| options[:in].respond_to?(method) } + unless [:include?, :call].any? { |method| delimiter.respond_to?(method) } raise ArgumentError, ERROR_MESSAGE end end def validate_each(record, attribute, value) - delimiter = options[:in] exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter if exclusions.send(inclusion_method(exclusions), value) - record.errors.add(attribute, :exclusion, options.except(:in).merge!(:value => value)) + record.errors.add(attribute, :exclusion, options.except(:in, :within).merge!(:value => value)) end end private + def delimiter + @delimiter ||= options[:in] || options[:within] + end + # 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 @@ -49,6 +52,7 @@ module ActiveModel # 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>:within</tt> - A synonym(or alias) for <tt>:in</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+). @@ -65,7 +69,7 @@ module ActiveModel # 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. + # * <tt>:strict</tt> - Specifies whether validation should be strict. # See <tt>ActiveModel::Validation#validates!</tt> for more information. def validates_exclusion_of(*attr_names) validates_with ExclusionValidator, _merge_attributes(attr_names) diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb index 568e877d95..f053452555 100644 --- a/activemodel/lib/active_model/validations/inclusion.rb +++ b/activemodel/lib/active_model/validations/inclusion.rb @@ -5,24 +5,27 @@ module ActiveModel module Validations class InclusionValidator < EachValidator ERROR_MESSAGE = "An object with the method #include? or a proc or lambda is required, " << - "and must be supplied as the :in option of the configuration hash" + "and must be supplied as the :in (or :within) option of the configuration hash" def check_validity! - unless [:include?, :call].any?{ |method| options[:in].respond_to?(method) } + unless [:include?, :call].any?{ |method| delimiter.respond_to?(method) } raise ArgumentError, ERROR_MESSAGE end end def validate_each(record, attribute, value) - delimiter = options[:in] exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter unless exclusions.send(inclusion_method(exclusions), value) - record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value)) + record.errors.add(attribute, :inclusion, options.except(:in, :within).merge!(:value => value)) end end private + def delimiter + @delimiter ||= options[:in] || options[:within] + end + # 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 @@ -48,6 +51,7 @@ module ActiveModel # 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>:within</tt> - A synonym(or alias) for <tt>:in</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 @@ -65,7 +69,7 @@ module ActiveModel # 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. + # * <tt>:strict</tt> - Specifies whether validation should be strict. # See <tt>ActiveModel::Validation#validates!</tt> for more information. def validates_inclusion_of(*attr_names) validates_with InclusionValidator, _merge_attributes(attr_names) |