From 3b22df2350dd671436ca5b1e4de4af3df9a8cca2 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Sat, 7 Jun 2014 09:38:15 -0600 Subject: [PATCH] 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 --- .../attribute_methods/time_zone_conversion.rb | 12 +++++++++++- .../test/cases/adapters/postgresql/array_test.rb | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb index 44a679a550..c1c3987cf5 100644 --- a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb +++ b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb @@ -10,7 +10,17 @@ module ActiveRecord def 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 diff --git a/activerecord/test/cases/adapters/postgresql/array_test.rb b/activerecord/test/cases/adapters/postgresql/array_test.rb index c78c502478..1d431f1666 100644 --- a/activerecord/test/cases/adapters/postgresql/array_test.rb +++ b/activerecord/test/cases/adapters/postgresql/array_test.rb @@ -208,11 +208,13 @@ class PostgresqlArrayTest < ActiveRecord::TestCase record = PgArray.new(datetimes: [time_string]) assert_equal [time], record.datetimes + assert_equal ActiveSupport::TimeZone[tz], record.datetimes.first.time_zone record.save! record.reload assert_equal [time], record.datetimes + assert_equal ActiveSupport::TimeZone[tz], record.datetimes.first.time_zone end end