Skip to content

Commit 357d0d4

Browse files
committed
Merge pull request #994 from joaomdmoura/master
Starting Docs structure
2 parents d714094 + 63436c7 commit 357d0d4

File tree

4 files changed

+201
-0
lines changed

4 files changed

+201
-0
lines changed

docs/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Docs - ActiveModel::Serializer 0.10.x
2+
3+
This is the documentation of AMS, it's focused on the **0.10.x version.**
4+
5+
-----
6+
7+
## General
8+
9+
- [Getting Started](general/getting_started.md)
10+
- [Adapters](general/adapters.md)
11+
12+
## How to
13+
14+
- [How to add root key](howto/add_root_key.md)
15+
16+
## Getting Help
17+
18+
If you find a bug, please report an [Issue](https://github.com/rails-api/active_model_serializers/issues/new).
19+
20+
If you have a question, please [post to Stack Overflow](http://stackoverflow.com/questions/tagged/active-model-serializers).
21+
22+
Thanks!
23+
24+
## Contributing
25+
26+
See [CONTRIBUTING.md](https://github.com/rails-api/active_model_serializers/blob/master/CONTRIBUTING.md)

docs/general/adapters.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Adapters
2+
3+
AMS does this 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 (```FlattenJSON``` 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.
7+
8+
## Built in Adapters
9+
10+
### FlattenJSON - Default
11+
12+
It's the default adapter, it generates a json response without a root key.
13+
Doesn't follow any specifc convention.
14+
15+
### JSON
16+
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.
18+
Doesn't follow any specifc convention.
19+
20+
### JSONAPI
21+
22+
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.
26+
27+
```ruby
28+
render @posts, include: ['authors', 'comments']
29+
# or
30+
render @posts, include: 'authors,comments'
31+
```
32+
33+
## Choosing an adapter
34+
35+
If you want to use a different adapter, such as JsonApi, you can change this in an initializer:
36+
37+
```ruby
38+
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
39+
```
40+
41+
or
42+
43+
```ruby
44+
ActiveModel::Serializer.config.adapter = :json_api
45+
```
46+
47+
If you want to have a root key in your responses you should use the Json adapter, instead of the default FlattenJson:
48+
49+
```ruby
50+
ActiveModel::Serializer.config.adapter = :json
51+
```

docs/general/getting_started.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Getting Started
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+
## Creating a Serializer
20+
21+
The easiest way to create a new serializer is to generate a new resource, which
22+
will generate a serializer at the same time:
23+
24+
```
25+
$ rails g resource post title:string body:string
26+
```
27+
28+
This will generate a serializer in `app/serializers/post_serializer.rb` for
29+
your new model. You can also generate a serializer for an existing model with
30+
the serializer generator:
31+
32+
```
33+
$ rails g serializer post
34+
```
35+
36+
The generated seralizer will contain basic `attributes` and
37+
`has_many`/`has_one`/`belongs_to` declarations, based on the model. For example:
38+
39+
```ruby
40+
class PostSerializer < ActiveModel::Serializer
41+
attributes :title, :body
42+
43+
has_many :comments
44+
has_one :author
45+
46+
end
47+
```
48+
49+
and
50+
51+
```ruby
52+
class CommentSerializer < ActiveModel::Serializer
53+
attributes :name, :body
54+
55+
belongs_to :post_id
56+
57+
end
58+
```
59+
60+
## Rails Integration
61+
62+
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:
63+
64+
```ruby
65+
class PostsController < ApplicationController
66+
67+
def show
68+
@post = Post.find(params[:id])
69+
render json: @post
70+
end
71+
72+
end
73+
```

docs/howto/add_root_key.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# How to add root key
2+
3+
Add the root key to your API is quite simple with AMS. The **Adapter** is what determines the format of your JSON response. The default adapter is the ```FlattenJSON``` which doesn't have the root key, so your response is something similar to:
4+
5+
```json
6+
{
7+
id: 1,
8+
title: "Awesome Post Tile",
9+
content: "Post content"
10+
}
11+
```
12+
13+
In order to add the root key you need to use the ```JSON``` Adapter, you can change this in an initializer:
14+
15+
```ruby
16+
ActiveModel::Serializer.config.adapter = :json
17+
```
18+
19+
You can also specify a class as adapter, as long as it complies with the AMS adapters interface.
20+
It will add the root key to all your serialized endpoints.
21+
22+
ex:
23+
24+
```json
25+
{
26+
post: {
27+
id: 1,
28+
title: "Awesome Post Tile",
29+
content: "Post content"
30+
}
31+
}
32+
```
33+
34+
or if it returns a collection:
35+
36+
```json
37+
{
38+
posts: [
39+
{
40+
id: 1,
41+
title: "Awesome Post Tile",
42+
content: "Post content"
43+
},
44+
{
45+
id: 2,
46+
title: "Another Post Tile",
47+
content: "Another post content"
48+
}
49+
]
50+
}
51+
```

0 commit comments

Comments
 (0)