Skip to content

Commit 9718e21

Browse files
committed
Change links to relationships in routes and urls
1 parent 7335d83 commit 9718e21

6 files changed

Lines changed: 146 additions & 146 deletions

File tree

lib/jsonapi/resource_serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def format_route(route)
217217
end
218218

219219
def self_link(source, association)
220-
"#{self_href(source)}/links/#{format_route(association.name)}"
220+
"#{self_href(source)}/relationships/#{format_route(association.name)}"
221221
end
222222

223223
def related_link(source, association)

lib/jsonapi/routing_ext.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,17 @@ def jsonapi_link(*links)
9595
methods = links_methods(options)
9696

9797
if methods.include?(:show)
98-
match "links/#{formatted_association_name}", controller: options[:controller],
98+
match "relationships/#{formatted_association_name}", controller: options[:controller],
9999
action: 'show_association', association: link_type.to_s, via: [:get]
100100
end
101101

102102
if methods.include?(:update)
103-
match "links/#{formatted_association_name}", controller: options[:controller],
103+
match "relationships/#{formatted_association_name}", controller: options[:controller],
104104
action: 'update_association', association: link_type.to_s, via: [:put, :patch]
105105
end
106106

107107
if methods.include?(:destroy)
108-
match "links/#{formatted_association_name}", controller: options[:controller],
108+
match "relationships/#{formatted_association_name}", controller: options[:controller],
109109
action: 'destroy_association', association: link_type.to_s, via: [:delete]
110110
end
111111
end
@@ -121,22 +121,22 @@ def jsonapi_links(*links)
121121
methods = links_methods(options)
122122

123123
if methods.include?(:show)
124-
match "links/#{formatted_association_name}", controller: options[:controller],
124+
match "relationships/#{formatted_association_name}", controller: options[:controller],
125125
action: 'show_association', association: link_type.to_s, via: [:get]
126126
end
127127

128128
if methods.include?(:create)
129-
match "links/#{formatted_association_name}", controller: options[:controller],
129+
match "relationships/#{formatted_association_name}", controller: options[:controller],
130130
action: 'create_association', association: link_type.to_s, via: [:post]
131131
end
132132

133133
if methods.include?(:update)
134-
match "links/#{formatted_association_name}", controller: options[:controller],
134+
match "relationships/#{formatted_association_name}", controller: options[:controller],
135135
action: 'update_association', association: link_type.to_s, via: [:put, :patch]
136136
end
137137

138138
if methods.include?(:destroy)
139-
match "links/#{formatted_association_name}/:keys", controller: options[:controller],
139+
match "relationships/#{formatted_association_name}/:keys", controller: options[:controller],
140140
action: 'destroy_association', association: link_type.to_s, via: [:delete]
141141
end
142142
end

