提交 9c172b29 编写于 作者: J Jon Leighton

Fix #3837.

We also need to time zone convert time zone aware attributes when
accessed via read_attribute, not only when via direct access.
上级 de24ed9f
......@@ -16,22 +16,16 @@ module TimeZoneConversion
module ClassMethods
protected
# Defined for all +datetime+ and +timestamp+ attributes when +time_zone_aware_attributes+ are enabled.
# This enhanced read method automatically converts the UTC time stored in the database to the time
# The enhanced read method automatically converts the UTC time stored in the database to the time
# zone stored in Time.zone.
def internal_attribute_access_code(attr_name, cast_code)
def attribute_cast_code(attr_name)
column = columns_hash[attr_name]
if create_time_zone_conversion_attribute?(attr_name, column)
super(attr_name, "(v=#{column.type_cast_code('v')}) && #{cast_code}")
else
super
end
end
typecast = "v = #{super}"
time_zone_conversion = "v.acts_like?(:time) ? v.in_time_zone : v"
def attribute_cast_code(attr_name)
if create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
"(v.acts_like?(:time) ? v.in_time_zone : v)"
"((#{typecast}) && (#{time_zone_conversion}))"
else
super
end
......
......@@ -553,6 +553,17 @@ def test_setting_time_zone_aware_attribute_in_other_time_zone
end
end
def test_setting_time_zone_aware_read_attribute
utc_time = Time.utc(2008, 1, 1)
cst_time = utc_time.in_time_zone("Central Time (US & Canada)")
in_time_zone "Pacific Time (US & Canada)" do
record = @target.create(:written_on => cst_time).reload
assert_equal utc_time, record[:written_on]
assert_equal ActiveSupport::TimeZone["Pacific Time (US & Canada)"], record[:written_on].time_zone
assert_equal Time.utc(2007, 12, 31, 16), record[:written_on].time
end
end
def test_setting_time_zone_aware_attribute_with_string
utc_time = Time.utc(2008, 1, 1)
(-11..13).each do |timezone_offset|
......@@ -572,6 +583,7 @@ def test_setting_time_zone_aware_attribute_to_blank_string_returns_nil
record = @target.new
record.written_on = ' '
assert_nil record.written_on
assert_nil record[:written_on]
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册