From 715eba280c8494e9279754440c631706351b9fa5 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Sat, 24 Apr 2010 15:11:20 -0400 Subject: [PATCH] adding 'not an integer' error message for :only_integer option in validates_numericality_of [#4406 state:resolved] --- activemodel/lib/active_model/locale/en.yml | 1 + .../lib/active_model/validations/numericality.rb | 12 +++++++----- .../test/cases/validations/i18n_validation_test.rb | 20 ++++++++++---------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/activemodel/lib/active_model/locale/en.yml b/activemodel/lib/active_model/locale/en.yml index ea58021..6271194 100644 --- a/activemodel/lib/active_model/locale/en.yml +++ b/activemodel/lib/active_model/locale/en.yml @@ -17,6 +17,7 @@ en: too_short: "is too short (minimum is {{count}} characters)" wrong_length: "is the wrong length (should be {{count}} characters)" not_a_number: "is not a number" + not_an_integer: "is not an integer" 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}}" diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index c6d84c5..3792223 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -25,8 +25,10 @@ module ActiveModel return if options[:allow_nil] && raw_value.nil? - unless value = parse_raw_value(raw_value, options) - record.errors.add(attr_name, :not_a_number, :value => raw_value, :default => options[:message]) + error = '' + value = parse_raw_value(raw_value, options, error) + unless error.blank? + record.errors.add(attr_name, error.intern, :value => raw_value, :default => options[:message]) return end @@ -49,14 +51,14 @@ module ActiveModel protected - def parse_raw_value(raw_value, options) + def parse_raw_value(raw_value, options, error) if options[:only_integer] - raw_value.to_i if raw_value.to_s =~ /\A[+-]?\d+\Z/ + raw_value.to_s =~ /\A[+-]?\d+\Z/ ? raw_value.to_i : error << 'not_an_integer' else begin Kernel.Float(raw_value) rescue ArgumentError, TypeError - nil + error << 'not_a_number' end end end diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index 7dd82e7..050ee22 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -217,15 +217,15 @@ class I18nValidationTest < ActiveModel::TestCase def test_validates_numericality_of_only_integer_generates_message Person.validates_numericality_of :title, :only_integer => true - @person.title = 'a' - @person.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => nil}) + @person.title = '1.0' + @person.errors.expects(:generate_message).with(:title, :not_an_integer, {:value => '1.0', :default => nil}) @person.valid? end def test_validates_numericality_of_only_integer_generates_message_with_custom_default_message Person.validates_numericality_of :title, :only_integer => true, :message => 'custom' - @person.title = 'a' - @person.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => 'custom'}) + @person.title = '1.0' + @person.errors.expects(:generate_message).with(:title, :not_an_integer, {:value => '1.0', :default => 'custom'}) @person.valid? end @@ -430,10 +430,10 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_numericality_of_finds_global_default_translation - I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}} + I18n.backend.store_translations 'en', :errors => {:messages => {:not_an_integer => 'global message'}} Person.validates_numericality_of :title, :only_integer => true - @person.title = 'a' + @person.title = '1.0' @person.valid? assert_equal ['global message'], @person.errors[:title] end @@ -441,20 +441,20 @@ class I18nValidationTest < ActiveModel::TestCase # validates_numericality_of with :only_integer w/o mocha def test_validates_numericality_of_only_integer_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:not_an_integer => 'custom message'}}}}}} I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}} Person.validates_numericality_of :title, :only_integer => true - @person.title = 'a' + @person.title = '1.0' @person.valid? assert_equal ['custom message'], @person.errors[:title] end def test_validates_numericality_of_only_integer_finds_global_default_translation - I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}} + I18n.backend.store_translations 'en', :errors => {:messages => {:not_an_integer => 'global message'}} Person.validates_numericality_of :title, :only_integer => true - @person.title = 'a' + @person.title = '1.0' @person.valid? assert_equal ['global message'], @person.errors[:title] end -- 1.6.5.2