Skip to content

Commit 3e8325b

Browse files
committed
Merge pull request #773 from sweatypitts/master
Make json api adapter 'include' option accept an array
2 parents b8df4b5 + ad5677c commit 3e8325b

File tree

6 files changed

+144
-105
lines changed

6 files changed

+144
-105
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ resources in the `"linked"` member when the resource names are included in the
189189
`include` option.
190190

191191
```ruby
192+
render @posts, include: ['authors', 'comments']
193+
# or
192194
render @posts, include: 'authors,comments'
193195
```
194196

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ def include_nested_assoc?(assoc)
121121
end
122122

123123
def check_assoc(assoc)
124-
@options[:include].split(',').any? do |s|
124+
include_opt = @options[:include]
125+
include_opt = include_opt.split(',') if include_opt.is_a?(String)
126+
include_opt.any? do |s|
125127
s.match(/^#{assoc.gsub('.', '\.')}/)
126128
end
127129
end

test/action_controller/json_api_linked_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def render_resource_with_nested_include
5252

5353
def render_resource_with_nested_has_many_include
5454
setup_post
55-
render json: @post, include: 'author,author.roles', adapter: :json_api
55+
render json: @post, include: ['author', 'author.roles'], adapter: :json_api
5656
end
5757

5858
def render_resource_with_missing_nested_has_many_include
@@ -74,7 +74,7 @@ def render_collection_without_include
7474

7575
def render_collection_with_include
7676
setup_post
77-
render json: [@post], include: 'author,comments', adapter: :json_api
77+
render json: [@post], include: ['author', 'comments'], adapter: :json_api
7878
end
7979
end
8080

test/adapter/json_api/belongs_to_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_include_type_for_association_when_different_than_name
8989

9090
def test_include_linked_resources_with_type_name
9191
serializer = BlogSerializer.new(@blog)
92-
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: "writer,articles")
92+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: ['writer', 'articles'])
9393
linked = adapter.serializable_hash[:linked]
9494
expected = {
9595
authors: [{

test/adapter/json_api/has_many_explicit_serializer_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def setup
2424
@serializer = PostPreviewSerializer.new(@post)
2525
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
2626
@serializer,
27-
include: 'comments,author'
27+
include: ['comments', 'author']
2828
)
2929
end
3030

test/adapter/json_api/linked_test.rb

Lines changed: 135 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,21 @@ def setup
1414
@second_post = Post.new(id: 20, title: 'New Post', body: 'Body')
1515
@third_post = Post.new(id: 30, title: 'Yet Another Post', body: 'Body')
1616
@blog = Blog.new({ name: 'AMS Blog' })
17+
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
18+
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
1719
@first_post.blog = @blog
1820
@second_post.blog = @blog
1921
@third_post.blog = nil
20-
@first_post.comments = []
22+
@first_post.comments = [@first_comment, @second_comment]
2123
@second_post.comments = []
24+
@third_post.comments = []
2225
@first_post.author = @author1
2326
@second_post.author = @author2
2427
@third_post.author = @author1
28+
@first_comment.post = @first_post
29+
@first_comment.author = nil
30+
@second_comment.post = @first_post
31+
@second_comment.author = nil
2532
@author1.posts = [@first_post, @third_post]
2633
@author1.bio = @bio1
2734
@author1.roles = []
@@ -33,116 +40,144 @@ def setup
3340
end
3441

3542
def test_include_multiple_posts_and_linked
36-
@serializer = ArraySerializer.new([@first_post, @second_post])
37-
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'author,author.bio,comments')
38-
39-
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
40-
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
41-
@first_post.comments = [@first_comment, @second_comment]
42-
@first_comment.post = @first_post
43-
@first_comment.author = nil
44-
@second_comment.post = @first_post
45-
@second_comment.author = nil
46-
assert_equal([
47-
{ title: "Hello!!", body: "Hello, world!!", id: "10", links: { comments: ['1', '2'], blog: "999", author: "1" } },
48-
{ title: "New Post", body: "Body", id: "20", links: { comments: [], blog: "999", author: "2" } }
49-
], @adapter.serializable_hash[:posts])
50-
43+
serializer = ArraySerializer.new([@first_post, @second_post])
44+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
45+
serializer,
46+
include: ['author', 'author.bio', 'comments']
47+
)
48+
alt_adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
49+
serializer,
50+
include: 'author,author.bio,comments'
51+
)
5152