test/controllers/controller_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ def test_show_has_one_relationship
13941394
id: '1'
13951395
},
13961396
links: {
1397-
self: 'http://test.host/posts/1/links/author',
1397+
self: 'http://test.host/posts/1/relationships/author',
13981398
related: 'http://test.host/posts/1/author'
13991399
}
14001400
}
@@ -1409,7 +1409,7 @@ def test_show_has_many_relationship
14091409
{type: 'tags', id: '5'}
14101410
],
14111411
links: {
1412-
self: 'http://test.host/posts/2/links/tags',
1412+
self: 'http://test.host/posts/2/relationships/tags',
14131413
related: 'http://test.host/posts/2/tags'
14141414
}
14151415
}
@@ -1428,7 +1428,7 @@ def test_show_has_one_relationship_nil
14281428
{
14291429
data: nil,
14301430
links: {
1431-
self: 'http://test.host/posts/17/links/author',
1431+
self: 'http://test.host/posts/17/relationships/author',
14321432
related: 'http://test.host/posts/17/author'
14331433
}
14341434
}
@@ -1881,19 +1881,19 @@ def test_get_related_resource
18811881
relationships: {
18821882
comments: {
18831883
links: {
1884-
self: 'http://test.host/people/1/links/comments',
1884+
self: 'http://test.host/people/1/relationships/comments',
18851885
related: 'http://test.host/people/1/comments'
18861886
}
18871887
},
18881888
posts: {
18891889
links: {
1890-
self: 'http://test.host/people/1/links/posts',
1890+
self: 'http://test.host/people/1/relationships/posts',
18911891
related: 'http://test.host/people/1/posts'
18921892
}
18931893
},
18941894
preferences: {
18951895
links: {
1896-
self: 'http://test.host/people/1/links/preferences',
1896+
self: 'http://test.host/people/1/relationships/preferences',
18971897
related: 'http://test.host/people/1/preferences'
18981898
},
18991899
data: {
@@ -1903,7 +1903,7 @@ def test_get_related_resource
19031903
},
19041904
"hair-cut" => {
19051905
"links" => {
1906-
"self" => "http://test.host/people/1/links/hair_cut",
1906+
"self" => "http://test.host/people/1/relationships/hair_cut",
19071907
"related" => "http://test.host/people/1/hair_cut"
19081908
},
19091909
"data" => nil
@@ -2057,7 +2057,7 @@ class Api::V1::PostsControllerTest < ActionController::TestCase
20572057
def test_show_post_namespaced
20582058
get :show, {id: '1'}
20592059
assert_response :success
2060-
assert_equal 'http://test.host/api/v1/posts/1/links/writer', json_response['data']['relationships']['writer']['links']['self']
2060+
assert_equal 'http://test.host/api/v1/posts/1/relationships/writer', json_response['data']['relationships']['writer']['links']['self']
20612061
end
20622062

20632063
def test_show_post_namespaced_include

test/integration/requests/request_test.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ def test_get_camelized_route_and_key_filtered
7070
def test_get_camelized_route_and_links
7171
JSONAPI.configuration.json_key_format = :camelized_key
7272
JSONAPI.configuration.route_format = :camelized_route
73-
get '/api/v4/expenseEntries/1/links/isoCurrency'
73+
get '/api/v4/expenseEntries/1/relationships/isoCurrency'
7474
assert_equal 200, status
7575
assert_hash_equals({'links' => {
76-
'self' => 'http://www.example.com/api/v4/expenseEntries/1/links/isoCurrency',
76+
'self' => 'http://www.example.com/api/v4/expenseEntries/1/relationships/isoCurrency',
7777
'related' => 'http://www.example.com/api/v4/expenseEntries/1/isoCurrency'
7878
},
7979
'data' => {
@@ -205,21 +205,21 @@ def test_post_single_minimal_invalid
205205

206206
def test_update_association_without_content_type
207207
ruby = Section.find_by(name: 'ruby')
208-
patch '/posts/3/links/section', { 'data' => {type: 'sections', id: ruby.id.to_s }}.to_json
208+
patch '/posts/3/relationships/section', { 'data' => {type: 'sections', id: ruby.id.to_s }}.to_json
209209

210210
assert_equal 415, status
211211
end
212212

213213
def test_patch_update_association_has_one
214214
ruby = Section.find_by(name: 'ruby')
215-
patch '/posts/3/links/section', { 'data' => {type: 'sections', id: ruby.id.to_s }}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
215+
patch '/posts/3/relationships/section', { 'data' => {type: 'sections', id: ruby.id.to_s }}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
216216

217217
assert_equal 204, status
218218
end
219219

220220
def test_put_update_association_has_one
221221
ruby = Section.find_by(name: 'ruby')
222-
put '/posts/3/links/section', { 'data' => {type: 'sections', id: ruby.id.to_s }}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
222+
put '/posts/3/relationships/section', { 'data' => {type: 'sections', id: ruby.id.to_s }}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
223223

224224
assert_equal 204, status
225225
end
@@ -228,14 +228,14 @@ def test_patch_update_association_has_many_acts_as_set
228228
# Comments are acts_as_set=false so PUT/PATCH should respond with 403
229229

230230
rogue = Comment.find_by(body: 'Rogue Comment Here')
231-
patch '/posts/5/links/comments', { 'data' => [{type: 'comments', id: rogue.id.to_s }]}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
231+
patch '/posts/5/relationships/comments', { 'data' => [{type: 'comments', id: rogue.id.to_s }]}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
232232

233233
assert_equal 403, status
234234
end
235235

236236
def test_post_update_association_has_many
237237
rogue = Comment.find_by(body: 'Rogue Comment Here')
238-
post '/posts/5/links/comments', { 'data' => [{type: 'comments', id: rogue.id.to_s }]}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
238+
post '/posts/5/relationships/comments', { 'data' => [{type: 'comments', id: rogue.id.to_s }]}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
239239

240240
assert_equal 204, status
241241
end
@@ -244,7 +244,7 @@ def test_put_update_association_has_many_acts_as_set
244244
# Comments are acts_as_set=false so PUT/PATCH should respond with 403. Note: JR currently treats PUT and PATCH as equivalent
245245

246246
rogue = Comment.find_by(body: 'Rogue Comment Here')
247-
put '/posts/5/links/comments', { 'data' => [{type: 'comments', id: rogue.id.to_s }]}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
247+
put '/posts/5/relationships/comments', { 'data' => [{type: 'comments', id: rogue.id.to_s }]}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
248248

249249
assert_equal 403, status
250250
end
@@ -416,7 +416,7 @@ def test_flow_link_has_one_self_link
416416
assert_equal 200, status
417417
assert_hash_equals(json_response, {
418418
'links' => {
419-
'self' => 'http://www.example.com/posts/1/links/author',
419+
'self' => 'http://www.example.com/posts/1/relationships/author',
420420
'related' => 'http://www.example.com/posts/1/author'
421421
},
422422
'data' => {type: 'people', id: '1'}
@@ -433,7 +433,7 @@ def test_flow_link_has_many_self_link
433433
assert_hash_equals(json_response,
434434
{
435435
'links' => {
436-
'self' => 'http://www.example.com/posts/1/links/tags',
436+
'self' => 'http://www.example.com/posts/1/relationships/tags',
437437
'related' => 'http://www.example.com/posts/1/tags'
438438
},
439439
'data' => [
@@ -460,7 +460,7 @@ def test_flow_link_has_many_self_link_put
460460
assert_hash_equals(json_response,
461461
{
462462
'links' => {
463-
'self' => 'http://www.example.com/posts/5/links/tags',
463+
'self' => 'http://www.example.com/posts/5/relationships/tags',
464464
'related' => 'http://www.example.com/posts/5/tags'
465465
},
466466
'data' => [
@@ -644,7 +644,7 @@ def test_patch_formatted_dasherized_replace_has_many
644644
def test_post_has_many_link
645645
JSONAPI.configuration.route_format = :dasherized_route
646646
JSONAPI.configuration.json_key_format = :dasherized_key
647-
post '/api/v6/purchase-orders/3/links/line-items',
647+
post '/api/v6/purchase-orders/3/relationships/line-items',
648648
{
649649
'data' => [
650650
{'type' => 'line-items', 'id' => '3'},
@@ -658,7 +658,7 @@ def test_post_has_many_link
658658
def test_patch_has_many_link
659659
JSONAPI.configuration.route_format = :dasherized_route
660660
JSONAPI.configuration.json_key_format = :dasherized_key
661-
patch '/api/v6/purchase-orders/3/links/order-flags',
661+
patch '/api/v6/purchase-orders/3/relationships/order-flags',
662662
{
663663
'data' => [
664664
{'type' => 'order-flags', 'id' => '1'},
@@ -672,7 +672,7 @@ def test_patch_has_many_link
672672
def test_patch_has_one
673673
JSONAPI.configuration.route_format = :dasherized_route
674674
JSONAPI.configuration.json_key_format = :dasherized_key
675-
patch '/api/v6/line-items/5/links/purchase-order',
675+
patch '/api/v6/line-items/5/relationships/purchase-order',
676676
{
677677
'data' => {'type' => 'purchase-orders', 'id' => '3'}
678678
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE

test/integration/routes/routes_test.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,37 @@ def test_routing_posts_show
1818
end
1919

2020
def test_routing_posts_links_author_show
21-
assert_routing({path: '/posts/1/links/author', method: :get},
21+
assert_routing({path: '/posts/1/relationships/author', method: :get},
2222
{controller: 'posts', action: 'show_association', post_id: '1', association: 'author'})
2323
end
2424

2525
def test_routing_posts_links_author_destroy
26-
assert_routing({path: '/posts/1/links/author', method: :delete},
26+
assert_routing({path: '/posts/1/relationships/author', method: :delete},
2727
{controller: 'posts', action: 'destroy_association', post_id: '1', association: 'author'})
2828
end
2929

3030
def test_routing_posts_links_author_update
31-
assert_routing({path: '/posts/1/links/author', method: :patch},
31+
assert_routing({path: '/posts/1/relationships/author', method: :patch},
3232
{controller: 'posts', action: 'update_association', post_id: '1', association: 'author'})
3333
end
3434

3535
def test_routing_posts_links_tags_show
36-
assert_routing({path: '/posts/1/links/tags', method: :get},
36+
assert_routing({path: '/posts/1/relationships/tags', method: :get},
3737
{controller: 'posts', action: 'show_association', post_id: '1', association: 'tags'})
3838
end
3939

4040
def test_routing_posts_links_tags_destroy
41-
assert_routing({path: '/posts/1/links/tags/1,2', method: :delete},
41+
assert_routing({path: '/posts/1/relationships/tags/1,2', method: :delete},
4242
{controller: 'posts', action: 'destroy_association', post_id: '1', keys: '1,2', association: 'tags'})
4343
end
4444

4545
def test_routing_posts_links_tags_create
46-
assert_routing({path: '/posts/1/links/tags', method: :post},
46+
assert_routing({path: '/posts/1/relationships/tags', method: :post},
4747
{controller: 'posts', action: 'create_association', post_id: '1', association: 'tags'})
4848
end
4949

5050
def test_routing_posts_links_tags_update_acts_as_set
51-
assert_routing({path: '/posts/1/links/tags', method: :patch},
51+
assert_routing({path: '/posts/1/relationships/tags', method: :patch},
5252
{controller: 'posts', action: 'update_association', post_id: '1', association: 'tags'})
5353
end
5454

@@ -64,13 +64,13 @@ def test_routing_v1_posts_delete
6464
end
6565

6666
def test_routing_v1_posts_links_writer_show
67-
assert_routing({path: '/api/v1/posts/1/links/writer', method: :get},
67+
assert_routing({path: '/api/v1/posts/1/relationships/writer', method: :get},
6868
{controller: 'api/v1/posts', action: 'show_association', post_id: '1', association: 'writer'})
6969
end
7070

7171
# V2
7272
def test_routing_v2_posts_links_author_show
73-
assert_routing({path: '/api/v2/posts/1/links/author', method: :get},
73+
assert_routing({path: '/api/v2/posts/1/relationships/author', method: :get},
7474
{controller: 'api/v2/posts', action: 'show_association', post_id: '1', association: 'author'})
7575
end
7676

@@ -100,7 +100,7 @@ def test_routing_v4_expenseEntries_resources
100100
assert_routing({path: '/api/v4/expenseEntries/1', method: :get},
101101
{action: 'show', controller: 'api/v4/expense_entries', id: '1'})
102102

103-
assert_routing({path: '/api/v4/expenseEntries/1/links/isoCurrency', method: :get},
103+
assert_routing({path: '/api/v4/expenseEntries/1/relationships/isoCurrency', method: :get},
104104
{controller: 'api/v4/expense_entries', action: 'show_association', expense_entry_id: '1', association: 'iso_currency'})
105105
end
106106

@@ -119,7 +119,7 @@ def test_routing_v5_expenseEntries_resources
119119
assert_routing({path: '/api/v5/expense-entries/1', method: :get},
120120
{action: 'show', controller: 'api/v5/expense_entries', id: '1'})
121121

122-
assert_routing({path: '/api/v5/expense-entries/1/links/iso-currency', method: :get},
122+
assert_routing({path: '/api/v5/expense-entries/1/relationships/iso-currency', method: :get},
123123
{controller: 'api/v5/expense_entries', action: 'show_association', expense_entry_id: '1', association: 'iso_currency'})
124124
end
125125

@@ -129,7 +129,7 @@ def test_routing_authors_show
129129
end
130130

131131
def test_routing_author_links_posts_create_not_acts_as_set
132-
assert_routing({path: '/api/v5/authors/1/links/posts', method: :post},
132+
assert_routing({path: '/api/v5/authors/1/relationships/posts', method: :post},
133133
{controller: 'api/v5/authors', action: 'create_association', author_id: '1', association: 'posts'})
134134
end
135135

@@ -146,12 +146,12 @@ def test_routing_primary_key_jsonapi_resources
146146
# end
147147

148148
# def test_routing_posts_links_author_except_destroy
149-
# assert_routing({ path: '/api/v3/posts/1/links/author', method: :delete },
149+
# assert_routing({ path: '/api/v3/posts/1/relationships/author', method: :delete },
150150
# { controller: 'api/v3/posts', action: 'destroy_association', post_id: '1', association: 'author' })
151151
# end
152152
#
153153
# def test_routing_posts_links_tags_only_create_show
154-
# assert_routing({ path: '/api/v3/posts/1/links/tags/1,2', method: :delete },
154+
# assert_routing({ path: '/api/v3/posts/1/relationships/tags/1,2', method: :delete },
155155
# { controller: 'api/v3/posts', action: 'destroy_association', post_id: '1', keys: '1,2', association: 'tags' })
156156
# end
157157

0 commit comments

Comments
 (0)