From b1ff29573f8870580e0d689fe9ff984566542c15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ku=C5=BAma?= Date: Fri, 27 Aug 2010 17:14:09 +0200 Subject: [PATCH] added not_equal_to option to NumericalityValidator --- activemodel/lib/active_model/locale/en.yml | 1 + .../lib/active_model/validations/numericality.rb | 3 ++- .../i18n_generate_message_validation_test.rb | 4 ++++ .../validations/numericality_validation_test.rb | 7 +++++++ 4 files changed, 14 insertions(+), 1 deletions(-) diff --git a/activemodel/lib/active_model/locale/en.yml b/activemodel/lib/active_model/locale/en.yml index 44425b4..fa11a4b 100644 --- a/activemodel/lib/active_model/locale/en.yml +++ b/activemodel/lib/active_model/locale/en.yml @@ -21,6 +21,7 @@ en: greater_than: "must be greater than %{count}" greater_than_or_equal_to: "must be greater than or equal to %{count}" equal_to: "must be equal to %{count}" + not_equal_to: "must not be equal to %{count}" less_than: "must be less than %{count}" less_than_or_equal_to: "must be less than or equal to %{count}" odd: "must be odd" diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index b6aff7a..126aa48 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -5,7 +5,7 @@ module ActiveModel class NumericalityValidator < EachValidator CHECKS = { :greater_than => :>, :greater_than_or_equal_to => :>=, :equal_to => :==, :less_than => :<, :less_than_or_equal_to => :<=, - :odd => :odd?, :even => :even? }.freeze + :odd => :odd?, :even => :even?, :not_equal_to => :"!=" }.freeze RESERVED_OPTIONS = CHECKS.keys + [:only_integer] @@ -99,6 +99,7 @@ module ActiveModel # * :greater_than - Specifies the value must be greater than the supplied value. # * :greater_than_or_equal_to - Specifies the value must be greater than or equal the supplied value. # * :equal_to - Specifies the value must be equal to the supplied value. + # * :not_equal_to - Specifies the value must not be equal to the supplied value. # * :less_than - Specifies the value must be less than the supplied value. # * :less_than_or_equal_to - Specifies the value must be less than or equal the supplied value. # * :odd - Specifies the value must be an odd number. diff --git a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb index 0679e67..0d1783b 100644 --- a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb @@ -120,6 +120,10 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase assert_equal "must be equal to 10", @person.errors.generate_message(:title, :equal_to, :value => 'title', :count => 10) end + def test_generate_message_not_equal_to_with_default_message + assert_equal "must not be equal to 10", @person.errors.generate_message(:title, :not_equal_to, :value => 'title', :count => 10) + end + def test_generate_message_less_than_with_default_message assert_equal "must be less than 10", @person.errors.generate_message(:title, :less_than, :value => 'title', :count => 10) end diff --git a/activemodel/test/cases/validations/numericality_validation_test.rb b/activemodel/test/cases/validations/numericality_validation_test.rb index e1d7d40..5316911 100644 --- a/activemodel/test/cases/validations/numericality_validation_test.rb +++ b/activemodel/test/cases/validations/numericality_validation_test.rb @@ -71,6 +71,13 @@ class NumericalityValidationTest < ActiveModel::TestCase valid!([10]) end + def test_validates_numericality_with_not_equal_to + Topic.validates_numericality_of :approved, :not_equal_to => 10 + + invalid!([10], 'must not be equal to 10') + valid!([-10, 11] + INFINITY) + end + def test_validates_numericality_with_less_than Topic.validates_numericality_of :approved, :less_than => 10 -- 1.7.0.4