Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
b814d8c9
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b814d8c9
编写于
1月 15, 2015
作者:
S
Sean Griffin
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #15726 from sgrif/sg-time-zone-aware-times
Time columns should support time zone aware attributes
上级
3f96b697
5cd3bbbb
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
116 addition
and
8 deletion
+116
-8
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+14
-0
activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
...b/active_record/attribute_methods/time_zone_conversion.rb
+29
-4
activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb
...active_record/connection_adapters/postgresql/oid/array.rb
+1
-1
activerecord/lib/active_record/type/time.rb
activerecord/lib/active_record/type/time.rb
+13
-0
activerecord/lib/active_record/type/time_value.rb
activerecord/lib/active_record/type/time_value.rb
+4
-0
activerecord/test/cases/attribute_methods_test.rb
activerecord/test/cases/attribute_methods_test.rb
+50
-1
activerecord/test/cases/helper.rb
activerecord/test/cases/helper.rb
+3
-0
activerecord/test/cases/multiparameter_attributes_test.rb
activerecord/test/cases/multiparameter_attributes_test.rb
+2
-2
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
b814d8c9
*
`time`
columns can now affected by
`time_zone_aware_attributes`
. If you have
set
`config.time_zone`
to a value other than
`'UTC'`
, they will be treated
as in that time zone by default in Rails 5.0. If this is not the desired
behavior, you can set
ActiveRecord::Base.time_zone_aware_types = [:datetime]
A deprecation warning will be emitted if you have a `:time` column, and have
not explicitly opted out.
Fixes #3145
*Sean Griffin*
*
`nil`
as a value for a binary column in a query no longer logs as
"
<NULL
binary
data
>
", and instead logs as just "nil".
...
...
activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
浏览文件 @
b814d8c9
...
...
@@ -13,7 +13,7 @@ def type_cast_from_user(value)
value
.
map
{
|
v
|
type_cast_from_user
(
v
)
}
elsif
value
.
respond_to?
(
:in_time_zone
)
begin
value
.
in_time_zone
||
super
user_input_in_time_zone
(
value
)
||
super
rescue
ArgumentError
nil
end
...
...
@@ -39,6 +39,9 @@ def convert_time_to_time_zone(value)
class_attribute
:skip_time_zone_conversion_for_attributes
,
instance_writer:
false
self
.
skip_time_zone_conversion_for_attributes
=
[]
class_attribute
:time_zone_aware_types
,
instance_writer:
false
self
.
time_zone_aware_types
=
[
:datetime
,
:not_explicitly_configured
]
end
module
ClassMethods
...
...
@@ -59,9 +62,31 @@ def inherited(subclass)
end
def
create_time_zone_conversion_attribute?
(
name
,
cast_type
)
time_zone_aware_attributes
&&
!
self
.
skip_time_zone_conversion_for_attributes
.
include?
(
name
.
to_sym
)
&&
(
:datetime
==
cast_type
.
type
)
enabled_for_column
=
time_zone_aware_attributes
&&
!
self
.
skip_time_zone_conversion_for_attributes
.
include?
(
name
.
to_sym
)
result
=
enabled_for_column
&&
time_zone_aware_types
.
include?
(
cast_type
.
type
)
if
enabled_for_column
&&
!
result
&&
cast_type
.
type
==
:time
&&
time_zone_aware_types
.
include?
(
:not_explicitly_configured
)
ActiveSupport
::
Deprecation
.
warn
(
<<-
MESSAGE
)
Time columns will become time zone aware in Rails 5.1. This
sill cause `String`s to be parsed as if they were in `Time.zone`,
and `Time`s to be converted to `Time.zone`.
To keep the old behavior, you must add the following to your initializer:
config.active_record.time_zone_aware_types = [:datetime]
To silence this deprecation warning, add the following:
config.active_record.time_zone_aware_types << :time
MESSAGE
end
result
end
end
end
...
...
activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb
浏览文件 @
b814d8c9
...
...
@@ -18,7 +18,7 @@ class Array < Type::Value # :nodoc:
end
attr_reader
:subtype
,
:delimiter
delegate
:type
,
to: :subtype
delegate
:type
,
:user_input_in_time_zone
,
to: :subtype
def
initialize
(
subtype
,
delimiter
=
','
)
@subtype
=
subtype
...
...
activerecord/lib/active_record/type/time.rb
浏览文件 @
b814d8c9
...
...
@@ -7,6 +7,19 @@ def type
:time
end
def
user_input_in_time_zone
(
value
)
return
unless
value
.
present?
case
value
when
::
String
value
=
"2000-01-01
#{
value
}
"
when
::
Time
value
=
value
.
change
(
year:
2000
,
day:
1
,
month:
1
)
end
super
(
value
)
end
private
def
cast_value
(
value
)
...
...
activerecord/lib/active_record/type/time_value.rb
浏览文件 @
b814d8c9
...
...
@@ -9,6 +9,10 @@ def type_cast_for_schema(value)
"'
#{
value
.
to_s
(
:db
)
}
'"
end
def
user_input_in_time_zone
(
value
)
value
.
in_time_zone
end
private
def
new_time
(
year
,
mon
,
mday
,
hour
,
min
,
sec
,
microsec
,
offset
=
nil
)
...
...
activerecord/test/cases/attribute_methods_test.rb
浏览文件 @
b814d8c9
...
...
@@ -657,7 +657,7 @@ def test_setting_time_zone_aware_attribute_interprets_time_zone_unaware_string_i
end
end
def
test_setting_time_zone_aware_
attribut
e_in_current_time_zone
def
test_setting_time_zone_aware_
datetim
e_in_current_time_zone
utc_time
=
Time
.
utc
(
2008
,
1
,
1
)
in_time_zone
"Pacific Time (US & Canada)"
do
record
=
@target
.
new
...
...
@@ -676,6 +676,47 @@ def test_yaml_dumping_record_with_time_zone_aware_attribute
end
end
def
test_setting_time_zone_aware_time_in_current_time_zone
in_time_zone
"Pacific Time (US & Canada)"
do
record
=
@target
.
new
time_string
=
"10:00:00"
expected_time
=
Time
.
zone
.
parse
(
"2000-01-01
#{
time_string
}
"
)
record
.
bonus_time
=
time_string
assert_equal
expected_time
,
record
.
bonus_time
assert_equal
ActiveSupport
::
TimeZone
[
"Pacific Time (US & Canada)"
],
record
.
bonus_time
.
time_zone
record
.
bonus_time
=
''
assert_nil
record
.
bonus_time
end
end
def
test_setting_time_zone_aware_time_with_dst
in_time_zone
"Pacific Time (US & Canada)"
do
current_time
=
Time
.
zone
.
local
(
2014
,
06
,
15
,
10
)
record
=
@target
.
new
(
bonus_time:
current_time
)
time_before_save
=
record
.
bonus_time
record
.
save
record
.
reload
assert_equal
time_before_save
,
record
.
bonus_time
assert_equal
ActiveSupport
::
TimeZone
[
"Pacific Time (US & Canada)"
],
record
.
bonus_time
.
time_zone
end
end
def
test_removing_time_zone_aware_types
with_time_zone_aware_types
(
:datetime
)
do
in_time_zone
"Pacific Time (US & Canada)"
do
record
=
@target
.
new
(
bonus_time:
"10:00:00"
)
expected_time
=
Time
.
utc
(
2000
,
01
,
01
,
10
)
assert_equal
expected_time
,
record
.
bonus_time
assert
record
.
bonus_time
.
utc?
end
end
end
def
test_setting_time_zone_conversion_for_attributes_should_write_value_on_class_variable
Topic
.
skip_time_zone_conversion_for_attributes
=
[
:field_a
]
Minimalistic
.
skip_time_zone_conversion_for_attributes
=
[
:field_b
]
...
...
@@ -908,6 +949,14 @@ def new_topic_like_ar_class(&block)
klass
end
def
with_time_zone_aware_types
(
*
types
)
old_types
=
ActiveRecord
::
Base
.
time_zone_aware_types
ActiveRecord
::
Base
.
time_zone_aware_types
=
types
yield
ensure
ActiveRecord
::
Base
.
time_zone_aware_types
=
old_types
end
def
cached_columns
Topic
.
columns
.
map
(
&
:name
)
end
...
...
activerecord/test/cases/helper.rb
浏览文件 @
b814d8c9
...
...
@@ -30,6 +30,9 @@
# Quote "type" if it's a reserved word for the current connection.
QUOTED_TYPE
=
ActiveRecord
::
Base
.
connection
.
quote_column_name
(
'type'
)
# FIXME: Remove this when the deprecation cycle on TZ aware types by default ends.
ActiveRecord
::
Base
.
time_zone_aware_types
<<
:time
def
current_adapter?
(
*
types
)
types
.
any?
do
|
type
|
ActiveRecord
::
ConnectionAdapters
.
const_defined?
(
type
)
&&
...
...
activerecord/test/cases/multiparameter_attributes_test.rb
浏览文件 @
b814d8c9
...
...
@@ -250,8 +250,8 @@ def test_multiparameter_attributes_on_time_only_column_with_time_zone_aware_attr
}
topic
=
Topic
.
find
(
1
)
topic
.
attributes
=
attributes
assert_equal
Time
.
utc
(
2000
,
1
,
1
,
16
,
24
,
0
),
topic
.
bonus_time
assert
topic
.
bonus_time
.
utc?
assert_equal
Time
.
zone
.
local
(
2000
,
1
,
1
,
16
,
24
,
0
),
topic
.
bonus_time
assert
_not
topic
.
bonus_time
.
utc?
end
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录