active_model_serializers 0.10.0.rc1 → 0.10.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -2
- data/Gemfile +1 -1
- data/README.md +6 -3
- data/lib/action_controller/serialization.rb +9 -1
- data/lib/active_model/serializer.rb +15 -21
- data/lib/active_model/serializer/adapter.rb +13 -4
- data/lib/active_model/serializer/adapter/flatten_json.rb +12 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +5 -5
- data/lib/active_model/serializer/adapter/json.rb +8 -10
- data/lib/active_model/serializer/adapter/json_api.rb +34 -30
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +3 -2
- data/lib/active_model/serializer/array_serializer.rb +8 -5
- data/lib/active_model/serializer/configuration.rb +1 -1
- data/lib/active_model/serializer/fieldset.rb +2 -2
- data/lib/active_model/serializer/railtie.rb +8 -0
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +1 -0
- data/lib/generators/serializer/resource_override.rb +12 -0
- data/test/action_controller/adapter_selector_test.rb +7 -5
- data/test/action_controller/explicit_serializer_test.rb +33 -9
- data/test/action_controller/json_api_linked_test.rb +25 -19
- data/test/action_controller/rescue_from_test.rb +32 -0
- data/test/action_controller/serialization_scope_name_test.rb +2 -2
- data/test/action_controller/serialization_test.rb +41 -23
- data/test/adapter/fragment_cache_test.rb +1 -1
- data/test/adapter/json/belongs_to_test.rb +9 -2
- data/test/adapter/json/collection_test.rb +16 -2
- data/test/adapter/json/has_many_test.rb +1 -1
- data/test/adapter/json_api/belongs_to_test.rb +45 -35
- data/test/adapter/json_api/collection_test.rb +30 -23
- data/test/adapter/json_api/has_many_embed_ids_test.rb +2 -2
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +14 -14
- data/test/adapter/json_api/has_many_test.rb +22 -18
- data/test/adapter/json_api/has_one_test.rb +8 -6
- data/test/adapter/json_api/linked_test.rb +97 -71
- data/test/adapter/json_test.rb +1 -1
- data/test/adapter_test.rb +1 -1
- data/test/array_serializer_test.rb +14 -0
- data/test/fixtures/poro.rb +31 -7
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/{serializers/generators_test.rb → generators/serializer_generator_test.rb} +2 -10
- data/test/serializers/adapter_for_test.rb +1 -1
- data/test/serializers/associations_test.rb +21 -0
- data/test/serializers/attribute_test.rb +16 -1
- data/test/serializers/attributes_test.rb +35 -0
- data/test/serializers/cache_test.rb +14 -4
- data/test/serializers/configuration_test.rb +1 -1
- data/test/serializers/meta_test.rb +38 -9
- data/test/serializers/serializer_for_test.rb +9 -0
- data/test/test_helper.rb +10 -7
- metadata +12 -5
@@ -8,7 +8,7 @@ module ActiveModel
|
|
8
8
|
@role = Role.new(name: 'Great Author', description:nil)
|
9
9
|
@role.author = [@author]
|
10
10
|
@role_serializer = RoleSerializer.new(@role)
|
11
|
-
@role_hash = FragmentCache.new(RoleSerializer.adapter.new(@role_serializer), @role_serializer, {}
|
11
|
+
@role_hash = FragmentCache.new(RoleSerializer.adapter.new(@role_serializer), @role_serializer, {})
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_fragment_fetch_with_virtual_attributes
|
@@ -25,14 +25,21 @@ module ActiveModel
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_includes_post
|
28
|
-
assert_equal({id: 42, title: 'New Post', body: 'Body'}, @adapter.serializable_hash[:post])
|
28
|
+
assert_equal({id: 42, title: 'New Post', body: 'Body'}, @adapter.serializable_hash[:comment][:post])
|
29
29
|
end
|
30
30
|
|
31
31
|
def test_include_nil_author
|
32
32
|
serializer = PostSerializer.new(@anonymous_post)
|
33
33
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
34
34
|
|
35
|
-
assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: {id: 999, name: "Custom blog"}, author: nil}, adapter.serializable_hash)
|
35
|
+
assert_equal({post: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: {id: 999, name: "Custom blog"}, author: nil}}, adapter.serializable_hash)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_include_nil_author_with_specified_serializer
|
39
|
+
serializer = PostPreviewSerializer.new(@anonymous_post)
|
40
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
41
|
+
|
42
|
+
assert_equal({posts: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}}, adapter.serializable_hash)
|
36
43
|
end
|
37
44
|
end
|
38
45
|
end
|
@@ -22,8 +22,22 @@ module ActiveModel
|
|
22
22
|
ActionController::Base.cache_store.clear
|
23
23
|
end
|
24
24
|
|
25
|
+
def test_with_serializer_option
|
26
|
+
@blog.special_attribute = "Special"
|
27
|
+
@blog.articles = [@first_post, @second_post]
|
28
|
+
@serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
|
29
|
+
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
30
|
+
|
31
|
+
expected = {custom_blogs:[{
|
32
|
+
id: 1,
|
33
|
+
special_attribute: "Special",
|
34
|
+
articles: [{id: 1,title: "Hello!!", body: "Hello, world!!"}, {id: 2, title: "New Post", body: "Body"}]
|
35
|
+
}]}
|
36
|
+
assert_equal expected, @adapter.serializable_hash
|
37
|
+
end
|
38
|
+
|
25
39
|
def test_include_multiple_posts
|
26
|
-
expected = [{
|
40
|
+
expected = { posts: [{
|
27
41
|
title: "Hello!!",
|
28
42
|
body: "Hello, world!!",
|
29
43
|
id: 1,
|
@@ -49,7 +63,7 @@ module ActiveModel
|
|
49
63
|
id: 999,
|
50
64
|
name: "Custom blog"
|
51
65
|
}
|
52
|
-
}]
|
66
|
+
}]}
|
53
67
|
assert_equal expected, @adapter.serializable_hash
|
54
68
|
end
|
55
69
|
end
|
@@ -32,9 +32,9 @@ module ActiveModel
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def test_includes_post_id
|
35
|
-
expected = {
|
35
|
+
expected = { data: { type: "posts", id: "42" } }
|
36
36
|
|
37
|
-
assert_equal(expected, @adapter.serializable_hash[:data][:
|
37
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:post])
|
38
38
|
end
|
39
39
|
|
40
40
|
def test_includes_linked_post
|
@@ -42,12 +42,14 @@ module ActiveModel
|
|
42
42
|
expected = [{
|
43
43
|
id: "42",
|
44
44
|
type: "posts",
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
45
|
+
attributes: {
|
46
|
+
title: 'New Post',
|
47
|
+
body: 'Body',
|
48
|
+
},
|
49
|
+
relationships: {
|
50
|
+
comments: { data: [ { type: "comments", id: "1" } ] },
|
51
|
+
blog: { data: { type: "blogs", id: "999" } },
|
52
|
+
author: { data: { type: "authors", id: "1" } }
|
51
53
|
}
|
52
54
|
}]
|
53
55
|
assert_equal expected, @adapter.serializable_hash[:included]
|
@@ -58,11 +60,13 @@ module ActiveModel
|
|
58
60
|
expected = [{
|
59
61
|
id: "42",
|
60
62
|
type: "posts",
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
63
|
+
attributes: {
|
64
|
+
title: 'New Post'
|
65
|
+
},
|
66
|
+
relationships: {
|
67
|
+
comments: { data: [ { type: "comments", id: "1" } ] },
|
68
|
+
blog: { data: { type: "blogs", id: "999" } },
|
69
|
+
author: { data: { type: "authors", id: "1" } }
|
66
70
|
}
|
67
71
|
}]
|
68
72
|
assert_equal expected, @adapter.serializable_hash[:included]
|
@@ -72,22 +76,22 @@ module ActiveModel
|
|
72
76
|
serializer = PostSerializer.new(@anonymous_post)
|
73
77
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
74
78
|
|
75
|
-
assert_equal({comments: {
|
79
|
+
assert_equal({comments: { data: [] }, blog: { data: { type: "blogs", id: "999" } }, author: { data: nil }}, adapter.serializable_hash[:data][:relationships])
|
76
80
|
end
|
77
81
|
|
78
82
|
def test_include_type_for_association_when_different_than_name
|
79
83
|
serializer = BlogSerializer.new(@blog)
|
80
84
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
81
|
-
|
85
|
+
relationships = adapter.serializable_hash[:data][:relationships]
|
82
86
|
expected = {
|
83
87
|
writer: {
|
84
|
-
|
88
|
+
data: {
|
85
89
|
type: "authors",
|
86
90
|
id: "1"
|
87
91
|
}
|
88
92
|
},
|
89
93
|
articles: {
|
90
|
-
|
94
|
+
data: [
|
91
95
|
{
|
92
96
|
type: "posts",
|
93
97
|
id: "42"
|
@@ -99,7 +103,7 @@ module ActiveModel
|
|
99
103
|
]
|
100
104
|
}
|
101
105
|
}
|
102
|
-
assert_equal expected,
|
106
|
+
assert_equal expected, relationships
|
103
107
|
end
|
104
108
|
|
105
109
|
def test_include_linked_resources_with_type_name
|
@@ -110,31 +114,37 @@ module ActiveModel
|
|
110
114
|
{
|
111
115
|
id: "1",
|
112
116
|
type: "authors",
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
117
|
+
attributes: {
|
118
|
+
name: "Steve K."
|
119
|
+
},
|
120
|
+
relationships: {
|
121
|
+
posts: { data: [] },
|
122
|
+
roles: { data: [] },
|
123
|
+
bio: { data: nil }
|
118
124
|
}
|
119
125
|
},{
|
120
126
|
id: "42",
|
121
127
|
type: "posts",
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
+
attributes: {
|
129
|
+
title: "New Post",
|
130
|
+
body: "Body"
|
131
|
+
},
|
132
|
+
relationships: {
|
133
|
+
comments: { data: [ { type: "comments", id: "1" } ] },
|
134
|
+
blog: { data: { type: "blogs", id: "999" } },
|
135
|
+
author: { data: { type: "authors", id: "1" } }
|
128
136
|
}
|
129
137
|
}, {
|
130
138
|
id: "43",
|
131
139
|
type: "posts",
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
140
|
+
attributes: {
|
141
|
+
title: "Hello!!",
|
142
|
+
body: "Hello, world!!"
|
143
|
+
},
|
144
|
+
relationships: {
|
145
|
+
comments: { data: [] },
|
146
|
+
blog: { data: { type: "blogs", id: "999" } },
|
147
|
+
author: { data: nil }
|
138
148
|
}
|
139
149
|
}
|
140
150
|
]
|
@@ -29,23 +29,27 @@ module ActiveModel
|
|
29
29
|
{
|
30
30
|
id: "1",
|
31
31
|
type: "posts",
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
attributes: {
|
33
|
+
title: "Hello!!",
|
34
|
+
body: "Hello, world!!"
|
35
|
+
},
|
36
|
+
relationships: {
|
37
|
+
comments: { data: [] },
|
38
|
+
blog: { data: { type: "blogs", id: "999" } },
|
39
|
+
author: { data: { type: "authors", id: "1" } }
|
38
40
|
}
|
39
41
|
},
|
40
42
|
{
|
41
43
|
id: "2",
|
42
44
|
type: "posts",
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
45
|
+
attributes: {
|
46
|
+
title: "New Post",
|
47
|
+
body: "Body"
|
48
|
+
},
|
49
|
+
relationships: {
|
50
|
+
comments: { data: [] },
|
51
|
+
blog: { data: { type: "blogs", id: "999" } },
|
52
|
+
author: { data: { type: "authors", id: "1" } }
|
49
53
|
}
|
50
54
|
}
|
51
55
|
]
|
@@ -60,25 +64,28 @@ module ActiveModel
|
|
60
64
|
{
|
61
65
|
id: "1",
|
62
66
|
type: "posts",
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
67
|
+
attributes: {
|
68
|
+
title: "Hello!!"
|
69
|
+
},
|
70
|
+
relationships: {
|
71
|
+
comments: { data: [] },
|
72
|
+
blog: { data: { type: "blogs", id: "999" } },
|
73
|
+
author: { data: { type: "authors", id: "1" } }
|
68
74
|
}
|
69
75
|
},
|
70
76
|
{
|
71
77
|
id: "2",
|
72
78
|
type: "posts",
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
79
|
+
attributes: {
|
80
|
+
title: "New Post"
|
81
|
+
},
|
82
|
+
relationships: {
|
83
|
+
comments: { data: [] },
|
84
|
+
blog: { data: { type: "blogs", id: "999" } },
|
85
|
+
author: { data: { type: "authors", id: "1" } }
|
78
86
|
}
|
79
87
|
}
|
80
88
|
]
|
81
|
-
|
82
89
|
assert_equal(expected, @adapter.serializable_hash[:data])
|
83
90
|
end
|
84
91
|
|
@@ -26,13 +26,13 @@ module ActiveModel
|
|
26
26
|
|
27
27
|
def test_includes_comment_ids
|
28
28
|
expected = {
|
29
|
-
|
29
|
+
data: [
|
30
30
|
{ type: "posts", id: "1"},
|
31
31
|
{ type: "posts", id: "2"}
|
32
32
|
]
|
33
33
|
}
|
34
34
|
|
35
|
-
assert_equal(expected, @adapter.serializable_hash[:data][:
|
35
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:posts])
|
36
36
|
end
|
37
37
|
|
38
38
|
def test_no_includes_linked_comments
|
@@ -30,13 +30,13 @@ module ActiveModel
|
|
30
30
|
|
31
31
|
def test_includes_comment_ids
|
32
32
|
expected = {
|
33
|
-
|
33
|
+
data: [
|
34
34
|
{ type: 'comments', id: '1' },
|
35
35
|
{ type: 'comments', id: '2' }
|
36
36
|
]
|
37
37
|
}
|
38
38
|
|
39
|
-
assert_equal(expected, @adapter.serializable_hash[:data][:
|
39
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
|
40
40
|
end
|
41
41
|
|
42
42
|
def test_includes_linked_data
|
@@ -45,22 +45,22 @@ module ActiveModel
|
|
45
45
|
{
|
46
46
|
id: '1',
|
47
47
|
type: 'comments',
|
48
|
-
|
49
|
-
post: {
|
48
|
+
relationships: {
|
49
|
+
post: { data: { type: 'posts', id: @post.id.to_s } }
|
50
50
|
}
|
51
51
|
},
|
52
52
|
{
|
53
53
|
id: '2',
|
54
54
|
type: 'comments',
|
55
|
-
|
56
|
-
post: {
|
55
|
+
relationships: {
|
56
|
+
post: { data: { type: 'posts', id: @post.id.to_s } }
|
57
57
|
}
|
58
58
|
},
|
59
59
|
{
|
60
60
|
id: @author.id.to_s,
|
61
61
|
type: "authors",
|
62
|
-
|
63
|
-
posts: {
|
62
|
+
relationships: {
|
63
|
+
posts: { data: [ {type: "posts", id: @post.id.to_s } ] }
|
64
64
|
}
|
65
65
|
}
|
66
66
|
]
|
@@ -70,26 +70,26 @@ module ActiveModel
|
|
70
70
|
|
71
71
|
def test_includes_author_id
|
72
72
|
expected = {
|
73
|
-
|
73
|
+
data: { type: "authors", id: @author.id.to_s }
|
74
74
|
}
|
75
75
|
|
76
|
-
assert_equal(expected, @adapter.serializable_hash[:data][:
|
76
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
|
77
77
|
end
|
78
78
|
|
79
79
|
def test_explicit_serializer_with_null_resource
|
80
80
|
@post.author = nil
|
81
81
|
|
82
|
-
expected = {
|
82
|
+
expected = { data: nil }
|
83
83
|
|
84
|
-
assert_equal(expected, @adapter.serializable_hash[:data][:
|
84
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
|
85
85
|
end
|
86
86
|
|
87
87
|
def test_explicit_serializer_with_null_collection
|
88
88
|
@post.comments = []
|
89
89
|
|
90
|
-
expected = {
|
90
|
+
expected = { data: [] }
|
91
91
|
|
92
|
-
assert_equal(expected, @adapter.serializable_hash[:data][:
|
92
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
|
93
93
|
end
|
94
94
|
end
|
95
95
|
end
|
@@ -33,9 +33,9 @@ module ActiveModel
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def test_includes_comment_ids
|
36
|
-
expected = {
|
36
|
+
expected = { data: [ { type: "comments", id: "1" }, { type: "comments", id: "2" } ] }
|
37
37
|
|
38
|
-
assert_equal(expected, @adapter.serializable_hash[:data][:
|
38
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
|
39
39
|
end
|
40
40
|
|
41
41
|
def test_includes_linked_comments
|
@@ -43,18 +43,22 @@ module ActiveModel
|
|
43
43
|
expected = [{
|
44
44
|
id: "1",
|
45
45
|
type: "comments",
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
attributes: {
|
47
|
+
body: 'ZOMG A COMMENT'
|
48
|
+
},
|
49
|
+
relationships: {
|
50
|
+
post: { data: { type: "posts", id: "1" } },
|
51
|
+
author: { data: nil }
|
50
52
|
}
|
51
53
|
}, {
|
52
54
|
id: "2",
|
53
55
|
type: "comments",
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
56
|
+
attributes: {
|
57
|
+
body: 'ZOMG ANOTHER COMMENT'
|
58
|
+
},
|
59
|
+
relationships: {
|
60
|
+
post: { data: { type: "posts", id: "1" } },
|
61
|
+
author: { data: nil }
|
58
62
|
}
|
59
63
|
}]
|
60
64
|
assert_equal expected, @adapter.serializable_hash[:included]
|
@@ -65,16 +69,16 @@ module ActiveModel
|
|
65
69
|
expected = [{
|
66
70
|
id: "1",
|
67
71
|
type: "comments",
|
68
|
-
|
69
|
-
post: {
|
70
|
-
author: {
|
72
|
+
relationships: {
|
73
|
+
post: { data: { type: "posts", id: "1" } },
|
74
|
+
author: { data: nil }
|
71
75
|
}
|
72
76
|
}, {
|
73
77
|
id: "2",
|
74
78
|
type: "comments",
|
75
|
-
|
76
|
-
post: {
|
77
|
-
author: {
|
79
|
+
relationships: {
|
80
|
+
post: { data: { type: "posts", id: "1" } },
|
81
|
+
author: { data: nil }
|
78
82
|
}
|
79
83
|
}]
|
80
84
|
assert_equal expected, @adapter.serializable_hash[:included]
|
@@ -90,9 +94,9 @@ module ActiveModel
|
|
90
94
|
def test_include_type_for_association_when_different_than_name
|
91
95
|
serializer = BlogSerializer.new(@blog)
|
92
96
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
93
|
-
actual = adapter.serializable_hash[:data][:
|
97
|
+
actual = adapter.serializable_hash[:data][:relationships][:articles]
|
94
98
|
expected = {
|
95
|
-
|
99
|
+
data: [{
|
96
100
|
type: "posts",
|
97
101
|
id: "1"
|
98
102
|
}]
|