Skip to content

Commit 09504f9

Browse files
author
Yohan Robert
committed
Update PR based on comments
1 parent 9965abd commit 09504f9

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Fixes:
3131
- [#1881](https://github.com/rails-api/active_model_serializers/pull/1881) ActiveModelSerializers::Model correctly works with string keys (@yevhene)
3232

3333
Misc:
34+
35+
- [#1981](https://github.com/rails-api/active_model_serializers/pull/1981) Improve link and pagination documentation(@groyoh)
3436
- [#1767](https://github.com/rails-api/active_model_serializers/pull/1767) Replace raising/rescuing `CollectionSerializer::NoSerializerError`,
3537
throw/catch `:no_serializer`. (@bf4)
3638
- [#1839](https://github.com/rails-api/active_model_serializers/pull/1839) `fields` tests demonstrating usage for both attributes and relationships. (@NullVoxPopuli)

docs/howto/add_link.md

+22-23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44

55
`ActiveModelSerializers` offers you many ways to add links in your JSON reponse depending on the adapter you are using.
66

7+
Note that within the following examples, we set the `adapter` option, but that is not needed if the `ActiveModelSerializers.config.adapter` variable is properly set. Also, some examples are given for both Rails and [non-Rails usage](outside_controller_use.md).
8+
9+
Unless stated otherwise, the examples given here will assume that the following classes are defined:
10+
11+
```ruby
12+
class User < ActiveModelSerializers::Model
13+
attributes :id, :name
14+
end
15+
16+
class UserSerializer < ActiveModel::Serializer
17+
attributes :id, :name
18+
end
19+
```
20+
721
## JSON API adapter
822

923
The JSON API specification allows the usage of the `links` member for representing links in three cases:
@@ -18,19 +32,9 @@ The JSON API specification allows the usage of the `links` member for representi
1832
In order to provide top-level links, you will need to specify the `links` option to the serialization:
1933

2034
```ruby
21-
class UserSerializer < ActiveModel::Serializer
22-
attributes :id, :name
23-
end
24-
2535
user = User.new(id: 1, name: "John")
26-
links = {
27-
home: "https://www.example.com/"
28-
}
29-
resource = ActiveModelSerializers::SerializableResource.new(
30-
user,
31-
adapter: :json_api,
32-
links: links
33-
)
36+
links = { home: "https://www.example.com/" }
37+
resource = ActiveModelSerializers::SerializableResource.new(user, adapter: :json_api, links: links)
3438
resource.to_json
3539
```
3640

@@ -57,18 +61,16 @@ When using Rails, you can add top-level links by providing the `links` option to
5761
class UserController < ActionController::Base
5862
def show
5963
user = User.new(id: 1, name: "John")
60-
links = {
61-
home: "https://www.example.com/"
62-
}
64+
links = { home: "https://www.example.com/" }
6365
render json: user, adapter: :json_api, links: links
6466
end
6567
end
6668
```
6769

6870
#### Pagination
6971

70-
`ActiveModelSerializers` will provides automatic pagination links for collections if use [Kaminari](https://github.com/amatsuda/kaminari)
71-
or [WillPaginate](https://github.com/mislav/will_paginate):
72+
`ActiveModelSerializers` will provide automatic pagination links for collections if you use [Kaminari](https://github.com/amatsuda/kaminari)
73+
or [WillPaginate](https://github.com/mislav/will_paginate) and the `ActiveModelSerializers.config.jsonapi_pagination_links_enabled` config is set to `true` (defaults to `true`). To disable this feature, you can simply set this config to `false`.
7274

7375
```ruby
7476
class UserController < ActionController::Base
@@ -108,15 +110,15 @@ Any of these actions would result in:
108110
}
109111
```
110112

111-
Note that `ActiveModelSerializers` pagination relies on a collection that has the methods `current_page`, `total_pages`, and `size`, such as are supported by both [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate). If you want to have paginated links your collections but don't want to use neither Kaminari nor WillPaginate, make sure to define these methods.
113+
Note that it is possible to opt-out `ActiveModelSerializers` pagination relies on a collection that has the methods `current_page`, `total_pages`, and `size`, such as are supported by both [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate). If you want to have paginated links your collections but don't want to use neither Kaminari nor WillPaginate, make sure to define these methods.
112114

113115
### [Resource link](http://jsonapi.org/format/#document-resource-object-links)
114116

115117
You can use the `link` class method to define the links you need in the resource's primary data:
116118

117119
```ruby
118120
class UserSerializer < ActiveModel::Serializer
119-
attributes :id, :name
121+
attributes :name
120122

121123
link(:self) { user_path(object.id) }
122124
link(:posts) { posts_path(user_id: object.id) }
@@ -190,7 +192,7 @@ If you only want the `links` member to appear you can set `include_data false` w
190192

191193
```ruby
192194
class UserSerializer < ActiveModel::Serializer
193-
attributes :id, :name
195+
attributes :name
194196
has_many :posts do
195197
link(:self) do
196198
include_data false
@@ -232,9 +234,6 @@ The `json` is not able to handle links defined by the `link` class method. Howev
232234
### Via `meta`
233235

234236
```ruby
235-
class UserSerializer < ActiveModel::Serializer
236-
attributes :id, :name
237-
end
238237
user = User.new(id: 1, name: "John")
239238
meta = {
240239
links: {

docs/howto/handle_pagination.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ resource = ActiveModelSerializers::SerializableResource.new(users, adapter: :jso
1818
resource.to_json
1919
```
2020

21-
* For usage of top-level links see [add_links.md#pagination].
21+
* For usage of top-level links [check here](add_links.md#pagination).
2222

2323
## JSON adapter
2424

0 commit comments

Comments
 (0)