You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/general/adapters.md
+86-15Lines changed: 86 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,38 @@
1
1
# Adapters
2
2
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.
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.
7
36
8
37
## Built in Adapters
9
38
@@ -14,47 +43,89 @@ Doesn't follow any specific convention.
14
43
15
44
### JSON
16
45
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
+
18
50
Doesn't follow any specific convention.
19
51
20
52
### JSON API
21
53
22
54
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.
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):
32
101
33
102
```ruby
34
-
render @posts, include:'authors,comments'
103
+
render @posts, include:params[:include]
35
104
```
36
105
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.
38
109
39
110
## Choosing an adapter
40
111
41
112
If you want to use a specify a default adapter, such as JsonApi, you can change this in an initializer:
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.**
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**
Copy file name to clipboardExpand all lines: docs/general/getting_started.md
+13-23Lines changed: 13 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,5 @@
1
1
# Getting Started
2
2
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
-
19
3
## Creating a Serializer
20
4
21
5
The easiest way to create a new serializer is to generate a new resource, which
@@ -33,7 +17,7 @@ the serializer generator:
33
17
$ rails g serializer post
34
18
```
35
19
36
-
The generated seralizer will contain basic `attributes` and
20
+
The generated serializer will contain basic `attributes` and
37
21
`has_many`/`has_one`/`belongs_to` declarations, based on the model. For example:
38
22
39
23
```ruby
@@ -42,7 +26,6 @@ class PostSerializer < ActiveModel::Serializer
42
26
43
27
has_many :comments
44
28
has_one :author
45
-
46
29
end
47
30
```
48
31
@@ -53,13 +36,20 @@ class CommentSerializer < ActiveModel::Serializer
53
36
attributes :name, :body
54
37
55
38
belongs_to :post_id
56
-
57
39
end
58
40
```
59
41
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
+
60
50
### Namespaced Models
61
51
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`).
63
53
64
54
### Model Associations and Nested Serializers
65
55
@@ -69,7 +59,7 @@ class PostSerializer < ActiveModel::Serializer
69
59
has_many :comments
70
60
end
71
61
```
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.
73
63
74
64
For example, in the following situation:
75
65
@@ -86,11 +76,11 @@ class PostSerializer < ActiveModel::Serializer
86
76
end
87
77
```
88
78
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`).
90
80
91
81
## Rails Integration
92
82
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:
0 commit comments