Don't mark Float::INFINITY as changed when reassigning it

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
This commit is contained in:
MaicolBen 2023-11-03 12:24:19 -03:00 committed by Jonathan Hefner
parent 9c8585156e
commit 19869e765e
3 changed files with 24 additions and 1 deletions

View File

@ -42,7 +42,8 @@ module ActiveModel
end
def number_to_non_number?(old_value, new_value_before_type_cast)
old_value != nil && non_numeric_string?(new_value_before_type_cast.to_s)
old_value != nil && !new_value_before_type_cast.is_a?(::Numeric) &&
non_numeric_string?(new_value_before_type_cast.to_s)
end
def non_numeric_string?(value)

View File

@ -1,3 +1,9 @@
* Don't mark Float::INFINITY as changed when reassigning it
When saving a record with a float infinite value, it shouldn't mark as changed
*Maicol Bentancor*
* Support `RETURNING` clause for MariaDB
*fatkodima*, *Nikolay Kondratyev*

View File

@ -48,4 +48,20 @@ class PostgresqlNumberTest < ActiveRecord::PostgreSQLTestCase
assert_equal new_single, record.single
assert_equal new_double, record.double
end
def test_reassigning_infinity_does_not_mark_record_as_changed
record = PostgresqlNumber.create!(single: Float::INFINITY, double: -Float::INFINITY)
record.reload
record.single = Float::INFINITY
record.double = -Float::INFINITY
assert_not_predicate record, :changed?
end
def test_reassigning_nan_does_not_mark_record_as_changed
record = PostgresqlNumber.create!(single: Float::NAN, double: Float::NAN)
record.reload
record.single = Float::NAN
record.double = Float::NAN
assert_not_predicate record, :changed?
end
end