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

Ensure time zones don't change after round trip with array columns

The times would be equivalent, even if they were in different time
zones. E.g. 12:00 UTC == 5:00 PDT
This commit is contained in:
Sean Griffin 2014-06-07 09:38:15 -06:00
parent 2f5fab8d5a
commit 3b22df2350
2 changed files with 13 additions and 1 deletions

View file

@ -10,7 +10,17 @@ module ActiveRecord
def type_cast(value) def type_cast(value)
value = @column.type_cast(value) value = @column.type_cast(value)
value.acts_like?(:time) ? value.in_time_zone : value convert_value_to_time_zone(value)
end
def convert_value_to_time_zone(value)
if value.is_a?(Array)
value.map { |v| convert_value_to_time_zone(v) }
elsif value.acts_like?(:time)
value.in_time_zone
else
value
end
end end
end end

View file

@ -208,11 +208,13 @@ class PostgresqlArrayTest < ActiveRecord::TestCase
record = PgArray.new(datetimes: [time_string]) record = PgArray.new(datetimes: [time_string])
assert_equal [time], record.datetimes assert_equal [time], record.datetimes
assert_equal ActiveSupport::TimeZone[tz], record.datetimes.first.time_zone
record.save! record.save!
record.reload record.reload
assert_equal [time], record.datetimes assert_equal [time], record.datetimes
assert_equal ActiveSupport::TimeZone[tz], record.datetimes.first.time_zone
end end
end end