Skip to content

Commit 420f795

Browse files
joaomdmouraJoão M. D. Moura
authored andcommitted
creating initial general and how to docs
1 parent b0a2e9f commit 420f795

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

docs/general/adapters.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 your one but you won't need to implement an adapter unless you wish to use a new format or
7+
media type with AMS.
8+
9+
## Built in Adapters
10+
11+
### FlattenJSON - Default
12+
13+
It's the default adapter, it generates a json response without a root key.
14+
Doesn't follow any specifc convention.
15+
16+
### JSON
17+
18+
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.
19+
Doesn't follow any specifc convention.
20+
21+
### JSONAPI
22+
23+
This adapter follows 1.0 of the format specified in
24+
[jsonapi.org/format](http://jsonapi.org/format). It will include the associated
25+
resources in the `"included"` member when the resource names are included in the
26+
`include` option.
27+
28+
```ruby
29+
render @posts, include: ['authors', 'comments']
30+
# or
31+
render @posts, include: 'authors,comments'
32+
```
33+
34+
## Choose an Adapter
35+
36+
If you want to use a different adapter, such as a JsonApi, you can change this in an initializer:
37+
38+
```ruby
39+
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
40+
```
41+
42+
or
43+
44+
```ruby
45+
ActiveModel::Serializer.config.adapter = :json_api
46+
```
47+
48+
If you want to have a root key on your responses you should use the Json adapter, instead of the default FlattenJson:
49+
50+
```ruby
51+
ActiveModel::Serializer.config.adapter = :json
52+
```

docs/general/getting_started.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
url :post
47+
end
48+
```
49+
50+
and
51+
52+
```ruby
53+
class CommentSerializer < ActiveModel::Serializer
54+
attributes :name, :body
55+
56+
belongs_to :post_id
57+
58+
url [:post, :comment]
59+
end
60+
```
61+
62+
## Rails Integration
63+
64+
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:
65+
66+
```ruby
67+
class PostsController < ApplicationController
68+
69+
def show
70+
@post = Post.find(params[:id])
71+
render json: @post
72+
end
73+
74+
end
75+
```

docs/howto/add_root_key.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# How to use 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 correspondent 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_api
17+
```
18+
19+
or
20+
21+
```ruby
22+
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::Json
23+
```
24+
25+
This will add the root key to all your serialized endpoints.
26+
27+
ex:
28+
29+
```json
30+
{
31+
post: {
32+
id: 1,
33+
title: "Awesome Post Tile",
34+
content: "Post content"
35+
}
36+
}
37+
```
38+
39+
or if it returns a collection:
40+
41+
```json
42+
{
43+
posts: [
44+
{
45+
id: 1,
46+
title: "Awesome Post Tile",
47+
content: "Post content"
48+
},
49+
{
50+
id: 2,
51+
title: "Another Post Tile",
52+
content: "Another post content"
53+
}
54+
]
55+
}
56+
```

0 commit comments

Comments
 (0)