Merge pull request #29249 from bradleypriest/numericality-precision-regression

Fix regression in Numericality validator
This commit is contained in:
Matthew Draper 2017-05-28 11:00:07 +09:30 committed by GitHub
commit 1758292f55
3 changed files with 22 additions and 1 deletions

View File

@ -1,3 +1,8 @@
* Fix regression in numericality validator when comparing Decimal and Float input
values with more scale than the schema.
*Bradley Priest*
* Fix methods `#keys`, `#values` in `ActiveModel::Errors`.
Change `#keys` to only return the keys that don't have empty messages.

View File

@ -36,7 +36,9 @@ module ActiveModel
return
end
unless raw_value.is_a?(Numeric)
if raw_value.is_a?(Numeric)
value = raw_value
else
value = parse_raw_value_as_a_number(raw_value)
end

View File

@ -167,6 +167,20 @@ class ValidationsTest < ActiveRecord::TestCase
assert topic.valid?
end
def test_numericality_validation_checks_against_raw_value
klass = Class.new(Topic) do
def self.model_name
ActiveModel::Name.new(self, nil, "Topic")
end
attribute :wibble, :decimal, scale: 2, precision: 9
validates_numericality_of :wibble, greater_than_or_equal_to: BigDecimal.new("97.18")
end
assert_not klass.new(wibble: "97.179").valid?
assert_not klass.new(wibble: 97.179).valid?
assert_not klass.new(wibble: BigDecimal.new("97.179")).valid?
end
def test_acceptance_validator_doesnt_require_db_connection
klass = Class.new(ActiveRecord::Base) do
self.table_name = "posts"