Skip to content

Commit

Permalink
Don't mark Float::INFINITY as changed when reassigning it
Browse files Browse the repository at this point in the history
Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
  • Loading branch information
MaicolBen and jonathanhefner committed Nov 6, 2023
1 parent 9c85851 commit 19869e7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion activemodel/lib/active_model/type/helpers/numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def equal_nan?(old_value, new_value)
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)
Expand Down
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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*
Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/adapters/postgresql/numbers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@ def test_update
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

0 comments on commit 19869e7

Please sign in to comment.