Merge pull request #15556 from sgrif/sg-time-zone-aware-arrays

Add array support when time zone aware attributes are enabled
This commit is contained in:
Yves Senn 2014-06-07 14:44:12 +02:00
commit 23a751c2e1
2 changed files with 29 additions and 1 deletions

@ -32,7 +32,7 @@ def define_method_attribute=(attr_name)
if create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
method_body, line = <<-EOV, __LINE__ + 1
def #{attr_name}=(time)
time_with_zone = time.respond_to?(:in_time_zone) ? time.in_time_zone : nil
time_with_zone = convert_value_to_time_zone(time)
previous_time = attribute_changed?("#{attr_name}") ? changed_attributes["#{attr_name}"] : read_attribute(:#{attr_name})
write_attribute(:#{attr_name}, time)
#{attr_name}_will_change! if previous_time != time_with_zone
@ -52,6 +52,18 @@ def create_time_zone_conversion_attribute?(name, column)
(:datetime == column.type)
end
end
private
def convert_value_to_time_zone(value)
if value.is_a?(Array)
value.map { |v| convert_value_to_time_zone(v) }
elsif value.respond_to?(:in_time_zone)
value.in_time_zone
else
nil
end
end
end
end
end

@ -12,6 +12,7 @@ def setup
@connection.create_table('pg_arrays') do |t|
t.string 'tags', array: true
t.integer 'ratings', array: true
t.datetime :datetimes, array: true
end
end
@column = PgArray.columns_hash['tags']
@ -195,6 +196,21 @@ def test_escaping
assert_equal tags, ar.tags
end
def test_datetime_with_timezone_awareness
with_timezone_config aware_attributes: true do
PgArray.reset_column_information
current_time = [Time.current]
record = PgArray.new(datetimes: current_time)
assert_equal current_time, record.datetimes
record.save!
record.reload
assert_equal current_time, record.datetimes
end
end
private
def assert_cycle field, array
# test creation