Skip to content

Commit d82c599

Browse files
committed
Always use plural for linked types
Although spec is agnostic about inflection rules, examples given are plural
1 parent 0f55f21 commit d82c599

File tree

9 files changed

+51
-51
lines changed

9 files changed

+51
-51
lines changed

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,7 @@ def check_assoc(assoc)
118118

119119
def serialized_object_type(serializer)
120120
return false unless Array(serializer).first
121-
type_name = Array(serializer).first.object.class.to_s.demodulize.underscore
122-
if serializer.respond_to?(:first)
123-
type_name.pluralize
124-
else
125-
type_name
126-
end
121+
Array(serializer).first.object.class.to_s.demodulize.underscore.pluralize
127122
end
128123

129124
def add_resource_links(attrs, serializer, options = {})

test/action_controller/json_api_linked_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ def test_render_resource_with_nested_has_many_include
111111
"id" => "1",
112112
"name" => "admin",
113113
"links" => {
114-
"author" => { "linkage" => { "type" =>"author", "id" => "1" } }
114+
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
115115
}
116116
}, {
117117
"id" => "2",
118118
"name" => "colab",
119119
"links" => {
120-
"author" => { "linkage" => { "type" =>"author", "id" => "1" } }
120+
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
121121
}
122122
}]
123123
}

test/adapter/json_api/belongs_to_test.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def setup
3232
end
3333

3434
def test_includes_post_id
35-
expected = { linkage: { type: "post", id: "42" } }
35+
expected = { linkage: { type: "posts", id: "42" } }
3636

3737
assert_equal(expected, @adapter.serializable_hash[:data][:links][:post])
3838
end
@@ -45,8 +45,8 @@ def test_includes_linked_post
4545
body: 'Body',
4646
links: {
4747
comments: { linkage: [ { type: "comments", id: "1" } ] },
48-
blog: { linkage: { type: "blog", id: "999" } },
49-
author: { linkage: { type: "author", id: "1" } }
48+
blog: { linkage: { type: "blogs", id: "999" } },
49+
author: { linkage: { type: "authors", id: "1" } }
5050
}
5151
}]
5252
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
@@ -58,8 +58,8 @@ def test_limiting_linked_post_fields
5858
title: 'New Post',
5959
links: {
6060
comments: { linkage: [ { type: "comments", id: "1" } ] },
61-
blog: { linkage: { type: "blog", id: "999" } },
62-
author: { linkage: { type: "author", id: "1" } }
61+
blog: { linkage: { type: "blogs", id: "999" } },
62+
author: { linkage: { type: "authors", id: "1" } }
6363
}
6464
}]
6565
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
@@ -69,7 +69,7 @@ def test_include_nil_author
6969
serializer = PostSerializer.new(@anonymous_post)
7070
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
7171

72-
assert_equal({comments: { linkage: [] }, blog: { linkage: { type: "blog", id: "999" } }, author: { linkage: nil }}, adapter.serializable_hash[:data][:links])
72+
assert_equal({comments: { linkage: [] }, blog: { linkage: { type: "blogs", id: "999" } }, author: { linkage: nil }}, adapter.serializable_hash[:data][:links])
7373
end
7474

7575
def test_include_type_for_association_when_different_than_name
@@ -79,7 +79,7 @@ def test_include_type_for_association_when_different_than_name
7979
expected = {
8080
writer: {
8181
linkage: {
82-
type: "author",
82+
type: "authors",
8383
id: "1"
8484
}
8585
},
@@ -119,16 +119,16 @@ def test_include_linked_resources_with_type_name
119119
id: "42",
120120
links: {
121121
comments: { linkage: [ { type: "comments", id: "1" } ] },
122-
blog: { linkage: { type: "blog", id: "999" } },
123-
author: { linkage: { type: "author", id: "1" } }
122+
blog: { linkage: { type: "blogs", id: "999" } },
123+
author: { linkage: { type: "authors", id: "1" } }
124124
}
125125
}, {
126126
title: "Hello!!",
127127
body: "Hello, world!!",
128128
id: "43",
129129
links: {
130130
comments: { linkage: [] },
131-
blog: { linkage: { type: "blog", id: "999" } },
131+
blog: { linkage: { type: "blogs", id: "999" } },
132132
author: { linkage: nil }
133133
}
134134
}]

