mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #24571 from raimo/patch-1
Print the proper ::Float::INFINITY value when used as a default value
This commit is contained in:
commit
de9a56b66a
3 changed files with 24 additions and 1 deletions
|
@ -7,6 +7,15 @@ module ActiveModel
|
|||
:float
|
||||
end
|
||||
|
||||
def type_cast_for_schema(value)
|
||||
return "::Float::NAN" if value.try(:nan?)
|
||||
case value
|
||||
when ::Float::INFINITY then "::Float::INFINITY"
|
||||
when -::Float::INFINITY then "-::Float::INFINITY"
|
||||
else super
|
||||
end
|
||||
end
|
||||
|
||||
alias serialize cast
|
||||
|
||||
private
|
||||
|
|
|
@ -423,6 +423,13 @@ class SchemaDumperDefaultsTest < ActiveRecord::TestCase
|
|||
t.datetime :datetime_with_default, default: "2014-06-05 07:17:04"
|
||||
t.time :time_with_default, default: "07:17:04"
|
||||
end
|
||||
|
||||
if current_adapter?(:PostgreSQLAdapter)
|
||||
@connection.create_table :infinity_defaults, force: true do |t|
|
||||
t.float :float_with_inf_default, default: Float::INFINITY
|
||||
t.float :float_with_nan_default, default: Float::NAN
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
teardown do
|
||||
|
@ -438,4 +445,11 @@ class SchemaDumperDefaultsTest < ActiveRecord::TestCase
|
|||
assert_match %r{t\.datetime\s+"datetime_with_default",\s+default: '2014-06-05 07:17:04'}, output
|
||||
assert_match %r{t\.time\s+"time_with_default",\s+default: '2000-01-01 07:17:04'}, output
|
||||
end
|
||||
|
||||
def test_schema_dump_with_float_column_infinity_default
|
||||
skip unless current_adapter?(:PostgreSQLAdapter)
|
||||
output = dump_table_schema('infinity_defaults')
|
||||
assert_match %r{t\.float\s+"float_with_inf_default",\s+default: ::Float::INFINITY}, output
|
||||
assert_match %r{t\.float\s+"float_with_nan_default",\s+default: ::Float::NAN}, output
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,7 +3,7 @@ require "models/task"
|
|||
|
||||
module ActiveRecord
|
||||
module Type
|
||||
class IntegerTest < ActiveRecord::TestCase
|
||||
class DateTimeTest < ActiveRecord::TestCase
|
||||
def test_datetime_seconds_precision_applied_to_timestamp
|
||||
skip "This test is invalid if subsecond precision isn't supported" unless subsecond_precision_supported?
|
||||
p = Task.create!(starting: ::Time.now)
|
||||
|
|
Loading…
Reference in a new issue