Skip to content

Commit

Permalink
Merge pull request #28050 from namusyaka/avoid-converting-int-into-float
Browse files Browse the repository at this point in the history
Avoid converting integer as a string into float
  • Loading branch information
rafaelfranca committed Mar 27, 2017
2 parents 3b474da + b0be779 commit 9426bd7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions activemodel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
* Avoid converting integer as a string into float.

*namusyaka*


Please check [5-1-stable](https://github.com/rails/rails/blob/5-1-stable/activemodel/CHANGELOG.md) for previous changes.
1 change: 1 addition & 0 deletions activemodel/lib/active_model/validations/numericality.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def is_number?(raw_value)
end

def parse_raw_value_as_a_number(raw_value)
return raw_value.to_i if is_integer?(raw_value)
Kernel.Float(raw_value) if raw_value !~ /\A0[xX]/
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ def test_validates_numericality_of_for_ruby_class
Person.clear_validators!
end

def test_validates_numericality_with_exponent_number
base = 10_000_000_000_000_000
Topic.validates_numericality_of :approved, less_than_or_equal_to: base
topic = Topic.new
topic.approved = (base + 1).to_s

assert topic.invalid?
end

def test_validates_numericality_with_invalid_args
assert_raise(ArgumentError) { Topic.validates_numericality_of :approved, greater_than_or_equal_to: "foo" }
assert_raise(ArgumentError) { Topic.validates_numericality_of :approved, less_than_or_equal_to: "foo" }
Expand Down

0 comments on commit 9426bd7

Please sign in to comment.