test/adapter/json_api/collection_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def test_include_multiple_posts
3232
id: "1",
3333
links: {
3434
comments: { linkage: [] },
35-
blog: { linkage: { type: "blog", id: "999" } },
36-
author: { linkage: { type: "author", id: "1" } }
35+
blog: { linkage: { type: "blogs", id: "999" } },
36+
author: { linkage: { type: "authors", id: "1" } }
3737
}
3838
},
3939
{
@@ -42,8 +42,8 @@ def test_include_multiple_posts
4242
id: "2",
4343
links: {
4444
comments: { linkage: [] },
45-
blog: { linkage: { type: "blog", id: "999" } },
46-
author: { linkage: { type: "author", id: "1" } }
45+
blog: { linkage: { type: "blogs", id: "999" } },
46+
author: { linkage: { type: "authors", id: "1" } }
4747
}
4848
}
4949
]
@@ -59,16 +59,16 @@ def test_limiting_fields
5959
title: "Hello!!",
6060
links: {
6161
comments: { linkage: [] },
62-
blog: { linkage: { type: "blog", id: "999" } },
63-
author: { linkage: { type: "author", id: "1" } }
62+
blog: { linkage: { type: "blogs", id: "999" } },
63+
author: { linkage: { type: "authors", id: "1" } }
6464
}
6565
},
6666
{
6767
title: "New Post",
6868
links: {
6969
comments: { linkage: [] },
70-
blog: { linkage: { type: "blog", id: "999" } },
71-
author: { linkage: { type: "author", id: "1" } }
70+
blog: { linkage: { type: "blogs", id: "999" } },
71+
author: { linkage: { type: "authors", id: "1" } }
7272
}
7373
}
7474
]

test/adapter/json_api/has_many_embed_ids_test.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ def setup
2525
end
2626

2727
def test_includes_comment_ids
28-
expected = {:linkage=>[{:type=>"posts", :id=>"1"}, {:type=>"posts", :id=>"2"}]}
28+
expected = {
29+
linkage: [
30+
{ type: "posts", id: "1"},
31+
{ type: "posts", id: "2"}
32+
]
33+
}
2934

3035
assert_equal(expected, @adapter.serializable_hash[:data][:links][:posts])
3136
end

test/adapter/json_api/has_many_explicit_serializer_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ def test_includes_linked_comments
4545
{
4646
id: '1',
4747
links: {
48-
post: { linkage: { type: 'post', id: @post.id.to_s } }
48+
post: { linkage: { type: 'posts', id: @post.id.to_s } }
4949
}
5050
},
5151
{
5252
id: '2',
5353
links: {
54-
post: { linkage: { type: 'post', id: @post.id.to_s } }
54+
post: { linkage: { type: 'posts', id: @post.id.to_s } }
5555
}
5656
}
5757
]
@@ -62,7 +62,7 @@ def test_includes_linked_comments
6262

6363
def test_includes_author_id
6464
expected = {
65-
linkage: { type: "author", id: @author.id.to_s }
65+
linkage: { type: "authors", id: @author.id.to_s }
6666
}
6767

6868
assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])

test/adapter/json_api/has_many_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ def test_includes_linked_comments
4444
id: "1",
4545
body: 'ZOMG A COMMENT',
4646
links: {
47-
post: { linkage: { type: "post", id: "1" } },
47+
post: { linkage: { type: "posts", id: "1" } },
4848
author: { linkage: nil }
4949
}
5050
}, {
5151
id: "2",
5252
body: 'ZOMG ANOTHER COMMENT',
5353
links: {
54-
post: { linkage: { type: "post", id: "1" } },
54+
post: { linkage: { type: "posts", id: "1" } },
5555
author: { linkage: nil }
5656
}
5757
}]
@@ -63,13 +63,13 @@ def test_limit_fields_of_linked_comments
6363
expected = [{
6464
id: "1",
6565
links: {
66-
post: { linkage: { type: "post", id: "1" } },
66+
post: { linkage: { type: "posts", id: "1" } },
6767
author: { linkage: nil }
6868
}
6969
}, {
7070
id: "2",
7171
links: {
72-
post: { linkage: { type: "post", id: "1" } },
72+
post: { linkage: { type: "posts", id: "1" } },
7373
author: { linkage: nil }
7474
}
7575
}]

test/adapter/json_api/has_one_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def setup
3030
end
3131

3232
def test_includes_bio_id
33-
expected = { linkage: { type: "bio", id: "43" } }
33+
expected = { linkage: { type: "bios", id: "43" } }
3434

