Skip to content

Commit 5b953ff

Browse files
committed
Address concerns from #1018 commit c59668e
1 parent c59668e commit 5b953ff

File tree

5 files changed

+51
-45
lines changed

5 files changed

+51
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Breaking changes:
44
Features:
5+
- [#1018](https://github.com/rails-api/active_model_serializers/pull/1018) Add more tests and docs for top-level links (@leandrocp)
56
Fixes:
67
- [#1501](https://github.com/rails-api/active_model_serializers/pull/1501) Adds tests for SerializableResource::use_adapter?,doc typos (@domitian)
78
- [#1488](https://github.com/rails-api/active_model_serializers/pull/1488) Require ActiveSupport's string inflections (@nate00)
@@ -66,8 +67,6 @@ Features:
6667
CollectionSerializer for clarity, add ActiveModelSerializers.config.collection_serializer (@bf4)
6768
- [#1295](https://github.com/rails-api/active_model_serializers/pull/1295) Add config `serializer_lookup_enabled` that,
6869
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)
7170

7271
Fixes:
7372

docs/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ 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)
2726

2827
## Integrations
2928

docs/general/rendering.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,46 @@ PR please :)
103103

104104
#### links
105105

106-
PR please :)
106+
##### How to add top-level links
107+
108+
JsonApi supports a [links object](http://jsonapi.org/format/#document-links) to be specified at top-level, that you can specify in the `render`:
109+
110+
```ruby
111+
links_object = {
112+
href: "http://example.com/api/posts",
113+
meta: {
114+
count: 10
115+
}
116+
}
117+
render json: @posts, links: links_object
118+
```
119+
120+
That's the result:
121+
122+
```json
123+
{
124+
"data": [
125+
{
126+
"type": "posts",
127+
"id": "1",
128+
"attributes": {
129+
"title": "JSON API is awesome!",
130+
"body": "You should be using JSON API",
131+
"created": "2015-05-22T14:56:29.000Z",
132+
"updated": "2015-05-22T14:56:28.000Z"
133+
}
134+
}
135+
],
136+
"links": {
137+
"href": "http://example.com/api/posts",
138+
"meta": {
139+
"count": 10
140+
}
141+
}
142+
}
143+
```
144+
145+
This feature is specific to JsonApi, so you have to use the use the [JsonApi Adapter](adapters.md#jsonapi)
107146

108147
### serializer_opts
109148

docs/howto/add_top_level_links.md

Lines changed: 0 additions & 40 deletions
This file was deleted.

test/adapter/json_api/links_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,16 @@ def test_nil_toplevel_links
6565
adapter: :json_api,
6666
links: nil
6767
).serializable_hash
68-
assert_equal(nil, hash[:links])
68+
refute hash.key?(:links), 'No links key to be output'
69+
end
70+
71+
def test_nil_toplevel_links_json_adapter
72+
hash = ActiveModel::SerializableResource.new(
73+
@post,
74+
adapter: :json,
75+
links: nil
76+
).serializable_hash
77+
refute hash.key?(:links), 'No links key to be output'
6978
end
7079

7180
def test_resource_links

0 commit comments

Comments
 (0)