Skip to content

Commit

Permalink
Fix validates_numericaly_of only integer error message [#4406 state:r…
Browse files Browse the repository at this point in the history
…esolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
reu authored and josevalim committed Apr 25, 2010
1 parent 864bd9c commit 77c099c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
1 change: 1 addition & 0 deletions activemodel/lib/active_model/locale/en.yml
Expand Up @@ -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: "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}}"
Expand Down
29 changes: 18 additions & 11 deletions activemodel/lib/active_model/validations/numericality.rb
Expand Up @@ -25,11 +25,18 @@ def validate_each(record, attr_name, value)

return if options[:allow_nil] && raw_value.nil?

unless value = parse_raw_value(raw_value, options)
unless value = parse_raw_value_as_a_number(raw_value)

This comment has been minimized.

Copy link
@deepak

deepak Feb 4, 2013

Contributor

value is now a Float? Is that really needed ?
This float value will be used only for validation right ? not for saving the record

what about using BigDecimal ?

record.errors.add(attr_name, :not_a_number, :value => raw_value, :default => options[:message])
return
end

if options[:only_integer]
unless value = parse_raw_value_as_an_integer(raw_value)
record.errors.add(attr_name, :not_an_integer, :value => raw_value, :default => options[:message])
return
end
end

options.slice(*CHECKS.keys).each do |option, option_value|
case option
when :odd, :even
Expand All @@ -44,23 +51,23 @@ def validate_each(record, attr_name, value)
record.errors.add(attr_name, option, :default => options[:message], :value => value, :count => option_value)
end
end
end
end
end

protected

def parse_raw_value(raw_value, options)
if options[:only_integer]
raw_value.to_i if raw_value.to_s =~ /\A[+-]?\d+\Z/
else
begin
Kernel.Float(raw_value)
rescue ArgumentError, TypeError
nil
end
def parse_raw_value_as_a_number(raw_value)
begin
Kernel.Float(raw_value)
rescue ArgumentError, TypeError
nil
end
end

def parse_raw_value_as_an_integer(raw_value)
raw_value.to_i if raw_value.to_s =~ /\A[+-]?\d+\Z/
end

end

module ClassMethods
Expand Down
18 changes: 9 additions & 9 deletions activemodel/test/cases/validations/i18n_validation_test.rb
Expand Up @@ -217,15 +217,15 @@ def test_validates_numericality_of_generates_message_with_custom_default_message

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 = '0.0'
@person.errors.expects(:generate_message).with(:title, :not_an_integer, {:value => '0.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 = '0.0'
@person.errors.expects(:generate_message).with(:title, :not_an_integer, {:value => '0.0', :default => 'custom'})
@person.valid?
end

Expand Down Expand Up @@ -441,20 +441,20 @@ def test_validates_numericality_of_finds_global_default_translation
# 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', :errors => {:messages => {:not_a_number => 'global 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_an_integer => '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
Expand Down

0 comments on commit 77c099c

Please sign in to comment.