3535
assert_equal(expected, @adapter.serializable_hash[:data][:links][:bio])
3636
end
@@ -43,7 +43,7 @@ def test_includes_linked_bio
4343
id: "43",
4444
content:"AMS Contributor",
4545
links: {
46-
author: { linkage: { type: "author", id: "1" } }
46+
author: { linkage: { type: "authors", id: "1" } }
4747
}
4848
}
4949
]

test/adapter/json_api/linked_test.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ def test_include_multiple_posts_and_linked
5757
id: "1",
5858
body: "ZOMG A COMMENT",
5959
links: {
60-
post: { linkage: { type: "post", id: "1" } },
60+
post: { linkage: { type: "posts", id: "1" } },
6161
author: { linkage: nil }
6262
}
6363
}, {
6464
id: "2",
6565
body: "ZOMG ANOTHER COMMENT",
6666
links: {
67-
post: { linkage: { type: "post", id: "1" } },
67+
post: { linkage: { type: "posts", id: "1" } },
6868
author: { linkage: nil }
6969
}
7070
}
@@ -76,15 +76,15 @@ def test_include_multiple_posts_and_linked
7676
links: {
7777
posts: { linkage: [ { type: "posts", id: "1" }, { type: "posts", id: "3" } ] },
7878
roles: { linkage: [] },
79-
bio: { linkage: { type: "bio", id: "1" } }
79+
bio: { linkage: { type: "bios", id: "1" } }
8080
}
8181
}, {
8282
id: "2",
8383
name: "Tenderlove",
8484
links: {
8585
posts: { linkage: [ { type: "posts", id:"2" } ] },
8686
roles: { linkage: [] },
87-
bio: { linkage: { type: "bio", id: "2" } }
87+
bio: { linkage: { type: "bios", id: "2" } }
8888
}
8989
}
9090
],
@@ -93,13 +93,13 @@ def test_include_multiple_posts_and_linked
9393
id: "1",
9494
content: "AMS Contributor",
9595
links: {
96-
author: { linkage: { type: "author", id: "1" } }
96+
author: { linkage: { type: "authors", id: "1" } }
9797
}
9898
}, {
9999
id: "2",
100100
content: "Rails Contributor",
101101
links: {
102-
author: { linkage: { type: "author", id: "2" } }
102+
author: { linkage: { type: "authors", id: "2" } }
103103
}
104104
}
105105
]
@@ -111,8 +111,8 @@ def test_include_multiple_posts_and_linked
111111
body: "Hello, world!!",
112112
links: {
113113
comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
114-
blog: { linkage: { type: "blog", id: "999" } },
115-
author: { linkage: { type: "author", id: "1" } }
114+
blog: { linkage: { type: "blogs", id: "999" } },
115+
author: { linkage: { type: "authors", id: "1" } }
116116
}
117117
},
118118
{
@@ -121,8 +121,8 @@ def test_include_multiple_posts_and_linked
121121
body: "Body",
122122
links: {
123123
comments: { linkage: [] },
124-
blog: { linkage: { type: "blog", id: "999" } },
125-
author: { linkage: { type: "author", id: "2" } }
124+
blog: { linkage: { type: "blogs", id: "999" } },
125+
author: { linkage: { type: "authors", id: "2" } }
126126
}
127127
}
128128
]
@@ -150,7 +150,7 @@ def test_include_multiple_posts_and_linked
150150
links: {
151151
posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] },
152152
roles: { linkage: [] },
153-
bio: { linkage: { type: "bio", id: "1" }}
153+
bio: { linkage: { type: "bios", id: "1" }}
154154
}
155155
}
156156
],
@@ -161,17 +161,17 @@ def test_include_multiple_posts_and_linked
161161
body: "Hello, world!!",
162162
links: {
163163
comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] },
164-
blog: { linkage: { type: "blog", id: "999" } },
165-
author: { linkage: { type: "author", id: "1" } }
164+
blog: { linkage: { type: "blogs", id: "999" } },
165+
author: { linkage: { type: "authors", id: "1" } }
166166
}
167167
}, {
168168
id: "30",
169169
title: "Yet Another Post",
170170
body: "Body",
171171
links: {
172172
comments: { linkage: [] },
173-
blog: { linkage: { type: "blog", id: "999" } },
174-
author: { linkage: { type: "author", id: "1" } }
173+
blog: { linkage: { type: "blogs", id: "999" } },
174+
author: { linkage: { type: "authors", id: "1" } }
175175
}
176176
}
177177
]

0 commit comments

Comments
 (0)