From 089371ac23f35550676e6c5ac99e789a97101475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Fri, 20 Jul 2012 14:12:01 -0300 Subject: `validates_inclusion_of` and `validates_exclusion_of` now accept `:within` option as alias of `:in` as documented. Fix #7118 --- .../lib/active_model/validations/exclusion.rb | 14 +++++++++----- .../lib/active_model/validations/inclusion.rb | 14 +++++++++----- .../cases/validations/exclusion_validation_test.rb | 10 ++++++++++ .../test/cases/validations/i18n_validation_test.rb | 22 ++++++++++++++++++++++ .../cases/validations/inclusion_validation_test.rb | 10 ++++++++++ 5 files changed, 60 insertions(+), 10 deletions(-) (limited to 'activemodel') 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 Range#include? on non-numeric ranges checks all possible # values in the range for equality, so it may be slow for large ranges. The new # Range#cover? 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 Range#cover? # (backported in Active Support for 1.8), otherwise with include?. + # * :within - A synonym(or alias) for :in # * :message - Specifies a custom error message (default is: "is reserved"). # * :allow_nil - 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. :unless => :skip_validation, # or :unless => Proc.new { |user| user.signup_step <= 2 }). The method, # proc or string should return or evaluate to a true or false value. - # * :strict - Specifies whether validation should be strict. + # * :strict - Specifies whether validation should be strict. # See ActiveModel::Validation#validates! 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 Range#include? on non-numeric ranges checks all possible # values in the range for equality, so it may be slow for large ranges. The new # Range#cover? 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 Range#cover? # (backported in Active Support for 1.8), otherwise with include?. + # * :within - A synonym(or alias) for :in # * :message - Specifies a custom error message (default is: "is not # included in the list"). # * :allow_nil - If set to true, skips this validation if the attribute @@ -65,7 +69,7 @@ module ActiveModel # if the validation should not occur (e.g. :unless => :skip_validation, # or :unless => Proc.new { |user| user.signup_step <= 2 }). The method, # proc or string should return or evaluate to a true or false value. - # * :strict - Specifies whether validation should be strict. + # * :strict - Specifies whether validation should be strict. # See ActiveModel::Validation#validates! for more information. def validates_inclusion_of(*attr_names) validates_with InclusionValidator, _merge_attributes(attr_names) diff --git a/activemodel/test/cases/validations/exclusion_validation_test.rb b/activemodel/test/cases/validations/exclusion_validation_test.rb index adab8ccb2b..baccf72ecb 100644 --- a/activemodel/test/cases/validations/exclusion_validation_test.rb +++ b/activemodel/test/cases/validations/exclusion_validation_test.rb @@ -28,6 +28,16 @@ class ExclusionValidationTest < ActiveModel::TestCase assert_equal ["option monkey is restricted"], t.errors[:title] end + def test_validates_exclusion_of_with_within_option + Topic.validates_exclusion_of( :title, :within => %w( abe monkey ) ) + + assert Topic.new("title" => "something", "content" => "abc") + + t = Topic.new("title" => "monkey") + assert t.invalid? + assert t.errors[:title].any? + end + def test_validates_exclusion_of_for_ruby_class Person.validates_exclusion_of :karma, :in => %w( abe monkey ) diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index e9f0e430fe..b9b5968e17 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -159,6 +159,17 @@ class I18nValidationTest < ActiveModel::TestCase end end + # validates_inclusion_of using :within w/ mocha + + COMMON_CASES.each do |name, validation_options, generate_message_options| + test "validates_inclusion_of using :within on generated message #{name}" do + Person.validates_inclusion_of :title, validation_options.merge(:within => %w(a b c)) + @person.title = 'z' + @person.errors.expects(:generate_message).with(:title, :inclusion, generate_message_options.merge(:value => 'z')) + @person.valid? + end + end + # validates_exclusion_of w/ mocha COMMON_CASES.each do |name, validation_options, generate_message_options| @@ -170,6 +181,17 @@ class I18nValidationTest < ActiveModel::TestCase end end + # validates_exclusion_of using :within w/ mocha + + COMMON_CASES.each do |name, validation_options, generate_message_options| + test "validates_exclusion_of using :within generated message #{name}" do + Person.validates_exclusion_of :title, validation_options.merge(:within => %w(a b c)) + @person.title = 'a' + @person.errors.expects(:generate_message).with(:title, :exclusion, generate_message_options.merge(:value => 'a')) + @person.valid? + end + end + # validates_numericality_of without :only_integer w/ mocha COMMON_CASES.each do |name, validation_options, generate_message_options| diff --git a/activemodel/test/cases/validations/inclusion_validation_test.rb b/activemodel/test/cases/validations/inclusion_validation_test.rb index 851d345eab..c57fa75faf 100644 --- a/activemodel/test/cases/validations/inclusion_validation_test.rb +++ b/activemodel/test/cases/validations/inclusion_validation_test.rb @@ -60,6 +60,16 @@ class InclusionValidationTest < ActiveModel::TestCase assert_equal ["option uhoh is not in the list"], t.errors[:title] end + def test_validates_inclusion_of_with_within_option + Topic.validates_inclusion_of( :title, :within => %w( a b c d e f g ) ) + + assert Topic.new("title" => "a", "content" => "abc").valid? + + t = Topic.new("title" => "uhoh", "content" => "abc") + assert t.invalid? + assert t.errors[:title].any? + end + def test_validates_inclusion_of_for_ruby_class Person.validates_inclusion_of :karma, :in => %w( abe monkey ) -- cgit v1.2.3