From 5749ed473c07f397405c74a8c0154d578fa03458 Mon Sep 17 00:00:00 2001 From: reu Date: Sat, 24 Apr 2010 15:18:26 -0300 Subject: [PATCH] Fix validates_numericaly_of only integer error message [#4406 resolved] --- activerecord/lib/active_record/locale/en.yml | 8 ++++---- activerecord/lib/active_record/validations.rb | 22 +++++++++++----------- activerecord/test/cases/validations_i18n_test.rb | 12 ++++++------ 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/activerecord/lib/active_record/locale/en.yml b/activerecord/lib/active_record/locale/en.yml index 2813524..2ec3551 100644 --- a/activerecord/lib/active_record/locale/en.yml +++ b/activerecord/lib/active_record/locale/en.yml @@ -16,6 +16,7 @@ en: wrong_length: "is the wrong length (should be {{count}} characters)" taken: "has already been taken" not_a_number: "is not a number" + not_an_integer: "must be 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}}" @@ -39,20 +40,19 @@ en: # attributes: # login: # blank: "This is a custom blank message for User login" - # Will define custom blank validation message for User model and + # Will define custom blank validation message for User model and # custom blank validation message for login attribute of User model. #models: - + # Translate model names. Used in Model.human_name(). #models: # For example, # user: "Dude" # will translate User model name to "Dude" - + # Translate model attribute names. Used in Model.human_attribute_name(attribute). #attributes: # For example, # user: # login: "Handle" # will translate User attribute "login" as "Handle" - diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 41d28f3..3d07086 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -1020,29 +1020,29 @@ module ActiveRecord next if configuration[:allow_nil] and raw_value.nil? + begin + value = Kernel.Float(raw_value) + rescue ArgumentError, TypeError + record.errors.add(attr_name, :not_a_number, :value => raw_value, :default => configuration[:message]) + next + end + if configuration[:only_integer] unless raw_value.to_s =~ /\A[+-]?\d+\Z/ - record.errors.add(attr_name, :not_a_number, :value => raw_value, :default => configuration[:message]) - next - end - raw_value = raw_value.to_i - else - begin - raw_value = Kernel.Float(raw_value) - rescue ArgumentError, TypeError - record.errors.add(attr_name, :not_a_number, :value => raw_value, :default => configuration[:message]) + record.errors.add(attr_name, :not_an_integer, :value => raw_value, :default => configuration[:message]) next end + value = raw_value.to_i end numericality_options.each do |option| case option when :odd, :even - unless raw_value.to_i.method(ALL_NUMERICALITY_CHECKS[option])[] + unless value.to_i.method(ALL_NUMERICALITY_CHECKS[option])[] record.errors.add(attr_name, option, :value => raw_value, :default => configuration[:message]) end else - record.errors.add(attr_name, option, :default => configuration[:message], :value => raw_value, :count => configuration[option]) unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]] + record.errors.add(attr_name, option, :default => configuration[:message], :value => raw_value, :count => configuration[option]) unless value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]] end end end diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index 9d29385..f10537a 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -383,23 +383,23 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_numericality_of :not_a_number, with :only_integer test "#validates_numericality_of (:not_a_number, with :only_integer) no custom message" do - expect_error_added(@topic, :title, :not_a_number, :default => nil, :value => 'a') do + expect_error_added(@topic, :title, :not_an_integer, :default => nil, :value => '1.0') do Topic.validates_numericality_of :title, :only_integer => true - @topic.title = 'a' + @topic.title = '1.0' end end test "#validates_numericality_of (:not_a_number, with :only_integer) and a custom message" do - expect_error_added(@topic, :title, :not_a_number, :default => 'custom', :value => 'a') do + expect_error_added(@topic, :title, :not_an_integer, :default => 'custom', :value => '1.0') do Topic.validates_numericality_of :title, :only_integer => true, :message => 'custom' - @topic.title = 'a' + @topic.title = '1.0' end end test "#validates_numericality_of (:not_a_number, with :only_integer) finds the correct message translations" do - assert_message_translations(@topic, :title, :not_a_number) do + assert_message_translations(@topic, :title, :not_an_integer) do Topic.validates_numericality_of :title, :only_integer => true - @topic.title = 'a' + @topic.title = '1.0' end end -- 1.6.6.1