Skip to content

Commit

Permalink
Merge pull request #29249 from bradleypriest/numericality-precision-r…
Browse files Browse the repository at this point in the history
…egression

Fix regression in Numericality validator
  • Loading branch information
matthewd authored and rafaelfranca committed May 31, 2017
1 parent c86a9e1 commit b0c66a2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions activemodel/CHANGELOG.md
@@ -1,3 +1,9 @@
* Fix regression in numericality validator when comparing Decimal and Float input
values with more scale than the schema.

*Bradley Priest*


## Rails 5.0.3 (May 12, 2017) ##

* The original string assigned to a model attribute is no longer incorrectly
Expand Down
4 changes: 3 additions & 1 deletion activemodel/lib/active_model/validations/numericality.rb
Expand Up @@ -39,7 +39,9 @@ def validate_each(record, attr_name, value)
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

Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/validations_test.rb
Expand Up @@ -169,6 +169,20 @@ def test_numericality_validation_with_mutation
Topic.reset_column_information
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'
Expand Down

0 comments on commit b0c66a2

Please sign in to comment.