Merge pull request #184 from eniolopes/master

Documentation for meta and meta_key options
This commit is contained in:
Rafael Mendonça França 2013-01-11 06:06:13 -08:00
commit 95ff5d5960

View File

@ -253,6 +253,43 @@ class PostSerializer < ActiveModel::Serializer
end
```
If you would like to add meta information to the outputted JSON, use the `:meta`
option:
```ruby
render :json => @posts, :serializer => CustomArraySerializer, :meta => {:total => 10}
```
The above usage of `:meta` will produce the following:
```json
{
"meta": { "total": 10 },
"posts": [
{ "title": "Post 1", "body": "Hello!" },
{ "title": "Post 2", "body": "Goodbye!" }
]
}
```
If you would like to change the meta key name you can use the `:meta_key` option:
```ruby
render :json => @posts, :serializer => CustomArraySerializer, :meta => {:total => 10}, :meta_key => 'meta_object'
```
The above usage of `:meta_key` will produce the following:
```json
{
"meta_object": { "total": 10 },
"posts": [
{ "title": "Post 1", "body": "Hello!" },
{ "title": "Post 2", "body": "Goodbye!" }
]
}
```
If you would like direct, low-level control of attribute serialization, you can
completely override the `attributes` method to return the hash you need: