active_model_serializers 0.8.3 → 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/.gitignore +3 -0
- data/.travis.yml +18 -20
- data/CHANGELOG.md +8 -67
- data/CONTRIBUTING.md +31 -0
- data/Gemfile +14 -1
- data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
- data/README.md +169 -495
- data/Rakefile +6 -12
- data/active_model_serializers.gemspec +21 -19
- data/lib/action_controller/serialization.rb +36 -27
- data/lib/active_model/serializer/adapter/flatten_json.rb +12 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +78 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +15 -0
- data/lib/active_model/serializer/adapter/json.rb +50 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +23 -0
- data/lib/active_model/serializer/adapter/json_api.rb +156 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -0
- data/lib/active_model/serializer/adapter.rb +96 -0
- data/lib/active_model/serializer/array_serializer.rb +35 -0
- data/lib/active_model/serializer/configuration.rb +13 -0
- data/lib/active_model/serializer/fieldset.rb +40 -0
- data/lib/active_model/serializer/railtie.rb +8 -0
- data/lib/active_model/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +177 -440
- data/lib/active_model_serializers.rb +10 -86
- data/lib/generators/serializer/USAGE +0 -3
- data/lib/generators/serializer/resource_override.rb +12 -0
- data/lib/generators/serializer/serializer_generator.rb +1 -6
- data/lib/generators/serializer/templates/serializer.rb +2 -13
- data/test/action_controller/adapter_selector_test.rb +53 -0
- data/test/action_controller/explicit_serializer_test.rb +134 -0
- data/test/action_controller/json_api_linked_test.rb +179 -0
- data/test/action_controller/rescue_from_test.rb +32 -0
- data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +7 -11
- data/test/action_controller/serialization_test.rb +383 -0
- data/test/adapter/fragment_cache_test.rb +27 -0
- data/test/adapter/json/belongs_to_test.rb +48 -0
- data/test/adapter/json/collection_test.rb +73 -0
- data/test/adapter/json/has_many_test.rb +36 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +96 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +110 -0
- data/test/adapter/json_api/has_one_test.rb +61 -0
- data/test/adapter/json_api/linked_test.rb +283 -0
- data/test/adapter/json_test.rb +34 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +43 -0
- data/test/array_serializer_test.rb +31 -63
- data/test/fixtures/poro.rb +230 -0
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/{generators_test.rb → generators/serializer_generator_test.rb} +2 -36
- data/test/serializers/adapter_for_test.rb +50 -0
- data/test/serializers/associations_test.rb +127 -0
- data/test/serializers/attribute_test.rb +38 -0
- data/test/serializers/attributes_test.rb +63 -0
- data/test/serializers/cache_test.rb +138 -0
- data/test/serializers/configuration_test.rb +15 -0
- data/test/serializers/fieldset_test.rb +26 -0
- data/test/serializers/meta_test.rb +107 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +65 -0
- data/test/serializers/urls_test.rb +26 -0
- data/test/test_helper.rb +28 -16
- metadata +109 -43
- data/DESIGN.textile +0 -586
- data/Gemfile.edge +0 -9
- data/bench/perf.rb +0 -43
- data/cruft.md +0 -19
- data/lib/active_model/array_serializer.rb +0 -104
- data/lib/active_model/serializer/associations.rb +0 -233
- data/lib/active_record/serializer_override.rb +0 -16
- data/lib/generators/resource_override.rb +0 -13
- data/test/association_test.rb +0 -592
- data/test/caching_test.rb +0 -96
- data/test/no_serialization_scope_test.rb +0 -34
- data/test/serialization_test.rb +0 -392
- data/test/serializer_support_test.rb +0 -51
- data/test/serializer_test.rb +0 -1465
- data/test/test_fakes.rb +0 -217
@@ -0,0 +1,157 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class Adapter
|
6
|
+
class JsonApi
|
7
|
+
class BelongsToTest < Minitest::Test
|
8
|
+
def setup
|
9
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
10
|
+
@author.bio = nil
|
11
|
+
@author.roles = []
|
12
|
+
@blog = Blog.new(id: 23, name: 'AMS Blog')
|
13
|
+
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
14
|
+
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
|
15
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
16
|
+
@post.comments = [@comment]
|
17
|
+
@post.blog = @blog
|
18
|
+
@anonymous_post.comments = []
|
19
|
+
@anonymous_post.blog = nil
|
20
|
+
@comment.post = @post
|
21
|
+
@comment.author = nil
|
22
|
+
@post.author = @author
|
23
|
+
@anonymous_post.author = nil
|
24
|
+
@blog = Blog.new(id: 1, name: "My Blog!!")
|
25
|
+
@blog.writer = @author
|
26
|
+
@blog.articles = [@post, @anonymous_post]
|
27
|
+
@author.posts = []
|
28
|
+
|
29
|
+
@serializer = CommentSerializer.new(@comment)
|
30
|
+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
|
31
|
+
ActionController::Base.cache_store.clear
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_includes_post_id
|
35
|
+
expected = { data: { type: "posts", id: "42" } }
|
36
|
+
|
37
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:post])
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_includes_linked_post
|
41
|
+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post')
|
42
|
+
expected = [{
|
43
|
+
id: "42",
|
44
|
+
type: "posts",
|
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" } }
|
53
|
+
}
|
54
|
+
}]
|
55
|
+
assert_equal expected, @adapter.serializable_hash[:included]
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_limiting_linked_post_fields
|
59
|
+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post', fields: {post: [:title]})
|
60
|
+
expected = [{
|
61
|
+
id: "42",
|
62
|
+
type: "posts",
|
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" } }
|
70
|
+
}
|
71
|
+
}]
|
72
|
+
assert_equal expected, @adapter.serializable_hash[:included]
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_include_nil_author
|
76
|
+
serializer = PostSerializer.new(@anonymous_post)
|
77
|
+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
78
|
+
|
79
|
+
assert_equal({comments: { data: [] }, blog: { data: { type: "blogs", id: "999" } }, author: { data: nil }}, adapter.serializable_hash[:data][:relationships])
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_include_type_for_association_when_different_than_name
|
83
|
+
serializer = BlogSerializer.new(@blog)
|
84
|
+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
85
|
+
relationships = adapter.serializable_hash[:data][:relationships]
|
86
|
+
expected = {
|
87
|
+
writer: {
|
88
|
+
data: {
|
89
|
+
type: "authors",
|
90
|
+
id: "1"
|
91
|
+
}
|
92
|
+
},
|
93
|
+
articles: {
|
94
|
+
data: [
|
95
|
+
{
|
96
|
+
type: "posts",
|
97
|
+
id: "42"
|
98
|
+
},
|
99
|
+
{
|
100
|
+
type: "posts",
|
101
|
+
id: "43"
|
102
|
+
}
|
103
|
+
]
|
104
|
+
}
|
105
|
+
}
|
106
|
+
assert_equal expected, relationships
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_include_linked_resources_with_type_name
|
110
|
+
serializer = BlogSerializer.new(@blog)
|
111
|
+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: ['writer', 'articles'])
|
112
|
+
linked = adapter.serializable_hash[:included]
|
113
|
+
expected = [
|
114
|
+
{
|
115
|
+
id: "1",
|
116
|
+
type: "authors",
|
117
|
+
attributes: {
|
118
|
+
name: "Steve K."
|
119
|
+
},
|
120
|
+
relationships: {
|
121
|
+
posts: { data: [] },
|
122
|
+
roles: { data: [] },
|
123
|
+
bio: { data: nil }
|
124
|
+
}
|
125
|
+
},{
|
126
|
+
id: "42",
|
127
|
+
type: "posts",
|
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" } }
|
136
|
+
}
|
137
|
+
}, {
|
138
|
+
id: "43",
|
139
|
+
type: "posts",
|
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 }
|
148
|
+
}
|
149
|
+
}
|
150
|
+
]
|
151
|
+
assert_equal expected, linked
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class Adapter
|
6
|
+
class JsonApi
|
7
|
+
class CollectionTest < Minitest::Test
|
8
|
+
def setup
|
9
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
10
|
+
@author.bio = nil
|
11
|
+
@blog = Blog.new(id: 23, name: 'AMS Blog')
|
12
|
+
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
13
|
+
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
14
|
+
@first_post.comments = []
|
15
|
+
@second_post.comments = []
|
16
|
+
@first_post.blog = @blog
|
17
|
+
@second_post.blog = nil
|
18
|
+
@first_post.author = @author
|
19
|
+
@second_post.author = @author
|
20
|
+
@author.posts = [@first_post, @second_post]
|
21
|
+
|
22
|
+
@serializer = ArraySerializer.new([@first_post, @second_post])
|
23
|
+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
|
24
|
+
ActionController::Base.cache_store.clear
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_include_multiple_posts
|
28
|
+
expected = [
|
29
|
+
{
|
30
|
+
id: "1",
|
31
|
+
type: "posts",
|
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" } }
|
40
|
+
}
|
41
|
+
},
|
42
|
+
{
|
43
|
+
id: "2",
|
44
|
+
type: "posts",
|
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" } }
|
53
|
+
}
|
54
|
+
}
|
55
|
+
]
|
56
|
+
|
57
|
+
assert_equal(expected, @adapter.serializable_hash[:data])
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_limiting_fields
|
61
|
+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, fields: ['title'])
|
62
|
+
|
63
|
+
expected = [
|
64
|
+
{
|
65
|
+
id: "1",
|
66
|
+
type: "posts",
|
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" } }
|
74
|
+
}
|
75
|
+
},
|
76
|
+
{
|
77
|
+
id: "2",
|
78
|
+
type: "posts",
|
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" } }
|
86
|
+
}
|
87
|
+
}
|
88
|
+
]
|
89
|
+
assert_equal(expected, @adapter.serializable_hash[:data])
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class Adapter
|
6
|
+
class JsonApi
|
7
|
+
class HasManyEmbedIdsTest < Minitest::Test
|
8
|
+
def setup
|
9
|
+
@author = Author.new(name: 'Steve K.')
|
10
|
+
@author.bio = nil
|
11
|
+
@author.roles = nil
|
12
|
+
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
13
|
+
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
14
|
+
@author.posts = [@first_post, @second_post]
|
15
|
+
@first_post.author = @author
|
16
|
+
@second_post.author = @author
|
17
|
+
@first_post.comments = []
|
18
|
+
@second_post.comments = []
|
19
|
+
@blog = Blog.new(id: 23, name: 'AMS Blog')
|
20
|
+
@first_post.blog = @blog
|
21
|
+
@second_post.blog = nil
|
22
|
+
|
23
|
+
@serializer = AuthorSerializer.new(@author)
|
24
|
+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_includes_comment_ids
|
28
|
+
expected = {
|
29
|
+
data: [
|
30
|
+
{ type: "posts", id: "1"},
|
31
|
+
{ type: "posts", id: "2"}
|
32
|
+
]
|
33
|
+
}
|
34
|
+
|
35
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:posts])
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_no_includes_linked_comments
|
39
|
+
assert_nil @adapter.serializable_hash[:linked]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class Adapter
|
6
|
+
class JsonApi
|
7
|
+
# Test 'has_many :assocs, serializer: AssocXSerializer'
|
8
|
+
class HasManyExplicitSerializerTest < Minitest::Test
|
9
|
+
def setup
|
10
|
+
@post = Post.new(title: 'New Post', body: 'Body')
|
11
|
+
@author = Author.new(name: 'Jane Blogger')
|
12
|
+
@author.posts = [@post]
|
13
|
+
@post.author = @author
|
14
|
+
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
15
|
+
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
16
|
+
@post.comments = [@first_comment, @second_comment]
|
17
|
+
@first_comment.post = @post
|
18
|
+
@first_comment.author = nil
|
19
|
+
@second_comment.post = @post
|
20
|
+
@second_comment.author = nil
|
21
|
+
@blog = Blog.new(id: 23, name: 'AMS Blog')
|
22
|
+
@post.blog = @blog
|
23
|
+
|
24
|
+
@serializer = PostPreviewSerializer.new(@post)
|
25
|
+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
|
26
|
+
@serializer,
|
27
|
+
include: ['comments', 'author']
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_includes_comment_ids
|
32
|
+
expected = {
|
33
|
+
data: [
|
34
|
+
{ type: 'comments', id: '1' },
|
35
|
+
{ type: 'comments', id: '2' }
|
36
|
+
]
|
37
|
+
}
|
38
|
+
|
39
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_includes_linked_data
|
43
|
+
# If CommentPreviewSerializer is applied correctly the body text will not be present in the output
|
44
|
+
expected = [
|
45
|
+
{
|
46
|
+
id: '1',
|
47
|
+
type: 'comments',
|
48
|
+
relationships: {
|
49
|
+
post: { data: { type: 'posts', id: @post.id.to_s } }
|
50
|
+
}
|
51
|
+
},
|
52
|
+
{
|
53
|
+
id: '2',
|
54
|
+
type: 'comments',
|
55
|
+
relationships: {
|
56
|
+
post: { data: { type: 'posts', id: @post.id.to_s } }
|
57
|
+
}
|
58
|
+
},
|
59
|
+
{
|
60
|
+
id: @author.id.to_s,
|
61
|
+
type: "authors",
|
62
|
+
relationships: {
|
63
|
+
posts: { data: [ {type: "posts", id: @post.id.to_s } ] }
|
64
|
+
}
|
65
|
+
}
|
66
|
+
]
|
67
|
+
|
68
|
+
assert_equal(expected, @adapter.serializable_hash[:included])
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_includes_author_id
|
72
|
+
expected = {
|
73
|
+
data: { type: "authors", id: @author.id.to_s }
|
74
|
+
}
|
75
|
+
|
76
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_explicit_serializer_with_null_resource
|
80
|
+
@post.author = nil
|
81
|
+
|
82
|
+
expected = { data: nil }
|
83
|
+
|
84
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_explicit_serializer_with_null_collection
|
88
|
+
@post.comments = []
|
89
|
+
|
90
|
+
expected = { data: [] }
|
91
|
+
|
92
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class Adapter
|
6
|
+
class JsonApi
|
7
|
+
class HasManyTest < Minitest::Test
|
8
|
+
def setup
|
9
|
+
ActionController::Base.cache_store.clear
|
10
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
11
|
+
@author.posts = []
|
12
|
+
@author.bio = nil
|
13
|
+
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
|
14
|
+
@post_without_comments = Post.new(id: 2, title: 'Second Post', body: 'Second')
|
15
|
+
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
16
|
+
@first_comment.author = nil
|
17
|
+
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
18
|
+
@second_comment.author = nil
|
19
|
+
@post.comments = [@first_comment, @second_comment]
|
20
|
+
@post_without_comments.comments = []
|
21
|
+
@first_comment.post = @post
|
22
|
+
@second_comment.post = @post
|
23
|
+
@post.author = @author
|
24
|
+
@post_without_comments.author = nil
|
25
|
+
@blog = Blog.new(id: 1, name: "My Blog!!")
|
26
|
+
@blog.writer = @author
|
27
|
+
@blog.articles = [@post]
|
28
|
+
@post.blog = @blog
|
29
|
+
@post_without_comments.blog = nil
|
30
|
+
|
31
|
+
@serializer = PostSerializer.new(@post)
|
32
|
+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_includes_comment_ids
|
36
|
+
expected = { data: [ { type: "comments", id: "1" }, { type: "comments", id: "2" } ] }
|
37
|
+
|
38
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_includes_linked_comments
|
42
|
+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments')
|
43
|
+
expected = [{
|
44
|
+
id: "1",
|
45
|
+
type: "comments",
|
46
|
+
attributes: {
|
47
|
+
body: 'ZOMG A COMMENT'
|
48
|
+
},
|
49
|
+
relationships: {
|
50
|
+
post: { data: { type: "posts", id: "1" } },
|
51
|
+
author: { data: nil }
|
52
|
+
}
|
53
|
+
}, {
|
54
|
+
id: "2",
|
55
|
+
type: "comments",
|
56
|
+
attributes: {
|
57
|
+
body: 'ZOMG ANOTHER COMMENT'
|
58
|
+
},
|
59
|
+
relationships: {
|
60
|
+
post: { data: { type: "posts", id: "1" } },
|
61
|
+
author: { data: nil }
|
62
|
+
}
|
63
|
+
}]
|
64
|
+
assert_equal expected, @adapter.serializable_hash[:included]
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_limit_fields_of_linked_comments
|
68
|
+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:id]})
|
69
|
+
expected = [{
|
70
|
+
id: "1",
|
71
|
+
type: "comments",
|
72
|
+
relationships: {
|
73
|
+
post: { data: { type: "posts", id: "1" } },
|
74
|
+
author: { data: nil }
|
75
|
+
}
|
76
|
+
}, {
|
77
|
+
id: "2",
|
78
|
+
type: "comments",
|
79
|
+
relationships: {
|
80
|
+
post: { data: { type: "posts", id: "1" } },
|
81
|
+
author: { data: nil }
|
82
|
+
}
|
83
|
+
}]
|
84
|
+
assert_equal expected, @adapter.serializable_hash[:included]
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_no_include_linked_if_comments_is_empty
|
88
|
+
serializer = PostSerializer.new(@post_without_comments)
|
89
|
+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
90
|
+
|
91
|
+
assert_nil adapter.serializable_hash[:linked]
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_include_type_for_association_when_different_than_name
|
95
|
+
serializer = BlogSerializer.new(@blog)
|
96
|
+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
97
|
+
actual = adapter.serializable_hash[:data][:relationships][:articles]
|
98
|
+
expected = {
|
99
|
+
data: [{
|
100
|
+
type: "posts",
|
101
|
+
id: "1"
|
102
|
+
}]
|
103
|
+
}
|
104
|
+
assert_equal expected, actual
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class Adapter
|
6
|
+
class JsonApi
|
7
|
+
class HasOneTest < Minitest::Test
|
8
|
+
def setup
|
9
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
10
|
+
@bio = Bio.new(id: 43, content: 'AMS Contributor')
|
11
|
+
@author.bio = @bio
|
12
|
+
@bio.author = @author
|
13
|
+
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
14
|
+
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
|
15
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
16
|
+
@post.comments = [@comment]
|
17
|
+
@anonymous_post.comments = []
|
18
|
+
@comment.post = @post
|
19
|
+
@comment.author = nil
|
20
|
+
@post.author = @author
|
21
|
+
@anonymous_post.author = nil
|
22
|
+
@blog = Blog.new(id: 1, name: "My Blog!!")
|
23
|
+
@blog.writer = @author
|
24
|
+
@blog.articles = [@post, @anonymous_post]
|
25
|
+
@author.posts = []
|
26
|
+
@author.roles = []
|
27
|
+
|
28
|
+
@serializer = AuthorSerializer.new(@author)
|
29
|
+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio,posts')
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_includes_bio_id
|
33
|
+
expected = { data: { type: "bios", id: "43" } }
|
34
|
+
|
35
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:bio])
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_includes_linked_bio
|
39
|
+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio')
|
40
|
+
|
41
|
+
expected = [
|
42
|
+
{
|
43
|
+
id: "43",
|
44
|
+
type: "bios",
|
45
|
+
attributes: {
|
46
|
+
content:"AMS Contributor",
|
47
|
+
rating: nil
|
48
|
+
},
|
49
|
+
relationships: {
|
50
|
+
author: { data: { type: "authors", id: "1" } }
|
51
|
+
}
|
52
|
+
}
|
53
|
+
]
|
54
|
+
|
55
|
+
assert_equal(expected, @adapter.serializable_hash[:included])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|