Skip to content

Commit 50f2754

Browse files
vasilakisfilNullVoxPopuli
authored andcommitted
Add docs for links (#1909)
Add docs for links Add docs for links Add docs for links Add docs for links Add docs for links Add controller info Grammar fixing Improve docs some small wording changes Add pr to changelog
1 parent 11bd778 commit 50f2754

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Misc:
2828
- [#1878](https://github.com/rails-api/active_model_serializers/pull/1878) Cache key generation for serializers now uses `ActiveSupport::Cache.expand_cache_key` instead of `Array#join` by default and is also overridable. This change should be backward-compatible. (@markiz)
2929

3030
- [#1799](https://github.com/rails-api/active_model_serializers/pull/1799) Add documentation for setting the adapter. (@ScottKbka)
31+
- [#1909](https://github.com/rails-api/active_model_serializers/pull/1909) Add documentation for relationship links. (@vasilakisfil, @NullVoxPopuli)
3132

3233
### [v0.10.2 (2016-07-05)](https://github.com/rails-api/active_model_serializers/compare/v0.10.1...v0.10.2)
3334

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This is the documentation of ActiveModelSerializers, it's focused on the **0.10.
2424

2525
- [How to add root key](howto/add_root_key.md)
2626
- [How to add pagination links](howto/add_pagination_links.md)
27+
- [How to add relationship links](howto/add_relationship_links.md)
2728
- [Using ActiveModelSerializers Outside Of Controllers](howto/outside_controller_use.md)
2829
- [Testing ActiveModelSerializers](howto/test.md)
2930
- [Passing Arbitrary Options](howto/passing_arbitrary_options.md)

docs/howto/add_relationship_links.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
[Back to Guides](../README.md)
2+
3+
# How to add relationship links
4+
5+
ActiveModelSerializers offers you many ways to add links in your JSON, depending on your needs.
6+
The most common use case for links is supporting nested resources.
7+
8+
The following examples are without included relationship data (`include` param is empty),
9+
specifically the following Rails controller was used for these examples:
10+
11+
```ruby
12+
class Api::V1::UsersController < ApplicationController
13+
def show
14+
render jsonapi: User.find(params[:id]),
15+
serializer: Api::V1::UserSerializer,
16+
include: []
17+
end
18+
```
19+
20+
Bear in mind though that ActiveModelSerializers are [framework-agnostic](outside_controller_use.md), Rails is just a common example here.
21+
22+
### Links as an attribute of a resource
23+
**This is applicable to JSONAPI, JSON and Attributes adapters**
24+
25+
You can define an attribute in the resource, named `links`.
26+
27+
```ruby
28+
class Api::V1::UserSerializer < ActiveModel::Serializer
29+
attributes :id, :name, :links
30+
31+
def links
32+
{
33+
self: api_v1_user_path(object.id),
34+
microposts: api_v1_microposts_path(user_id: object.id)
35+
}
36+
end
37+
end
38+
```
39+
40+
This will resilt in (example is in jsonapi adapter):
41+
```json
42+
{
43+
"data": {
44+
"id": "1",
45+
"type": "users",
46+
"attributes": {
47+
"name": "Example User",
48+
"links": {
49+
"self": "/api/v1/users/1",
50+
"microposts": "/api/v1/microposts?user_id=1"
51+
}
52+
}
53+
}
54+
}
55+
```
56+
57+
58+
### Links as a property of the resource definiton
59+
**This is only applicable to JSONAPI adapter**
60+
61+
You can use the `links` class method to define the links you need in the resource's primary data.
62+
63+
```ruby
64+
class Api::V1::UserSerializer < ActiveModel::Serializer
65+
attributes :id, :name
66+
67+
link(:self) { api_v1_user_path(object.id) }
68+
link(:microposts) { api_v1_microposts_path(user_id: object.id) }
69+
end
70+
```
71+
72+
This will resilt in (example is in jsonapi adapter):
73+
```json
74+
{
75+
"data": {
76+
"id": "1",
77+
"type": "users",
78+
"attributes": {
79+
"name": "Example User"
80+
},
81+
"links": {
82+
"self": "/api/v1/users/1",
83+
"microposts": "/api/v1/microposts?user_id=1"
84+
}
85+
}
86+
}
87+
```
88+
89+
### Links that follow the JSONAPI spec
90+
**This is only applicable to JSONAPI adapter**
91+
92+
If you have a JSONAPI-strict client that you are working with (like `ember-data`)
93+
you need to construct the links inside the relationships. Also the link to fetch the
94+
relationship data must be under the `related` attribute, whereas to manipulate the
95+
relationship (in case of many-to-many relationship) must be under the `self` attribute.
96+
97+
You can find more info in the [spec](http://jsonapi.org/format/#document-resource-object-relationships).
98+
99+
Here is how you can do this:
100+
101+
```ruby
102+
class Api::V1::UserSerializer < ActiveModel::Serializer
103+
attributes :id, :name
104+
105+
has_many :microposts, serializer: Api::V1::MicropostSerializer do
106+
link(:related) { api_v1_microposts_path(user_id: object.id) }
107+
end
108+
109+
#this is needed to avoid n+1, gem core devs are working to remove this necessity
110+
#more on: https://github.com/rails-api/active_model_serializers/issues/1325
111+
def microposts
112+
object.microposts.loaded ? object.microposts : object.microposts.none
113+
end
114+
end
115+
```
116+
117+
This will result in:
118+
119+
```json
120+
{
121+
"data": {
122+
"id": "1",
123+
"type": "users",
124+
"attributes": {
125+
"name": "Example User"
126+
},
127+
"relationships": {
128+
"microposts": {
129+
"data": [],
130+
"links": {
131+
"related": "/api/v1/microposts?user_id=1"
132+
}
133+
}
134+
}
135+
}
136+
}
137+
```

0 commit comments

Comments
 (0)