Created
June 6, 2014 17:09
-
-
Save sgrif/aaae4c4295ef6013cf2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 1877869..57f57b6 100644 | |
--- a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb | |
+++ b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb | |
@@ -32,7 +32,7 @@ module ActiveRecord | |
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 | |
@@ -46,12 +46,25 @@ module ActiveRecord | |
end | |
private | |
+ | |
def create_time_zone_conversion_attribute?(name, column) | |
time_zone_aware_attributes && | |
!self.skip_time_zone_conversion_for_attributes.include?(name.to_sym) && | |
(:datetime == column.type) | |
end | |
end | |
+ | |
+ private | |
+ | |
+ def convert_value_to_time_zone(value) | |
+ if value.is_a?(Array) | |
+ value.map(&method(:convert_value_to_time_zone)) | |
+ elsif value.respond_to?(:in_time_zone) | |
+ value.in_time_zone | |
+ else | |
+ nil | |
+ end | |
+ end | |
end | |
end | |
end | |
diff --git a/activerecord/test/cases/adapters/postgresql/array_test.rb b/activerecord/test/cases/adapters/postgresql/array_test.rb | |
index e03d83d..90b7256 100644 | |
--- a/activerecord/test/cases/adapters/postgresql/array_test.rb | |
+++ b/activerecord/test/cases/adapters/postgresql/array_test.rb | |
@@ -12,6 +12,7 @@ class PostgresqlArrayTest < ActiveRecord::TestCase | |
@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 @@ class PostgresqlArrayTest < ActiveRecord::TestCase | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment