Skip to content

Commit d1097f2

Browse files
committed
[DOCS] Refactor, update, create documentation [ci skip]
1 parent f562449 commit d1097f2

19 files changed

+543
-407
lines changed

README.md

Lines changed: 59 additions & 320 deletions
Large diffs are not rendered by default.

docs/README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
# Docs - ActiveModel::Serializer 0.10.x
22

3-
This is the documentation of AMS, it's focused on the **0.10.x version.**
3+
This is the documentation of ActiveModelSerializers, it's focused on the **0.10.x version.**
44

55
-----
66

77
## General
88

99
- [Getting Started](general/getting_started.md)
10-
- [Adapters](general/adapters.md)
1110
- [Configuration Options](general/configuration_options.md)
11+
- [Serializers](general/serializers.md)
12+
- [Adapters](general/adapters.md)
13+
- [Rendering](general/rendering.md)
14+
- [Caching](general/caching.md)
1215
- [Logging](general/logging.md)
1316
- [Instrumentation](general/instrumentation.md)
1417

1518
## How to
1619

1720
- [How to add root key](howto/add_root_key.md)
1821
- [How to add pagination links](howto/add_pagination_links.md)
19-
- [Using AMS Outside Of Controllers](howto/outside_controller_use.md)
22+
- [Using ActiveModelSerializers Outside Of Controllers](howto/outside_controller_use.md)
2023

2124
## Integrations
22-
| Integration | Supported AMS versions | Gem name and/or link
25+
| Integration | Supported ActiveModelSerializers versions | Gem name and/or link
2326
|----|-----|----
2427
| Ember.js | 0.9.x | [active-model-adapter](https://github.com/ember-data/active-model-adapter)
2528
| Ember.js | 0.10.x + | [docs/integrations/ember-and-json-api.md](integrations/ember-and-json-api.md)

docs/general/adapters.md

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
11
# Adapters
22

3-
AMS works through two components: **serializers** and **adapters**.
4-
Serializers describe _which_ attributes and relationships should be serialized.
5-
Adapters describe _how_ attributes and relationships should be serialized.
6-
You can use one of the built-in adapters (```Attributes``` is the default one) or create one by yourself, but you won't need to implement an adapter unless you wish to use a new format or media type with AMS.
3+
ActiveModelSerializers offers the ability to configure which adapter
4+
to use both globally and/or when serializing (usually when rendering).
5+
6+
The global adapter configuration is set on [`ActiveModelSerializers.config`](configuration_options.md).
7+
It should be set only once, preferably at initialization.
8+
9+
For example:
10+
11+
```ruby
12+
ActiveModelSerializers.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
13+
```
14+
15+
or
16+
17+
```ruby
18+
ActiveModelSerializers.config.adapter = :json_api
19+
```
20+
21+
or
22+
23+
```ruby
24+
ActiveModelSerializers.config.adapter = :json
25+
```
26+
27+
The local adapter option is in the format `adapter: adapter`, where `adapter` is
28+
any of the same values as set globally.
29+
30+
The configured adapter can be set as a symbol, class, or class name, as described in
31+
[Advanced adapter configuration](adapters.md#Advanced adapter configuration).
32+
33+
The `Attributes` adapter does not include a root key. It is just the serialized attributes.
34+
35+
Use either the `JSON` or `JSON API` adapters if you want the response document to have a root key.
736

837
## Built in Adapters
938

@@ -14,47 +43,89 @@ Doesn't follow any specific convention.
1443

1544
### JSON
1645

17-
It also generates a json response but always with a root key. The root key **can't be overridden**, and will be automatically defined accordingly to the objects being serialized.
46+
The response document always with a root key.
47+
48+
The root key **can't be overridden**, and will be derived from the resource being serialized.
49+
1850
Doesn't follow any specific convention.
1951

2052
### JSON API
2153

2254
This adapter follows **version 1.0** of the format specified in
23-
[jsonapi.org/format](http://jsonapi.org/format). It will include the associated
24-
resources in the `"included"` member when the resource names are included in the
25-
`include` option.
55+
[jsonapi.org/format](http://jsonapi.org/format).
56+
57+
#### Included
58+
59+
It will include the associated resources in the `"included"` member
60+
when the resource names are included in the `include` option.
61+
Including nested associated resources is also supported.
2662

2763
```ruby
28-
render @posts, include: ['authors', 'comments']
64+
render @posts, include: ['author', 'comments', 'comments.author']
65+
# or
66+
render @posts, include: 'author,comments,comments.author'
2967
```
3068

31-
or
69+
In addition, two types of wildcards may be used:
70+
71+
- `*` includes one level of associations.
72+
- `**` includes all recursively.
73+
74+
These can be combined with other paths.
75+
76+
```ruby
77+
render @posts, include: '**' # or '*' for a single layer
78+
```
79+
80+
The format of the `include` option can be either:
81+
82+
- a String composed of a comma-separated list of [relationship paths](http://jsonapi.org/format/#fetching-includes).
83+
- an Array of Symbols and Hashes.
84+
- a mix of both.
85+
86+
The following would render posts and include:
87+
88+
- the author
89+
- the author's comments, and
90+
- every resource referenced by the author's comments (recursively).
91+
92+
It could be combined, like above, with other paths in any combination desired.
93+
94+
```ruby
95+
render @posts, include: 'author.comments.**'
96+
```
97+
98+
##### Security Considerations
99+
100+
Since the included options may come from the query params (i.e. user-controller):
32101

33102
```ruby
34-
render @posts, include: 'authors,comments'
103+
render @posts, include: params[:include]
35104
```
36105

37-
The format of the `include` option can be either a String composed of a comma-separated list of [relationship paths](http://jsonapi.org/format/#fetching-includes), an Array of Symbols and Hashes, or a mix of both.
106+
The user could pass in `include=**`.
107+
108+
We recommend filtering any user-supplied includes appropriately.
38109

39110
## Choosing an adapter
40111

41112
If you want to use a specify a default adapter, such as JsonApi, you can change this in an initializer:
42113

43114
```ruby
44-
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
115+
ActiveModelSerializers.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
45116
```
46117

47118
or
48119

49120
```ruby
50-
ActiveModel::Serializer.config.adapter = :json_api
121+
ActiveModelSerializers.config.adapter = :json_api
51122
```
52123

53124
If you want to have a root key for each resource in your responses, you should use the Json or
54125
JsonApi adapters instead of the default Attributes:
55126

56127
```ruby
57-
ActiveModel::Serializer.config.adapter = :json
128+
ActiveModelSerializers.config.adapter = :json
58129
```
59130

60131
## Advanced adapter configuration

docs/general/caching.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Caching
2+
3+
To cache a serializer, call ```cache``` and pass its options.
4+
The options are the same options of ```ActiveSupport::Cache::Store```, plus
5+
a ```key``` option that will be the prefix of the object cache
6+
on a pattern ```"#{key}/#{object.id}-#{object.updated_at}"```.
7+
8+
The cache support is optimized to use the cached object in multiple request. An object cached on a ```show``` request will be reused at the ```index```. If there is a relationship with another cached serializer it will also be created and reused automatically.
9+
10+
**[NOTE] Every object is individually cached.**
11+
12+
**[NOTE] The cache is automatically expired after an object is updated, but it's not deleted.**
13+
14+
```ruby
15+
cache(options = nil) # options: ```{key, expires_in, compress, force, race_condition_ttl}```
16+
```
17+
18+
Take the example bellow:
19+
20+
```ruby
21+
class PostSerializer < ActiveModel::Serializer
22+
cache key: 'post', expires_in: 3.hours
23+
attributes :title, :body
24+
25+
has_many :comments
26+
end
27+
```
28+
29+
On this example every ```Post``` object will be cached with
30+
the key ```"post/#{post.id}-#{post.updated_at}"```. You can use this key to expire it as you want,
31+
but in this case it will be automatically expired after 3 hours.
32+
33+
## Fragment Caching
34+
35+
If there is some API endpoint that shouldn't be fully cached, you can still optimise it, using Fragment Cache on the attributes and relationships that you want to cache.
36+
37+
You can define the attribute by using ```only``` or ```except``` option on cache method.
38+
39+
**[NOTE] Cache serializers will be used at their relationships**
40+
41+
Example:
42+
43+
```ruby
44+
class PostSerializer < ActiveModel::Serializer
45+
cache key: 'post', expires_in: 3.hours, only: [:title]
46+
attributes :title, :body
47+
48+
has_many :comments
49+
end
50+
```

docs/general/configuration_options.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Configuration Options
22

3-
The following configuration options can be set on `ActiveModel::Serializer.config` inside an initializer.
3+
The following configuration options can be set on `ActiveModelSerializers.config`,
4+
preferably inside an initializer.
45

56
## General
67

@@ -17,3 +18,7 @@ The following configuration options can be set on `ActiveModel::Serializer.confi
1718
Default: `'1.0'`.
1819
- `jsonapi_toplevel_meta`: Optional metadata. Not included if empty.
1920
Default: `{}`.
21+
22+
## Hooks
23+
24+
To run a hook when ActiveModelSerializers is loaded, use `ActiveSupport.on_load(:active_model_serializers) do end`

docs/general/getting_started.md

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
# Getting Started
22

3-
## Installation
4-
5-
### ActiveModel::Serializer is already included on Rails >= 5
6-
7-
Add this line to your application's Gemfile:
8-
9-
```
10-
gem 'active_model_serializers'
11-
```
12-
13-
And then execute:
14-
15-
```
16-
$ bundle
17-
```
18-
193
## Creating a Serializer
204

215
The easiest way to create a new serializer is to generate a new resource, which
@@ -33,7 +17,7 @@ the serializer generator:
3317
$ rails g serializer post
3418
```
3519

36-
The generated seralizer will contain basic `attributes` and
20+
The generated serializer will contain basic `attributes` and
3721
`has_many`/`has_one`/`belongs_to` declarations, based on the model. For example:
3822

3923
```ruby
@@ -42,7 +26,6 @@ class PostSerializer < ActiveModel::Serializer
4226

4327
has_many :comments
4428
has_one :author
45-
4629
end
4730
```
4831

@@ -53,13 +36,20 @@ class CommentSerializer < ActiveModel::Serializer
5336
attributes :name, :body
5437

5538
belongs_to :post_id
56-
5739
end
5840
```
5941

42+
The attribute names are a **whitelist** of attributes to be serialized.
43+
44+
The `has_many`, `has_one`, and `belongs_to` declarations describe relationships between
45+
resources. By default, when you serialize a `Post`, you will get its `Comments`
46+
as well.
47+
48+
For more information, see [Serializers](docs/general/serializers.md).
49+
6050
### Namespaced Models
6151

62-
When serializing a model inside a namespace, such as `Api::V1::Post`, AMS will expect the corresponding serializer to be inside the same namespace (namely `Api::V1::PostSerializer`).
52+
When serializing a model inside a namespace, such as `Api::V1::Post`, ActiveModelSerializers will expect the corresponding serializer to be inside the same namespace (namely `Api::V1::PostSerializer`).
6353

6454
### Model Associations and Nested Serializers
6555

@@ -69,7 +59,7 @@ class PostSerializer < ActiveModel::Serializer
6959
has_many :comments
7060
end
7161
```
72-
AMS will look for `PostSerializer::CommentSerializer` in priority, and fall back to `::CommentSerializer` in case the former does not exist. This allows for more control over the way a model gets serialized as an association of an other model.
62+
ActiveModelSerializers will look for `PostSerializer::CommentSerializer` in priority, and fall back to `::CommentSerializer` in case the former does not exist. This allows for more control over the way a model gets serialized as an association of an other model.
7363

7464
For example, in the following situation:
7565

@@ -86,11 +76,11 @@ class PostSerializer < ActiveModel::Serializer
8676
end
8777
```
8878

89-
AMS will use `PostSerializer::CommentSerializer` (thus including only the `:body_short` attribute) when serializing a `Comment` as part of a `Post`, but use `::CommentSerializer` when serializing a `Comment` directly (thus including `:body, :date, :nb_likes`).
79+
ActiveModelSerializers will use `PostSerializer::CommentSerializer` (thus including only the `:body_short` attribute) when serializing a `Comment` as part of a `Post`, but use `::CommentSerializer` when serializing a `Comment` directly (thus including `:body, :date, :nb_likes`).
9080

9181
## Rails Integration
9282

93-
AMS will automatically integrate with you Rails app, you won't need to update your controller, this is a example of how it will look like:
83+
ActiveModelSerializers will automatically integrate with you Rails app, you won't need to update your controller, this is a example of how it will look like:
9484

9585
```ruby
9686
class PostsController < ApplicationController

0 commit comments

Comments
 (0)