Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
99820fbe
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,发现更多精彩内容 >>
提交
99820fbe
编写于
1月 29, 2017
作者:
K
Kir Shatrov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Report the attribute on ActiveRecord::SerializationTypeMismatch
上级
20107ff6
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
34 addition
and
17 deletion
+34
-17
activerecord/lib/active_record/attribute_methods/serialization.rb
...cord/lib/active_record/attribute_methods/serialization.rb
+1
-1
activerecord/lib/active_record/coders/yaml_column.rb
activerecord/lib/active_record/coders/yaml_column.rb
+3
-2
activerecord/lib/active_record/store.rb
activerecord/lib/active_record/store.rb
+3
-3
activerecord/test/cases/coders/yaml_column_test.rb
activerecord/test/cases/coders/yaml_column_test.rb
+13
-11
activerecord/test/cases/serialized_attribute_test.rb
activerecord/test/cases/serialized_attribute_test.rb
+14
-0
未找到文件。
activerecord/lib/active_record/attribute_methods/serialization.rb
浏览文件 @
99820fbe
...
...
@@ -54,7 +54,7 @@ def serialize(attr_name, class_name_or_coder = Object)
elsif
[
:load
,
:dump
].
all?
{
|
x
|
class_name_or_coder
.
respond_to?
(
x
)
}
class_name_or_coder
else
Coders
::
YAMLColumn
.
new
(
class_name_or_coder
)
Coders
::
YAMLColumn
.
new
(
attr_name
,
class_name_or_coder
)
end
decorate_attribute_type
(
attr_name
,
:serialize
)
do
|
type
|
...
...
activerecord/lib/active_record/coders/yaml_column.rb
浏览文件 @
99820fbe
...
...
@@ -5,7 +5,8 @@ module Coders # :nodoc:
class
YAMLColumn
# :nodoc:
attr_accessor
:object_class
def
initialize
(
object_class
=
Object
)
def
initialize
(
attr_name
,
object_class
=
Object
)
@attr_name
=
attr_name
@object_class
=
object_class
check_arity_of_constructor
end
...
...
@@ -31,7 +32,7 @@ def load(yaml)
def
assert_valid_value
(
obj
)
unless
obj
.
nil?
||
obj
.
is_a?
(
object_class
)
raise
SerializationTypeMismatch
,
"Attribute was supposed to be a
#{
object_class
}
, but was a
#{
obj
.
class
}
. --
#{
obj
.
inspect
}
"
"Attribute
`
#{
@attr_name
}
`
was supposed to be a
#{
object_class
}
, but was a
#{
obj
.
class
}
. --
#{
obj
.
inspect
}
"
end
end
...
...
activerecord/lib/active_record/store.rb
浏览文件 @
99820fbe
...
...
@@ -78,7 +78,7 @@ class << self
module
ClassMethods
def
store
(
store_attribute
,
options
=
{})
serialize
store_attribute
,
IndifferentCoder
.
new
(
options
[
:coder
])
serialize
store_attribute
,
IndifferentCoder
.
new
(
store_attribute
,
options
[
:coder
])
store_accessor
(
store_attribute
,
options
[
:accessors
])
if
options
.
has_key?
:accessors
end
...
...
@@ -177,12 +177,12 @@ def self.prepare(object, store_attribute)
end
class
IndifferentCoder
# :nodoc:
def
initialize
(
coder_or_class_name
)
def
initialize
(
attr_name
,
coder_or_class_name
)
@coder
=
if
coder_or_class_name
.
respond_to?
(
:load
)
&&
coder_or_class_name
.
respond_to?
(
:dump
)
coder_or_class_name
else
ActiveRecord
::
Coders
::
YAMLColumn
.
new
(
coder_or_class_name
||
Object
)
ActiveRecord
::
Coders
::
YAMLColumn
.
new
(
attr_name
,
coder_or_class_name
||
Object
)
end
end
...
...
activerecord/test/cases/coders/yaml_column_test.rb
浏览文件 @
99820fbe
...
...
@@ -5,46 +5,48 @@ module ActiveRecord
module
Coders
class
YAMLColumnTest
<
ActiveRecord
::
TestCase
def
test_initialize_takes_class
coder
=
YAMLColumn
.
new
(
Object
)
coder
=
YAMLColumn
.
new
(
"attr_name"
,
Object
)
assert_equal
Object
,
coder
.
object_class
end
def
test_type_mismatch_on_different_classes_on_dump
coder
=
YAMLColumn
.
new
(
Array
)
assert_raises
(
SerializationTypeMismatch
)
do
coder
=
YAMLColumn
.
new
(
"attr_name"
,
Array
)
error
=
assert_raises
(
SerializationTypeMismatch
)
do
coder
.
dump
(
"a"
)
end
assert_equal
%{Attribute `attr_name` was supposed to be a Array, but was a String. -- "a"}
,
error
.
to_s
end
def
test_type_mismatch_on_different_classes
coder
=
YAMLColumn
.
new
(
Array
)
assert_raises
(
SerializationTypeMismatch
)
do
coder
=
YAMLColumn
.
new
(
"attr_name"
,
Array
)
error
=
assert_raises
(
SerializationTypeMismatch
)
do
coder
.
load
"--- foo"
end
assert_equal
%{Attribute `attr_name` was supposed to be a Array, but was a String. -- "foo"}
,
error
.
to_s
end
def
test_nil_is_ok
coder
=
YAMLColumn
.
new
coder
=
YAMLColumn
.
new
(
"attr_name"
)
assert_nil
coder
.
load
"--- "
end
def
test_returns_new_with_different_class
coder
=
YAMLColumn
.
new
SerializationTypeMismatch
coder
=
YAMLColumn
.
new
(
"attr_name"
,
SerializationTypeMismatch
)
assert_equal
SerializationTypeMismatch
,
coder
.
load
(
"--- "
).
class
end
def
test_returns_string_unless_starts_with_dash
coder
=
YAMLColumn
.
new
coder
=
YAMLColumn
.
new
(
"attr_name"
)
assert_equal
"foo"
,
coder
.
load
(
"foo"
)
end
def
test_load_handles_other_classes
coder
=
YAMLColumn
.
new
coder
=
YAMLColumn
.
new
(
"attr_name"
)
assert_equal
[],
coder
.
load
([])
end
def
test_load_doesnt_swallow_yaml_exceptions
coder
=
YAMLColumn
.
new
coder
=
YAMLColumn
.
new
(
"attr_name"
)
bad_yaml
=
"--- {"
assert_raises
(
Psych
::
SyntaxError
)
do
coder
.
load
(
bad_yaml
)
...
...
@@ -52,7 +54,7 @@ def test_load_doesnt_swallow_yaml_exceptions
end
def
test_load_doesnt_handle_undefined_class_or_module
coder
=
YAMLColumn
.
new
coder
=
YAMLColumn
.
new
(
"attr_name"
)
missing_class_yaml
=
'--- !ruby/object:DoesNotExistAndShouldntEver {}\n'
assert_raises
(
ArgumentError
)
do
coder
.
load
(
missing_class_yaml
)
...
...
activerecord/test/cases/serialized_attribute_test.rb
浏览文件 @
99820fbe
...
...
@@ -240,6 +240,20 @@ def test_regression_serialized_default_on_text_column_with_null_false
assert_equal
[],
light
.
long_state
end
def
test_unexpected_serialized_type
Topic
.
serialize
:content
,
Hash
topic
=
Topic
.
create!
(
content:
{
zomg:
true
})
Topic
.
serialize
:content
,
Array
topic
.
reload
error
=
assert_raise
(
ActiveRecord
::
SerializationTypeMismatch
)
do
topic
.
content
end
expected
=
"Attribute `content` was supposed to be a Array, but was a Hash. -- {:zomg=>true}"
assert_equal
expected
,
error
.
to_s
end
def
test_serialized_column_should_unserialize_after_update_column
t
=
Topic
.
create
(
content:
"first"
)
assert_equal
(
"first"
,
t
.
content
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录