1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Do not rely on the column type when quoting infinity

This commit is contained in:
Sean Griffin 2014-07-03 07:33:54 -06:00
parent 03c9c0e2fa
commit 81b718728e
3 changed files with 46 additions and 3 deletions

View file

@ -7,6 +7,7 @@ module ActiveRecord
def cast_value(value)
case value
when ::Float then value
when 'Infinity' then ::Float::INFINITY
when '-Infinity' then -::Float::INFINITY
when 'NaN' then ::Float::NAN

View file

@ -34,9 +34,7 @@ module ActiveRecord
else super
end
when Float
if value.infinite? && column.type == :datetime
"'#{value.to_s.downcase}'"
elsif value.infinite? || value.nan?
if value.infinite? || value.nan?
"'#{value.to_s}'"
else
super

View file

@ -0,0 +1,44 @@
require "cases/helper"
class PostgresqlInfinityTest < ActiveRecord::TestCase
class PostgresqlInfinity < ActiveRecord::Base
end
setup do
@connection = ActiveRecord::Base.connection
@connection.create_table(:postgresql_infinities) do |t|
t.float :float
t.datetime :datetime
end
end
teardown do
@connection.execute("DROP TABLE IF EXISTS postgresql_infinities")
end
test "type casting infinity on a float column" do
record = PostgresqlInfinity.create!(float: Float::INFINITY)
record.reload
assert_equal Float::INFINITY, record.float
end
test "update_all with infinity on a float column" do
record = PostgresqlInfinity.create!
PostgresqlInfinity.update_all(float: Float::INFINITY)
record.reload
assert_equal Float::INFINITY, record.float
end
test "type casting infinity on a datetime column" do
record = PostgresqlInfinity.create!(datetime: Float::INFINITY)
record.reload
assert_equal Float::INFINITY, record.datetime
end
test "update_all with infinity on a datetime column" do
record = PostgresqlInfinity.create!
PostgresqlInfinity.update_all(datetime: Float::INFINITY)
record.reload
assert_equal Float::INFINITY, record.datetime
end
end