5253
expected = {
53-
comments: [{
54-
id: "1",
55-
body: "ZOMG A COMMENT",
56-
links: {
57-
post: "10",
58-
author: nil
54+
linked: {
55+
comments: [
56+
{
57+
id: "1",
58+
body: "ZOMG A COMMENT",
59+
links: {
60+
post: "1",
61+
author: nil
62+
}
63+
}, {
64+
id: "2",
65+
body: "ZOMG ANOTHER COMMENT",
66+
links: {
67+
post: "1",
68+
author: nil
69+
}
70+
}
71+
],
72+
authors: [
73+
{
74+
id: "1",
75+
name: "Steve K.",
76+
links: {
77+
posts: ["1", "3"],
78+
roles: [],
79+
bio: "1"
80+
}
81+
}, {
82+
id: "2",
83+
name: "Tenderlove",
84+
links: {
85+
posts: ["2"],
86+
roles: [],
87+
bio: "2"
88+
}
89+
}
90+
],
91+
bios: [
92+
{
93+
id: "1",
94+
content: "AMS Contributor",
95+
links: {
96+
author: "1"
97+
}
98+
}, {
99+
id: "2",
100+
content: "Rails Contributor",
101+
links: {
102+
author: "2"
103+
}
104+
}
105+
]
106+
},
107+
posts: [
108+
{
109+
id: "10",
110+
title: "Hello!!",
111+
body: "Hello, world!!",
112+
links: {
113+
comments: ['1', '2'],
114+
blog: "999",
115+
author: "1"
116+
}
117+
},
118+
{
119+
id: "2",
120+
title: "New Post",
121+
body: "Body",
122+
links: {
123+
comments: [],
124+
blog: "999",
125+
author: "2"
126+
}
59127
}
60-
}, {
61-
id: "2",
62-
body: "ZOMG ANOTHER COMMENT",
63-
links: {
64-
post: "10",
65-
author: nil
66-
}
67-
}],
68-
authors: [{
69-
id: "1",
70-
name: "Steve K.",
71-
links: {
72-
posts: ["10", "30"],
73-
roles: [],
74-
bio: "1"
75-
}
76-
}, {
77-
id: "2",
78-
name: "Tenderlove",
79-
links: {
80-
posts: ["20"],
81-
roles: [],
82-
bio: "2"
83-
}
84-
}],
85-
bios: [{
86-
id: "1",
87-
content: "AMS Contributor",
88-
links: {
89-
author: "1"
90-
}
91-
}, {
92-
id: "2",
93-
content: "Rails Contributor",
94-
links: {
95-
author: "2"
96-
}
97-
}]
128+
]
98129
}
99-
assert_equal expected, @adapter.serializable_hash[:linked]
130+
assert_equal expected, adapter.serializable_hash
131+
assert_equal expected, alt_adapter.serializable_hash
100132
end
101133

102-
def test_include_bio_and_linked
103-
@serializer = BioSerializer.new(@bio1)
104-
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'author,author.posts')
105-
106-
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
107-
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
108-
@first_post.comments = [@first_comment, @second_comment]
109-
@third_post.comments = []
110-
@first_comment.post = @first_post
111-
@first_comment.author = nil
112-
@second_comment.post = @first_post
113-
@second_comment.author = nil
134+
def test_include_multiple_posts_and_linked
135+
serializer = BioSerializer.new @bio1
136+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
137+
serializer,
138+
include: ['author', 'author.posts']
139+
)
140+
alt_adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
141+
serializer,
142+
include: 'author,author.posts'
143+
)
114144

115145
expected = {
116-
authors: [{
117-
id: "1",
118-
name: "Steve K.",
119-
links: {
120-
posts: ["10", "30"],
121-
roles: [],
122-
bio: "1"
123-
}
124-
}],
125-
posts: [{
126-
title: "Hello!!",
127-
body: "Hello, world!!",
128-
id: "10",
129-
links: {
130-
comments: ["1", "2"],
131-
blog: "999",
132-
author: "1"
146+
authors: [
147+
{
148+
id: "1",
149+
name: "Steve K.",
150+
links: {
151+
posts: ["10", "30"],
152+
roles: [],
153+
bio: "1"
154+
}
133155
}
134-
}, {
135-
title: "Yet Another Post",
136-
body: "Body",
137-
id: "30",
138-
links: {
139-
comments: [],
140-
blog: nil,
141-
author: "1"
156+
],
157+
posts: [
158+
{
159+
id: "10",
160+
title: "Hello!!",
161+
body: "Hello, world!!",
162+
links: {
163+
comments: ["1", "2"],
164+
blog: "999",
165+
author: "1"
166+
}
167+
}, {
168+
id: "30",
169+
title: "Yet Another Post",
170+
body: "Body",
171+
links: {
172+
comments: [],
173+
blog: nil,
174+
author: "1"
175+
}
142176
}
143-
}]
177+
]
144178
}
145-
assert_equal expected, @adapter.serializable_hash[:linked]
179+
assert_equal expected, adapter.serializable_hash[:linked]
180+
assert_equal expected, alt_adapter.serializable_hash[:linked]
146181
end
147182

148183
def test_ignore_model_namespace_for_linked_resource_type

0 commit comments

Comments
 (0)