Skip to content

Commit c59668e

Browse files
committed
Merge branch 'leandrocp-add-top-level-links'
Needs followup - #1018 (comment) - #1018 (comment) - #1018 (comment) - #1018 (comment)
2 parents dcbe4ef + b55fc32 commit c59668e

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ tmp
2222
.ruby-version
2323
.ruby-gemset
2424
vendor/bundle
25+
tags

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ Features:
6666
CollectionSerializer for clarity, add ActiveModelSerializers.config.collection_serializer (@bf4)
6767
- [#1295](https://github.com/rails-api/active_model_serializers/pull/1295) Add config `serializer_lookup_enabled` that,
6868
when disabled, requires serializers to explicitly specified. (@trek)
69+
- [#1247](https://github.com/rails-api/active_model_serializers/pull/1247) Add top-level links (@beauby)
70+
* Add more tests and docs for top-level links (@leandrocp)
6971

7072
Fixes:
7173

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This is the documentation of ActiveModelSerializers, it's focused on the **0.10.
2323
- [How to add pagination links](howto/add_pagination_links.md)
2424
- [Using ActiveModelSerializers Outside Of Controllers](howto/outside_controller_use.md)
2525
- [Testing ActiveModelSerializers](howto/test.md)
26+
- [How to add top-level links](howto/add_top_level_links.md) (```JSON-API``` only)
2627

2728
## Integrations
2829

docs/howto/add_top_level_links.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# How to add top-level links
2+
3+
JsonApi supports a [links object](http://jsonapi.org/format/#document-links) to be specified at top-level, that you can specify in the `render`:
4+
5+
```ruby
6+
links_object = {
7+
href: "http://example.com/api/posts",
8+
meta: {
9+
count: 10
10+
}
11+
}
12+
render json: @posts, links: links_object
13+
```
14+
15+
That's the result:
16+
17+
```json
18+
{
19+
"data": [
20+
{
21+
"type": "posts",
22+
"id": "1",
23+
"attributes": {
24+
"title": "JSON API is awesome!",
25+
"body": "You should be using JSON API",
26+
"created": "2015-05-22T14:56:29.000Z",
27+
"updated": "2015-05-22T14:56:28.000Z"
28+
}
29+
}
30+
],
31+
"links": {
32+
"href": "http://example.com/api/posts",
33+
"meta": {
34+
"count": 10
35+
}
36+
}
37+
}
38+
```
39+
40+
This feature is specific to JsonApi, so you have to use the use the [JsonApi Adapter](https://github.com/rails-api/active_model_serializers/blob/master/docs/general/adapters.md#jsonapi)

test/action_controller/serialization_test.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ def render_array_using_implicit_serializer_and_meta
4545
render json: @profiles, meta: { total: 10 }
4646
end
4747

48+
def render_array_using_implicit_serializer_and_links
49+
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
50+
@profiles = [
51+
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
52+
]
53+
54+
render json: @profiles, links: { self: 'http://example.com/api/profiles/1' }
55+
end
56+
end
57+
4858
def render_object_with_cache_enabled
4959
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
5060
@author = Author.new(id: 1, name: 'Joao Moura.')
@@ -254,6 +264,29 @@ def test_render_array_using_implicit_serializer_and_meta
254264
assert_equal expected.to_json, @response.body
255265
end
256266

267+
def test_render_array_using_implicit_serializer_and_links
268+
get :render_array_using_implicit_serializer_and_links
269+
270+
expected = {
271+
data: [
272+
{
273+
id: assigns(:profiles).first.id.to_s,
274+
type: 'profiles',
275+
attributes: {
276+
name: 'Name 1',
277+
description: 'Description 1'
278+
}
279+
}
280+
],
281+
links: {
282+
self: 'http://example.com/api/profiles/1'
283+
}
284+
}
285+
286+
assert_equal 'application/json', @response.content_type
287+
assert_equal expected.to_json, @response.body
288+
end
289+
257290
def test_render_with_cache_enable
258291
expected = {
259292
id: 1,

test/adapter/json_api/links_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ def test_toplevel_links
5959
assert_equal(expected, hash[:links])
6060
end
6161

62+
def test_nil_toplevel_links
63+
hash = ActiveModel::SerializableResource.new(
64+
@post,
65+
adapter: :json_api,
66+
links: nil
67+
).serializable_hash
68+
assert_equal(nil, hash[:links])
69+
end
70+
6271
def test_resource_links
6372
hash = serializable(@author, adapter: :json_api).serializable_hash
6473
expected = {

0 commit comments

Comments
 (0)