Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
978b3d60
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,体验更适合开发者的 AI 搜索 >>
提交
978b3d60
编写于
8月 04, 2017
作者:
S
Sean Griffin
提交者:
GitHub
8月 04, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #29520 from kirs/serialize-vs-postgres-native-column
Do not let use `serialize` on native JSON/array column
上级
6453b0e5
a0751d27
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
42 addition
and
0 deletion
+42
-0
activerecord/lib/active_record/attribute_methods/serialization.rb
...cord/lib/active_record/attribute_methods/serialization.rb
+24
-0
activerecord/test/cases/adapters/postgresql/array_test.rb
activerecord/test/cases/adapters/postgresql/array_test.rb
+9
-0
activerecord/test/cases/adapters/postgresql/json_test.rb
activerecord/test/cases/adapters/postgresql/json_test.rb
+9
-0
未找到文件。
activerecord/lib/active_record/attribute_methods/serialization.rb
浏览文件 @
978b3d60
...
...
@@ -5,6 +5,16 @@ module AttributeMethods
module
Serialization
extend
ActiveSupport
::
Concern
class
ColumnNotSerializableError
<
StandardError
def
initialize
(
name
,
type
)
super
<<-
EOS
.
strip_heredoc
Column `
#{
name
}
` of type
#{
type
.
class
}
does not support `serialize` feature.
Usually it means that you are trying to use `serialize`
on a column that already implements serialization natively.
EOS
end
end
module
ClassMethods
# If you have an attribute that needs to be saved to the database as an
# object, and retrieved as the same object, then specify the name of that
...
...
@@ -60,9 +70,23 @@ def serialize(attr_name, class_name_or_coder = Object)
end
decorate_attribute_type
(
attr_name
,
:serialize
)
do
|
type
|
if
type_incompatible_with_serialize?
(
type
)
raise
ColumnNotSerializableError
.
new
(
attr_name
,
type
)
end
Type
::
Serialized
.
new
(
type
,
coder
)
end
end
private
def
type_incompatible_with_serialize?
(
type
)
type
.
is_a?
(
ActiveRecord
::
Type
::
Json
)
||
(
defined?
(
ActiveRecord
::
ConnectionAdapters
::
PostgreSQL
)
&&
type
.
is_a?
(
ActiveRecord
::
ConnectionAdapters
::
PostgreSQL
::
OID
::
Array
)
)
end
end
end
end
...
...
activerecord/test/cases/adapters/postgresql/array_test.rb
浏览文件 @
978b3d60
...
...
@@ -47,6 +47,15 @@ def test_column
assert
ratings_column
.
array?
end
def
test_not_compatible_with_serialize
new_klass
=
Class
.
new
(
PgArray
)
do
serialize
:tags
,
Array
end
assert_raises
(
ActiveRecord
::
AttributeMethods
::
Serialization
::
ColumnNotSerializableError
)
do
new_klass
.
new
end
end
def
test_default
@connection
.
add_column
"pg_arrays"
,
"score"
,
:integer
,
array:
true
,
default:
[
4
,
4
,
2
]
PgArray
.
reset_column_information
...
...
activerecord/test/cases/adapters/postgresql/json_test.rb
浏览文件 @
978b3d60
...
...
@@ -33,6 +33,15 @@ def test_deserialize_with_array
x
.
reload
assert_equal
[
"foo"
=>
"bar"
],
x
.
objects
end
def
test_not_compatible_with_serialize_macro
new_klass
=
Class
.
new
(
klass
)
do
serialize
:payload
,
JSON
end
assert_raises
(
ActiveRecord
::
AttributeMethods
::
Serialization
::
ColumnNotSerializableError
)
do
new_klass
.
new
end
end
end
class
PostgresqlJSONTest
<
ActiveRecord
::
PostgreSQLTestCase
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录