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
_Windows Build Status -_[](https://ci.appveyor.com/project/joaomdmoura/active-model-serializers/branch/master)
8
8
9
+
# About
10
+
9
11
ActiveModelSerializers brings convention over configuration to your JSON generation.
10
12
11
-
AMS does this through two components: **serializers** and **adapters**.
13
+
ActiveModelSerializers works through two components: **serializers** and **adapters**.
14
+
12
15
Serializers describe _which_ attributes and relationships should be serialized.
16
+
13
17
Adapters describe _how_ attributes and relationships should be serialized.
14
18
15
-
By default AMS will use the **Attributes Adapter**. But we strongly advise you to use **JsonApi Adapter** that follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
16
-
Check how to change the adapter in the sections bellow.
19
+
SerializableResource co-ordinates the resource, Adapter and Serializer to produce the
20
+
resource serialization, which has the `#as_json`, `#to_json` and `#serializable_hash`
21
+
methods used by the Rails JSON Renderer. (SerializableResource actually delegates
22
+
these methods to the adapter.)
23
+
24
+
By default ActiveModelSerializers will use the **Attributes Adapter**.
25
+
But we strongly advise you to use **JsonApi Adapter**, which
26
+
follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
27
+
Check how to change the adapter in the sections below.
17
28
18
29
# Documentation
19
30
@@ -24,222 +35,13 @@ Master
24
35
25
36
# RELEASE CANDIDATE, PLEASE READ
26
37
27
-
This is the master branch of AMS. It will become the `0.10.0` release when it's
38
+
This is the master branch of ActiveModelSerializers. It will become the `0.10.0` release when it's
28
39
ready. Currently this is a release candidate. This is **not** backward
29
40
compatible with `0.9.0` or `0.8.0`.
30
41
31
42
`0.10.x` will be based on the `0.8.0` code, but with a more flexible
32
43
architecture. We'd love your help. [Learn how you can help here.](https://github.com/rails-api/active_model_serializers/blob/master/CONTRIBUTING.md)
33
44
34
-
## Example
35
-
36
-
Given two models, a `Post(title: string, body: text)` and a
37
-
`Comment(name: string, body: text, post_id: integer)`, you will have two
38
-
serializers:
39
-
40
-
```ruby
41
-
classPostSerializer < ActiveModel::Serializer
42
-
cache key:'posts', expires_in:3.hours
43
-
attributes :title, :body
44
-
45
-
has_many :comments
46
-
end
47
-
```
48
-
49
-
and
50
-
51
-
```ruby
52
-
classCommentSerializer < ActiveModel::Serializer
53
-
attributes :name, :body
54
-
55
-
belongs_to :post
56
-
end
57
-
```
58
-
59
-
Generally speaking, you as a user of AMS will write (or generate) these
60
-
serializer classes. If you want to use a different adapter, such as a JsonApi, you can
`meta` will only be included in your response if you are using an Adapter that supports `root`,
147
-
as JsonAPI and Json adapters, the default adapter (Attributes) doesn't have `root`.
148
-
149
-
### Using a serializer without `render`
150
-
151
-
At times, you might want to use a serializer without rendering it to the view. For those cases, you can create an instance of `ActiveModel::SerializableResource` with
152
-
the resource you want to be serialized and call `.serializable_hash`.
If you want to override any association, you can use:
169
-
170
-
```ruby
171
-
classPostSerializer < ActiveModel::Serializer
172
-
attributes :id, :body
173
-
174
-
has_many :comments
175
-
176
-
defcomments
177
-
object.comments.active
178
-
end
179
-
end
180
-
```
181
-
182
-
### Overriding attribute methods
183
-
184
-
If you want to override any attribute, you can use:
185
-
186
-
```ruby
187
-
classPostSerializer < ActiveModel::Serializer
188
-
attributes :id, :body
189
-
190
-
has_many :comments
191
-
192
-
defbody
193
-
object.body.downcase
194
-
end
195
-
end
196
-
```
197
-
198
-
### Built in Adapters
199
-
200
-
#### Attributes
201
-
202
-
It's the default adapter, it generates a json response without a root key.
203
-
Doesn't follow any specific convention.
204
-
205
-
#### JSON
206
-
207
-
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 with the objects being serialized.
208
-
Doesn't follow any specific convention.
209
-
210
-
#### JSON API
211
-
212
-
This adapter follows 1.0 of the format specified in
213
-
[jsonapi.org/format](http://jsonapi.org/format). It will include the associated
214
-
resources in the `"included"` member when the resource names are included in the
215
-
`include` option. Including nested associated resources is also supported.
In addition, two types of wildcards may be used. `*` includes one level of associations, and `**` includes all recursively. These can be combined with other paths.
224
-
225
-
```ruby
226
-
render @posts, include:'**'# or '*' for a single layer
227
-
```
228
-
229
-
The following would render posts and include the author, the author's comments, and every resource referenced by the author's comments (recursively). It could be combined, like above, with other paths in any combination desired.
230
-
231
-
```ruby
232
-
render @posts, include:'author.comments.**'
233
-
```
234
-
235
-
The JSON API [specifies](http://jsonapi.org/format/#fetching-includes) that the user may supply a parameter specifying which related resources are to be included:
236
-
237
-
```ruby
238
-
render @posts, include: params[:include]
239
-
```
240
-
241
-
This raises some security concerns since the user could pass in `include=**`, so filter the values for `include` appropriately if you decide to support this JSON API feature.
242
-
243
45
## Installation
244
46
245
47
Add this line to your application's Gemfile:
@@ -254,6 +56,8 @@ And then execute:
254
56
$ bundle
255
57
```
256
58
59
+
*ActiveModelSerializers is already included on Rails >= 5*
60
+
257
61
## Creating a Serializer
258
62
259
63
The easiest way to create a new serializer is to generate a new resource, which
@@ -299,91 +103,68 @@ The `has_many`, `has_one`, and `belongs_to` declarations describe relationships
299
103
resources. By default, when you serialize a `Post`, you will get its `Comments`
300
104
as well.
301
105
302
-
You may also use the `:serializer` option to specify a custom serializer class, for example:
106
+
For more information, see [Serializers](docs/general/serializers.md).
To cache a serializer, call ```cache``` and pass its options.
329
-
The options are the same options of ```ActiveSupport::Cache::Store```, plus
330
-
a ```key``` option that will be the prefix of the object cache
331
-
on a pattern ```"#{key}/#{object.id}-#{object.updated_at}"```.
332
-
333
-
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.
334
-
335
-
**[NOTE] Every object is individually cached.**
336
-
337
-
**[NOTE] The cache is automatically expired after an object is updated, but it's not deleted.**
resource=SomeResource.new(title:'ActiveModelSerializers', body:'Convention over configuration')
341
129
```
342
130
343
-
Take the example bellow:
131
+
Given a serializer for the serializable model:
344
132
345
133
```ruby
346
-
classPostSerializer < ActiveModel::Serializer
347
-
cache key:'post', expires_in:3.hours
348
-
attributes :title, :body
349
-
350
-
has_many :comments
134
+
classSomeSerializer < ActiveModel::Serializer
135
+
attribute :title, key::name
136
+
attributes :body
351
137
end
352
138
```
353
139
354
-
On this example every ```Post``` object will be cached with
355
-
the key ```"post/#{post.id}-#{post.updated_at}"```. You can use this key to expire it as you want,
356
-
but in this case it will be automatically expired after 3 hours.
357
-
358
-
### Fragment Caching
359
-
360
-
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.
361
-
362
-
You can define the attribute by using ```only``` or ```except``` option on cache method.
363
-
364
-
**[NOTE] Cache serializers will be used at their relationships**
0 commit comments