|
1 |
| -# ActiveModelSerializers |
| 1 | +# ActiveModel::Serializers |
| 2 | + |
| 3 | +[](https://travis-ci.org/steveklabnik/active_model_serializers?branch=master) |
2 | 4 |
|
3 |
| -[](https://travis-ci.org/steveklabnik/active_model_serializers?branch=master) |
| 5 | +ActiveModel::Serializers brings convention over configuration to your JSON generation. |
4 | 6 |
|
5 |
| -TODO: Write a gem description |
| 7 | +AMS does this through two components: **serializers** and **adapters**. Serializers describe which attributes and relationships should be serialized. Adapters describe how attributes and relationships should be serialized. |
6 | 8 |
|
7 |
| -## Installation |
| 9 | +## Example |
8 | 10 |
|
9 |
| -Add this line to your application's Gemfile: |
| 11 | +Given two models, a `Post(title: string, body: text)` and a `Comment(name:string, body:text, post_id:integer)`, you will have two serializers: |
10 | 12 |
|
11 |
| - gem 'active_model_serializers' |
| 13 | +``` |
| 14 | +class PostSerializer < ActiveModel::Serializer |
| 15 | + attribute :title, :body |
| 16 | + |
| 17 | + has_many :comments |
12 | 18 |
|
13 |
| -And then execute: |
| 19 | + url :post |
| 20 | +end |
| 21 | +``` |
14 | 22 |
|
15 |
| - $ bundle |
| 23 | +and |
16 | 24 |
|
17 |
| -Or install it yourself as: |
| 25 | +``` |
| 26 | +class CommentSerializer < ActiveModel::Serializer |
| 27 | + attribute :name, :body |
| 28 | + |
| 29 | + belongs_to :post_id |
| 30 | + |
| 31 | + url [:post, :comment] |
| 32 | +end |
| 33 | +``` |
18 | 34 |
|
19 |
| - $ gem install active_model_serializers |
| 35 | +Generally speaking, you as a user of AMS will write (or generate) these serializer classes. By default, they will use the JsonApiAdapter, implemented by AMS. If you want to use a different adapter, such as a HalAdapter, you can change this in an initializer: |
20 | 36 |
|
21 |
| -## Usage |
| 37 | +``` |
| 38 | +ActiveModel::Serializer.default_adapter = ActiveModel::Serializer::Adapter::HalAdapter |
| 39 | +``` |
22 | 40 |
|
23 |
| -TODO: Write usage instructions here |
| 41 | +You won't need to implement an adapter unless you wish to use a new format or media type with AMS. |
24 | 42 |
|
25 |
| -## Contributing |
| 43 | +In your controllers, when you use `render :json`, Rails will now first search |
| 44 | +for a serializer for the object and use it if available. |
26 | 45 |
|
27 |
| -1. Fork it ( https://github.com/[my-github-username]/active_model_serializers/fork ) |
28 |
| -2. Create your feature branch (`git checkout -b my-new-feature`) |
29 |
| -3. Commit your changes (`git commit -am 'Add some feature'`) |
30 |
| -4. Push to the branch (`git push origin my-new-feature`) |
31 |
| -5. Create a new Pull Request |
| 46 | +```ruby |
| 47 | +class PostsController < ApplicationController |
| 48 | + def show |
| 49 | + @post = Post.find(params[:id]) |
| 50 | + |
| 51 | + render json: @post |
| 52 | + end |
| 53 | +end |
| 54 | +``` |
| 55 | + |
| 56 | +In this case, Rails will look for a serializer named `PostSerializer`, and if |
| 57 | +it exists, use it to serialize the `Post`. |
| 58 | + |
| 59 | +## Installation |
| 60 | + |
| 61 | +Add this line to your application's Gemfile: |
| 62 | + |
| 63 | + gem 'active_model_serializers' |
| 64 | + |
| 65 | +And then execute: |
| 66 | + |
| 67 | + $ bundle |
| 68 | + |
| 69 | +## Creating a Serializer |
| 70 | + |
| 71 | +The easiest way to create a new serializer is to generate a new resource, which |
| 72 | +will generate a serializer at the same time: |
| 73 | + |
| 74 | +``` |
| 75 | +$ rails g resource post title:string body:string |
| 76 | +``` |
| 77 | + |
| 78 | +This will generate a serializer in `app/serializers/post_serializer.rb` for |
| 79 | +your new model. You can also generate a serializer for an existing model with |
| 80 | +the serializer generator: |
| 81 | + |
| 82 | +``` |
| 83 | +$ rails g serializer post |
| 84 | +``` |
| 85 | + |
| 86 | +The generated seralizer will contain basic `attributes` and `has_many`/`belongs_to` declarations, based on |
| 87 | +the model. For example: |
| 88 | + |
| 89 | +``` |
| 90 | +class PostSerializer < ActiveModel::Serializer |
| 91 | + attribute :title, :body |
| 92 | + |
| 93 | + has_many :comments |
| 94 | +
|
| 95 | + url :post |
| 96 | +end |
| 97 | +``` |
| 98 | + |
| 99 | +and |
| 100 | + |
| 101 | +``` |
| 102 | +class CommentSerializer < ActiveModel::Serializer |
| 103 | + attribute :name, :body |
| 104 | + |
| 105 | + belongs_to :post_id |
| 106 | + |
| 107 | + url [:post, :comment] |
| 108 | +end |
| 109 | +``` |
| 110 | + |
| 111 | +The attribute names are a **whitelist** of attributes to be serialized. |
| 112 | + |
| 113 | +The `has_many` and `belongs_to` declarations describe relationships between resources. By default, when you serialize a `Post`, you will |
| 114 | +get its `Comment`s as well. |
| 115 | + |
| 116 | +The `url` declaration describes which named routes to use while generating URLs for your JSON. Not every adapter will require URLs. |
| 117 | + |
| 118 | +## Contributing |
| 119 | + |
| 120 | +1. Fork it ( https://github.com/rails-api/active_model_serializers/fork ) |
| 121 | +2. Create your feature branch (`git checkout -b my-new-feature`) |
| 122 | +3. Commit your changes (`git commit -am 'Add some feature'`) |
| 123 | +4. Push to the branch (`git push origin my-new-feature`) |
| 124 | +5. Create a new Pull Request |
0 commit comments