Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
c5be90e8
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,发现更多精彩内容 >>
提交
c5be90e8
编写于
3月 04, 2013
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #9467 from senny/9459_include_json_root_out_of_sync
`ActiveRecord::Base.include_root_in_json` is `false` by default.
上级
54f9b758
8c7d4018
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
60 addition
and
34 deletion
+60
-34
activerecord/lib/active_record/serialization.rb
activerecord/lib/active_record/serialization.rb
+1
-1
activerecord/test/cases/json_serialization_test.rb
activerecord/test/cases/json_serialization_test.rb
+55
-33
activerecord/test/cases/serialization_test.rb
activerecord/test/cases/serialization_test.rb
+4
-0
未找到文件。
activerecord/lib/active_record/serialization.rb
浏览文件 @
c5be90e8
...
@@ -5,7 +5,7 @@ module Serialization
...
@@ -5,7 +5,7 @@ module Serialization
include
ActiveModel
::
Serializers
::
JSON
include
ActiveModel
::
Serializers
::
JSON
included
do
included
do
self
.
include_root_in_json
=
tru
e
self
.
include_root_in_json
=
fals
e
end
end
def
serializable_hash
(
options
=
nil
)
def
serializable_hash
(
options
=
nil
)
...
...
activerecord/test/cases/json_serialization_test.rb
浏览文件 @
c5be90e8
...
@@ -6,7 +6,21 @@
...
@@ -6,7 +6,21 @@
require
'models/tag'
require
'models/tag'
require
'models/comment'
require
'models/comment'
module
JsonSerializationHelpers
private
def
set_include_root_in_json
(
value
)
original_root_in_json
=
ActiveRecord
::
Base
.
include_root_in_json
ActiveRecord
::
Base
.
include_root_in_json
=
value
yield
ensure
ActiveRecord
::
Base
.
include_root_in_json
=
original_root_in_json
end
end
class
JsonSerializationTest
<
ActiveRecord
::
TestCase
class
JsonSerializationTest
<
ActiveRecord
::
TestCase
include
JsonSerializationHelpers
class
NamespacedContact
<
Contact
class
NamespacedContact
<
Contact
column
:name
,
:string
column
:name
,
:string
end
end
...
@@ -23,20 +37,24 @@ def setup
...
@@ -23,20 +37,24 @@ def setup
end
end
def
test_should_demodulize_root_in_json
def
test_should_demodulize_root_in_json
@contact
=
NamespacedContact
.
new
:name
=>
'whatever'
set_include_root_in_json
(
true
)
do
json
=
@contact
.
to_json
@contact
=
NamespacedContact
.
new
name:
'whatever'
assert_match
%r{^
\{
"namespaced_contact":
\{
}
,
json
json
=
@contact
.
to_json
assert_match
%r{^
\{
"namespaced_contact":
\{
}
,
json
end
end
end
def
test_should_include_root_in_json
def
test_should_include_root_in_json
json
=
@contact
.
to_json
set_include_root_in_json
(
true
)
do
json
=
@contact
.
to_json
assert_match
%r{^
\{
"contact":
\{
}
,
json
assert_match
%r{"name":"Konata Izumi"}
,
json
assert_match
%r{^
\{
"contact":
\{
}
,
json
assert_match
%r{"age":16}
,
json
assert_match
%r{"name":"Konata Izumi"}
,
json
assert
json
.
include?
(
%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})
)
assert_match
%r{"age":16}
,
json
assert_match
%r{"awesome":true}
,
json
assert
json
.
include?
(
%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})
)
assert_match
%r{"preferences":
\{
"shows":"anime"
\}
}
,
json
assert_match
%r{"awesome":true}
,
json
assert_match
%r{"preferences":
\{
"shows":"anime"
\}
}
,
json
end
end
end
def
test_should_encode_all_encodable_attributes
def
test_should_encode_all_encodable_attributes
...
@@ -141,6 +159,8 @@ def test_serializable_hash_should_not_modify_options_in_argument
...
@@ -141,6 +159,8 @@ def test_serializable_hash_should_not_modify_options_in_argument
class
DatabaseConnectedJsonEncodingTest
<
ActiveRecord
::
TestCase
class
DatabaseConnectedJsonEncodingTest
<
ActiveRecord
::
TestCase
fixtures
:authors
,
:posts
,
:comments
,
:tags
,
:taggings
fixtures
:authors
,
:posts
,
:comments
,
:tags
,
:taggings
include
JsonSerializationHelpers
def
setup
def
setup
@david
=
authors
(
:david
)
@david
=
authors
(
:david
)
@mary
=
authors
(
:mary
)
@mary
=
authors
(
:mary
)
...
@@ -227,23 +247,21 @@ def @david.favorite_quote; "Constraints are liberating"; end
...
@@ -227,23 +247,21 @@ def @david.favorite_quote; "Constraints are liberating"; end
end
end
def
test_should_allow_only_option_for_list_of_authors
def
test_should_allow_only_option_for_list_of_authors
ActiveRecord
::
Base
.
include_root_in_json
=
false
set_include_root_in_json
(
false
)
do
authors
=
[
@david
,
@mary
]
authors
=
[
@david
,
@mary
]
assert_equal
%([{"name":"David"},{"name":"Mary"}])
,
ActiveSupport
::
JSON
.
encode
(
authors
,
:only
=>
:name
)
assert_equal
%([{"name":"David"},{"name":"Mary"}])
,
ActiveSupport
::
JSON
.
encode
(
authors
,
only: :name
)
ensure
end
ActiveRecord
::
Base
.
include_root_in_json
=
true
end
end
def
test_should_allow_except_option_for_list_of_authors
def
test_should_allow_except_option_for_list_of_authors
ActiveRecord
::
Base
.
include_root_in_json
=
false
set_include_root_in_json
(
false
)
do
authors
=
[
@david
,
@mary
]
authors
=
[
@david
,
@mary
]
encoded
=
ActiveSupport
::
JSON
.
encode
(
authors
,
:except
=>
[
encoded
=
ActiveSupport
::
JSON
.
encode
(
authors
,
except:
[
:name
,
:author_address_id
,
:author_address_extra_id
,
:name
,
:author_address_id
,
:author_address_extra_id
,
:organization_id
,
:owned_essay_id
:organization_id
,
:owned_essay_id
])
])
assert_equal
%([{"id":1},{"id":2}])
,
encoded
assert_equal
%([{"id":1},{"id":2}])
,
encoded
ensure
end
ActiveRecord
::
Base
.
include_root_in_json
=
true
end
end
def
test_should_allow_includes_for_list_of_authors
def
test_should_allow_includes_for_list_of_authors
...
@@ -262,17 +280,21 @@ def test_should_allow_includes_for_list_of_authors
...
@@ -262,17 +280,21 @@ def test_should_allow_includes_for_list_of_authors
end
end
def
test_should_allow_options_for_hash_of_authors
def
test_should_allow_options_for_hash_of_authors
authors_hash
=
{
set_include_root_in_json
(
true
)
do
1
=>
@david
,
authors_hash
=
{
2
=>
@mary
1
=>
@david
,
}
2
=>
@mary
assert_equal
%({"1":{"author":{"name":"David"}}})
,
ActiveSupport
::
JSON
.
encode
(
authors_hash
,
:only
=>
[
1
,
:name
])
}
assert_equal
%({"1":{"author":{"name":"David"}}})
,
ActiveSupport
::
JSON
.
encode
(
authors_hash
,
only:
[
1
,
:name
])
end
end
end
def
test_should_be_able_to_encode_relation
def
test_should_be_able_to_encode_relation
authors_relation
=
Author
.
where
(
:id
=>
[
@david
.
id
,
@mary
.
id
])
set_include_root_in_json
(
true
)
do
authors_relation
=
Author
.
where
(
id:
[
@david
.
id
,
@mary
.
id
])
json
=
ActiveSupport
::
JSON
.
encode
authors_relation
,
:only
=>
:name
json
=
ActiveSupport
::
JSON
.
encode
authors_relation
,
only: :name
assert_equal
'[{"author":{"name":"David"}},{"author":{"name":"Mary"}}]'
,
json
assert_equal
'[{"author":{"name":"David"}},{"author":{"name":"Mary"}}]'
,
json
end
end
end
end
end
activerecord/test/cases/serialization_test.rb
浏览文件 @
c5be90e8
...
@@ -18,6 +18,10 @@ def setup
...
@@ -18,6 +18,10 @@ def setup
}
}
end
end
def
test_include_root_in_json_is_false_by_default
assert_equal
false
,
ActiveRecord
::
Base
.
include_root_in_json
,
"include_root_in_json should be false by default but was not"
end
def
test_serialize_should_be_reversible
def
test_serialize_should_be_reversible
FORMATS
.
each
do
|
format
|
FORMATS
.
each
do
|
format
|
@serialized
=
Contact
.
new
.
send
(
"to_
#{
format
}
"
)
@serialized
=
Contact
.
new
.
send
(
"to_
#{
format
}
"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录