mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
[DOCS] Refactor, update, create documentation [ci skip]
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
[Back to Guides](README.md)
|
||||
|
||||
# ARCHITECTURE
|
||||
|
||||
An **`ActiveModel::Serializer`** wraps a [serializable resource](https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/serialization.rb)
|
||||
|
||||
@@ -1,40 +1,34 @@
|
||||
# Docs - ActiveModel::Serializer 0.10.x
|
||||
|
||||
This is the documentation of AMS, it's focused on the **0.10.x version.**
|
||||
This is the documentation of ActiveModelSerializers, it's focused on the **0.10.x version.**
|
||||
|
||||
-----
|
||||
|
||||
## General
|
||||
|
||||
- [Getting Started](general/getting_started.md)
|
||||
- [Adapters](general/adapters.md)
|
||||
- [Configuration Options](general/configuration_options.md)
|
||||
- [Serializers](general/serializers.md)
|
||||
- [Adapters](general/adapters.md)
|
||||
- [Rendering](general/rendering.md)
|
||||
- [Caching](general/caching.md)
|
||||
- [Logging](general/logging.md)
|
||||
- [Instrumentation](general/instrumentation.md)
|
||||
- [JSON API Schema](jsonapi/schema.md)
|
||||
- [ARCHITECTURE](ARCHITECTURE.md)
|
||||
|
||||
## How to
|
||||
|
||||
- [How to add root key](howto/add_root_key.md)
|
||||
- [How to add pagination links](howto/add_pagination_links.md)
|
||||
- [Using AMS Outside Of Controllers](howto/outside_controller_use.md)
|
||||
- [Using ActiveModelSerializers Outside Of Controllers](howto/outside_controller_use.md)
|
||||
|
||||
## Integrations
|
||||
| Integration | Supported AMS versions | Gem name and/or link
|
||||
|
||||
| Integration | Supported ActiveModelSerializers versions | Gem name and/or link
|
||||
|----|-----|----
|
||||
| Ember.js | 0.9.x | [active-model-adapter](https://github.com/ember-data/active-model-adapter)
|
||||
| Ember.js | 0.10.x + | [docs/integrations/ember-and-json-api.md](integrations/ember-and-json-api.md)
|
||||
| Grape | 0.10.x + | [#1258](https://github.com/rails-api/active_model_serializers/issues/1258) |
|
||||
| Grape | 0.9.x | https://github.com/jrhe/grape-active_model_serializers/ |
|
||||
| Sinatra | 0.9.x | https://github.com/SauloSilva/sinatra-active-model-serializers/
|
||||
|
||||
## Getting Help
|
||||
|
||||
If you find a bug, please report an [Issue](https://github.com/rails-api/active_model_serializers/issues/new).
|
||||
|
||||
If you have a question, please [post to Stack Overflow](http://stackoverflow.com/questions/tagged/active-model-serializers).
|
||||
|
||||
Thanks!
|
||||
|
||||
## Contributing
|
||||
|
||||
See [CONTRIBUTING.md](https://github.com/rails-api/active_model_serializers/blob/master/CONTRIBUTING.md)
|
||||
|
||||
@@ -1,9 +1,40 @@
|
||||
[Back to Guides](../README.md)
|
||||
|
||||
# Adapters
|
||||
|
||||
AMS works through two components: **serializers** and **adapters**.
|
||||
Serializers describe _which_ attributes and relationships should be serialized.
|
||||
Adapters describe _how_ attributes and relationships should be serialized.
|
||||
You can use one of the built-in adapters (```Attributes``` 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.
|
||||
ActiveModelSerializers offers the ability to configure which adapter
|
||||
to use both globally and/or when serializing (usually when rendering).
|
||||
|
||||
The global adapter configuration is set on [`ActiveModelSerializers.config`](configuration_options.md).
|
||||
It should be set only once, preferably at initialization.
|
||||
|
||||
For example:
|
||||
|
||||
```ruby
|
||||
ActiveModelSerializers.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```ruby
|
||||
ActiveModelSerializers.config.adapter = :json_api
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```ruby
|
||||
ActiveModelSerializers.config.adapter = :json
|
||||
```
|
||||
|
||||
The local adapter option is in the format `adapter: adapter`, where `adapter` is
|
||||
any of the same values as set globally.
|
||||
|
||||
The configured adapter can be set as a symbol, class, or class name, as described in
|
||||
[Advanced adapter configuration](adapters.md#advanced-adapter-configuration).
|
||||
|
||||
The `Attributes` adapter does not include a root key. It is just the serialized attributes.
|
||||
|
||||
Use either the `JSON` or `JSON API` adapters if you want the response document to have a root key.
|
||||
|
||||
## Built in Adapters
|
||||
|
||||
@@ -14,48 +45,69 @@ Doesn't follow any specific convention.
|
||||
|
||||
### JSON
|
||||
|
||||
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.
|
||||
The response document always with a root key.
|
||||
|
||||
The root key **can't be overridden**, and will be derived from the resource being serialized.
|
||||
|
||||
Doesn't follow any specific convention.
|
||||
|
||||
### JSON API
|
||||
|
||||
This adapter follows **version 1.0** of the format specified in
|
||||
[jsonapi.org/format](http://jsonapi.org/format). It will include the associated
|
||||
resources in the `"included"` member when the resource names are included in the
|
||||
`include` option.
|
||||
This adapter follows **version 1.0** of the [format specified](../jsonapi/schema.md) in
|
||||
[jsonapi.org/format](http://jsonapi.org/format).
|
||||
|
||||
#### Included
|
||||
|
||||
It will include the associated resources in the `"included"` member
|
||||
when the resource names are included in the `include` option.
|
||||
Including nested associated resources is also supported.
|
||||
|
||||
```ruby
|
||||
render @posts, include: ['authors', 'comments']
|
||||
render @posts, include: ['author', 'comments', 'comments.author']
|
||||
# or
|
||||
render @posts, include: 'author,comments,comments.author'
|
||||
```
|
||||
|
||||
or
|
||||
In addition, two types of wildcards may be used:
|
||||
|
||||
- `*` includes one level of associations.
|
||||
- `**` includes all recursively.
|
||||
|
||||
These can be combined with other paths.
|
||||
|
||||
```ruby
|
||||
render @posts, include: 'authors,comments'
|
||||
render @posts, include: '**' # or '*' for a single layer
|
||||
```
|
||||
|
||||
The format of the `include` option can be either a String composed of a comma-separated list of [relationship paths](http://jsonapi.org/format/#fetching-includes), an Array of Symbols and Hashes, or a mix of both.
|
||||
The format of the `include` option can be either:
|
||||
|
||||
## Choosing an adapter
|
||||
- a String composed of a comma-separated list of [relationship paths](http://jsonapi.org/format/#fetching-includes).
|
||||
- an Array of Symbols and Hashes.
|
||||
- a mix of both.
|
||||
|
||||
If you want to use a specify a default adapter, such as JsonApi, you can change this in an initializer:
|
||||
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.
|
||||
|
||||
```ruby
|
||||
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
|
||||
render @posts, include: 'author.comments.**'
|
||||
```
|
||||
|
||||
or
|
||||
##### Security Considerations
|
||||
|
||||
Since the included options may come from the query params (i.e. user-controller):
|
||||
|
||||
```ruby
|
||||
ActiveModel::Serializer.config.adapter = :json_api
|
||||
render @posts, include: params[:include]
|
||||
```
|
||||
|
||||
If you want to have a root key for each resource in your responses, you should use the Json or
|
||||
JsonApi adapters instead of the default Attributes:
|
||||
The user could pass in `include=**`.
|
||||
|
||||
```ruby
|
||||
ActiveModel::Serializer.config.adapter = :json
|
||||
```
|
||||
We recommend filtering any user-supplied includes appropriately.
|
||||
|
||||
## Advanced adapter configuration
|
||||
|
||||
|
||||
52
docs/general/caching.md
Normal file
52
docs/general/caching.md
Normal file
@@ -0,0 +1,52 @@
|
||||
[Back to Guides](../README.md)
|
||||
|
||||
# Caching
|
||||
|
||||
To cache a serializer, call ```cache``` and pass its options.
|
||||
The options are the same options of ```ActiveSupport::Cache::Store```, plus
|
||||
a ```key``` option that will be the prefix of the object cache
|
||||
on a pattern ```"#{key}/#{object.id}-#{object.updated_at}"```.
|
||||
|
||||
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.
|
||||
|
||||
**[NOTE] Every object is individually cached.**
|
||||
|
||||
**[NOTE] The cache is automatically expired after an object is updated, but it's not deleted.**
|
||||
|
||||
```ruby
|
||||
cache(options = nil) # options: ```{key, expires_in, compress, force, race_condition_ttl}```
|
||||
```
|
||||
|
||||
Take the example bellow:
|
||||
|
||||
```ruby
|
||||
class PostSerializer < ActiveModel::Serializer
|
||||
cache key: 'post', expires_in: 3.hours
|
||||
attributes :title, :body
|
||||
|
||||
has_many :comments
|
||||
end
|
||||
```
|
||||
|
||||
On this example every ```Post``` object will be cached with
|
||||
the key ```"post/#{post.id}-#{post.updated_at}"```. You can use this key to expire it as you want,
|
||||
but in this case it will be automatically expired after 3 hours.
|
||||
|
||||
## Fragment Caching
|
||||
|
||||
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.
|
||||
|
||||
You can define the attribute by using ```only``` or ```except``` option on cache method.
|
||||
|
||||
**[NOTE] Cache serializers will be used at their relationships**
|
||||
|
||||
Example:
|
||||
|
||||
```ruby
|
||||
class PostSerializer < ActiveModel::Serializer
|
||||
cache key: 'post', expires_in: 3.hours, only: [:title]
|
||||
attributes :title, :body
|
||||
|
||||
has_many :comments
|
||||
end
|
||||
```
|
||||
@@ -1,6 +1,9 @@
|
||||
[Back to Guides](../README.md)
|
||||
|
||||
# Configuration Options
|
||||
|
||||
The following configuration options can be set on `ActiveModel::Serializer.config` inside an initializer.
|
||||
The following configuration options can be set on `ActiveModelSerializers.config`,
|
||||
preferably inside an initializer.
|
||||
|
||||
## General
|
||||
|
||||
@@ -17,3 +20,7 @@ The following configuration options can be set on `ActiveModel::Serializer.confi
|
||||
Default: `'1.0'`.
|
||||
- `jsonapi_toplevel_meta`: Optional metadata. Not included if empty.
|
||||
Default: `{}`.
|
||||
|
||||
## Hooks
|
||||
|
||||
To run a hook when ActiveModelSerializers is loaded, use `ActiveSupport.on_load(:active_model_serializers) do end`
|
||||
|
||||
@@ -1,21 +1,7 @@
|
||||
[Back to Guides](../README.md)
|
||||
|
||||
# Getting Started
|
||||
|
||||
## Installation
|
||||
|
||||
### ActiveModel::Serializer is already included on Rails >= 5
|
||||
|
||||
Add this line to your application's Gemfile:
|
||||
|
||||
```
|
||||
gem 'active_model_serializers'
|
||||
```
|
||||
|
||||
And then execute:
|
||||
|
||||
```
|
||||
$ bundle
|
||||
```
|
||||
|
||||
## Creating a Serializer
|
||||
|
||||
The easiest way to create a new serializer is to generate a new resource, which
|
||||
@@ -33,7 +19,7 @@ the serializer generator:
|
||||
$ rails g serializer post
|
||||
```
|
||||
|
||||
The generated seralizer will contain basic `attributes` and
|
||||
The generated serializer will contain basic `attributes` and
|
||||
`has_many`/`has_one`/`belongs_to` declarations, based on the model. For example:
|
||||
|
||||
```ruby
|
||||
@@ -42,7 +28,6 @@ class PostSerializer < ActiveModel::Serializer
|
||||
|
||||
has_many :comments
|
||||
has_one :author
|
||||
|
||||
end
|
||||
```
|
||||
|
||||
@@ -53,13 +38,20 @@ class CommentSerializer < ActiveModel::Serializer
|
||||
attributes :name, :body
|
||||
|
||||
belongs_to :post_id
|
||||
|
||||
end
|
||||
```
|
||||
|
||||
The attribute names are a **whitelist** of attributes to be serialized.
|
||||
|
||||
The `has_many`, `has_one`, and `belongs_to` declarations describe relationships between
|
||||
resources. By default, when you serialize a `Post`, you will get its `Comments`
|
||||
as well.
|
||||
|
||||
For more information, see [Serializers](docs/general/serializers.md).
|
||||
|
||||
### Namespaced Models
|
||||
|
||||
When serializing a model inside a namespace, such as `Api::V1::Post`, AMS will expect the corresponding serializer to be inside the same namespace (namely `Api::V1::PostSerializer`).
|
||||
When serializing a model inside a namespace, such as `Api::V1::Post`, ActiveModelSerializers will expect the corresponding serializer to be inside the same namespace (namely `Api::V1::PostSerializer`).
|
||||
|
||||
### Model Associations and Nested Serializers
|
||||
|
||||
@@ -69,7 +61,7 @@ class PostSerializer < ActiveModel::Serializer
|
||||
has_many :comments
|
||||
end
|
||||
```
|
||||
AMS will look for `PostSerializer::CommentSerializer` in priority, and fall back to `::CommentSerializer` in case the former does not exist. This allows for more control over the way a model gets serialized as an association of an other model.
|
||||
ActiveModelSerializers will look for `PostSerializer::CommentSerializer` in priority, and fall back to `::CommentSerializer` in case the former does not exist. This allows for more control over the way a model gets serialized as an association of an other model.
|
||||
|
||||
For example, in the following situation:
|
||||
|
||||
@@ -86,11 +78,11 @@ class PostSerializer < ActiveModel::Serializer
|
||||
end
|
||||
```
|
||||
|
||||
AMS will use `PostSerializer::CommentSerializer` (thus including only the `:body_short` attribute) when serializing a `Comment` as part of a `Post`, but use `::CommentSerializer` when serializing a `Comment` directly (thus including `:body, :date, :nb_likes`).
|
||||
ActiveModelSerializers will use `PostSerializer::CommentSerializer` (thus including only the `:body_short` attribute) when serializing a `Comment` as part of a `Post`, but use `::CommentSerializer` when serializing a `Comment` directly (thus including `:body, :date, :nb_likes`).
|
||||
|
||||
## Rails Integration
|
||||
|
||||
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:
|
||||
ActiveModelSerializers 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:
|
||||
|
||||
```ruby
|
||||
class PostsController < ApplicationController
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
[Back to Guides](../README.md)
|
||||
|
||||
# Instrumentation
|
||||
|
||||
ActiveModelSerializers uses the
|
||||
@@ -30,6 +32,7 @@ ActiveSupport::Notifications.subscribe 'render.active_model_serializers' do |*ar
|
||||
# event.payload
|
||||
# whatever
|
||||
end
|
||||
```
|
||||
|
||||
## [LogSubscriber](http://api.rubyonrails.org/classes/ActiveSupport/LogSubscriber.html)
|
||||
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
[Back to Guides](../README.md)
|
||||
|
||||
# Logging
|
||||
|
||||
If we are using ActiveModel::Serializers on Rails app by default the `Rails.logger` will be used.
|
||||
If we are using ActiveModelSerializers on a Rails app by default the `Rails.logger` will be used.
|
||||
|
||||
On a non Rails enviroment by default the `ActiveSupport::TaggedLogging` will be
|
||||
used.
|
||||
|
||||
If we need to customize the logger we can define this in an initializer:
|
||||
You may customize the logger we by in an initializer, for example:
|
||||
|
||||
```ruby
|
||||
ActiveModelSerializers.logger = Logger.new(STDOUT)
|
||||
|
||||
136
docs/general/rendering.md
Normal file
136
docs/general/rendering.md
Normal file
@@ -0,0 +1,136 @@
|
||||
[Back to Guides](../README.md)
|
||||
|
||||
# Rendering
|
||||
|
||||
### Implicit Serializer
|
||||
|
||||
In your controllers, when you use `render :json`, Rails will now first search
|
||||
for a serializer for the object and use it if available.
|
||||
|
||||
```ruby
|
||||
class PostsController < ApplicationController
|
||||
def show
|
||||
@post = Post.find(params[:id])
|
||||
|
||||
render json: @post
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
In this case, Rails will look for a serializer named `PostSerializer`, and if
|
||||
it exists, use it to serialize the `Post`.
|
||||
|
||||
### Explicit Serializer
|
||||
|
||||
If you wish to use a serializer other than the default, you can explicitly pass it to the renderer.
|
||||
|
||||
#### 1. For a resource:
|
||||
|
||||
```ruby
|
||||
render json: @post, serializer: PostPreviewSerializer
|
||||
```
|
||||
|
||||
#### 2. For a resource collection:
|
||||
|
||||
Specify the serializer for each resource with `each_serializer`
|
||||
|
||||
```ruby
|
||||
render json: @posts, each_serializer: PostPreviewSerializer
|
||||
```
|
||||
|
||||
The default serializer for collections is `CollectionSerializer`.
|
||||
|
||||
Specify the collection serializer with the `serializer` option.
|
||||
|
||||
```ruby
|
||||
render json: @posts, serializer: CollectionSerializer, each_serializer: PostPreviewSerializer
|
||||
```
|
||||
|
||||
## Serializing non-ActiveRecord objects
|
||||
|
||||
All serializable resources must pass the
|
||||
[ActiveModel::Serializer::Lint::Tests](../../lib/active_model/serializer/lint.rb#L17).
|
||||
|
||||
See the ActiveModelSerializers::Model for a base class that implements the full
|
||||
API for a plain-old Ruby object (PORO).
|
||||
|
||||
## SerializableResource options
|
||||
|
||||
The `options` hash passed to `render` or `ActiveModel::SerializableResource.new(resource, options)`
|
||||
are partitioned into `serializer_opts` and `adapter_opts`. `adapter_opts` are passed to new Adapters;
|
||||
`serializer_opts` are passed to new Serializers.
|
||||
|
||||
The `adapter_opts` are specified in [ActiveModel::SerializableResource::ADAPTER_OPTIONS](../../lib/active_model/serializable_resource.rb#L4).
|
||||
The `serializer_opts` are the remaining options.
|
||||
|
||||
(In Rails, the `options` are also passed to the `as_json(options)` or `to_json(options)`
|
||||
methods on the resource serialization by the Rails JSON renderer. They are, therefore, important
|
||||
to know about, but not part of ActiveModelSerializers.)
|
||||
|
||||
See [ARCHITECTURE](../ARCHITECTURE.md) for more information.
|
||||
|
||||
### adapter_opts
|
||||
|
||||
#### fields
|
||||
|
||||
PR please :)
|
||||
|
||||
#### adapter
|
||||
|
||||
PR please :)
|
||||
|
||||
#### meta
|
||||
|
||||
If you want a `meta` attribute in your response, specify it in the `render`
|
||||
call:
|
||||
|
||||
```ruby
|
||||
render json: @post, meta: { total: 10 }
|
||||
```
|
||||
|
||||
The key can be customized using `meta_key` option.
|
||||
|
||||
```ruby
|
||||
render json: @post, meta: { total: 10 }, meta_key: "custom_meta"
|
||||
```
|
||||
|
||||
`meta` will only be included in your response if you are using an Adapter that supports `root`,
|
||||
as JsonAPI and Json adapters, the default adapter (Attributes) doesn't have `root`.
|
||||
|
||||
#### meta_key
|
||||
|
||||
PR please :)
|
||||
|
||||
#### links
|
||||
|
||||
PR please :)
|
||||
|
||||
### serializer_opts
|
||||
|
||||
#### include
|
||||
|
||||
PR please :)
|
||||
|
||||
#### root
|
||||
|
||||
PR please :)
|
||||
|
||||
#### serializer
|
||||
|
||||
PR please :)
|
||||
|
||||
#### scope
|
||||
|
||||
PR please :)
|
||||
|
||||
#### scope_name
|
||||
|
||||
PR please :)
|
||||
|
||||
## Using a serializer without `render`
|
||||
|
||||
See [Usage outside of a controller](../howto/outside_controller_use.md#serializing-before-controller-render).
|
||||
|
||||
## Pagination
|
||||
|
||||
See [How to add pagination links](https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/add_pagination_links.md).
|
||||
213
docs/general/serializers.md
Normal file
213
docs/general/serializers.md
Normal file
@@ -0,0 +1,213 @@
|
||||
[Back to Guides](../README.md)
|
||||
|
||||
# Serializers
|
||||
|
||||
Given a serializer class:
|
||||
|
||||
```ruby
|
||||
class SomeSerializer < ActiveModel::Serializer
|
||||
end
|
||||
```
|
||||
|
||||
The following methods may be defined in it:
|
||||
|
||||
### Attributes
|
||||
|
||||
#### ::attributes
|
||||
|
||||
Serialization of the resource `title` and `body`
|
||||
|
||||
| In Serializer | #attributes |
|
||||
|---------------------------- |-------------|
|
||||
| `attributes :title, :body` | `{ title: 'Some Title', body: 'Some Body' }`
|
||||
| `attributes :title, :body`<br>`def body "Special #{object.body}" end` | `{ title: 'Some Title', body: 'Special Some Body' }`
|
||||
|
||||
|
||||
#### ::attribute
|
||||
|
||||
Serialization of the resource `title`
|
||||
|
||||
| In Serializer | #attributes |
|
||||
|---------------------------- |-------------|
|
||||
| `attribute :title` | `{ title: 'Some Title' } `
|
||||
| `attribute :title, key: :name` | `{ name: 'Some Title' } `
|
||||
| `attribute :title { 'A Different Title'}` | `{ title: 'A Different Title' } `
|
||||
| `attribute :title`<br>`def title 'A Different Title' end` | `{ title: 'A Different Title' }`
|
||||
|
||||
### Associations
|
||||
|
||||
#### ::has_one
|
||||
|
||||
e.g.
|
||||
|
||||
```ruby
|
||||
has_one :bio
|
||||
has_one :blog, key: :site
|
||||
has_one :maker, virtual_value: { id: 1 }
|
||||
```
|
||||
|
||||
#### ::has_many
|
||||
|
||||
e.g.
|
||||
|
||||
```ruby
|
||||
has_many :comments
|
||||
has_many :comments, key: :reviews
|
||||
has_many :comments, serializer: CommentPreviewSerializer
|
||||
has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]
|
||||
has_many :comments, key: :last_comments do
|
||||
last(1)
|
||||
end
|
||||
```
|
||||
|
||||
#### ::belongs_to
|
||||
|
||||
e.g.
|
||||
|
||||
```ruby
|
||||
belongs_to :author, serializer: AuthorPreviewSerializer
|
||||
belongs_to :author, key: :writer
|
||||
belongs_to :post
|
||||
belongs_to :blog
|
||||
def blog
|
||||
Blog.new(id: 999, name: 'Custom blog')
|
||||
end
|
||||
```
|
||||
|
||||
### Caching
|
||||
|
||||
#### ::cache
|
||||
|
||||
e.g.
|
||||
|
||||
```ruby
|
||||
cache key: 'post', expires_in: 0.1, skip_digest: true
|
||||
cache expires_in: 1.day, skip_digest: true
|
||||
cache key: 'writer', skip_digest: true
|
||||
cache only: [:name], skip_digest: true
|
||||
cache except: [:content], skip_digest: true
|
||||
cache key: 'blog'
|
||||
cache only: [:id]
|
||||
```
|
||||
|
||||
#### #cache_key
|
||||
|
||||
e.g.
|
||||
|
||||
```ruby
|
||||
# Uses a custom non-time-based cache key
|
||||
def cache_key
|
||||
"#{self.class.name.downcase}/#{self.id}"
|
||||
end
|
||||
```
|
||||
|
||||
### Other
|
||||
|
||||
#### ::type
|
||||
|
||||
e.g.
|
||||
|
||||
```ruby
|
||||
class UserProfileSerializer < ActiveModel::Serializer
|
||||
type 'profile'
|
||||
end
|
||||
```
|
||||
|
||||
#### ::link
|
||||
|
||||
e.g.
|
||||
|
||||
```ruby
|
||||
link :other, 'https://example.com/resource'
|
||||
link :self do
|
||||
href "https://example.com/link_author/#{object.id}"
|
||||
end
|
||||
```
|
||||
|
||||
#### #object
|
||||
|
||||
The object being serialized.
|
||||
|
||||
#### #root
|
||||
|
||||
PR please :)
|
||||
|
||||
#### #scope
|
||||
|
||||
PR please :)
|
||||
|
||||
#### #read_attribute_for_serialization(key)
|
||||
|
||||
The serialized value for a given key. e.g. `read_attribute_for_serialization(:title) #=> 'Hello World'`
|
||||
|
||||
#### #links
|
||||
|
||||
PR please :)
|
||||
|
||||
#### #json_key
|
||||
|
||||
PR please :)
|
||||
|
||||
## Examples
|
||||
|
||||
Given two models, a `Post(title: string, body: text)` and a
|
||||
`Comment(name: string, body: text, post_id: integer)`, you will have two
|
||||
serializers:
|
||||
|
||||
```ruby
|
||||
class PostSerializer < ActiveModel::Serializer
|
||||
cache key: 'posts', expires_in: 3.hours
|
||||
attributes :title, :body
|
||||
|
||||
has_many :comments
|
||||
end
|
||||
```
|
||||
|
||||
and
|
||||
|
||||
```ruby
|
||||
class CommentSerializer < ActiveModel::Serializer
|
||||
attributes :name, :body
|
||||
|
||||
belongs_to :post
|
||||
end
|
||||
```
|
||||
|
||||
Generally speaking, you, as a user of ActiveModelSerializers, will write (or generate) these
|
||||
serializer classes.
|
||||
|
||||
## More Info
|
||||
|
||||
For more information, see [the Serializer class on GitHub](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer.rb)
|
||||
|
||||
## Overriding association methods
|
||||
|
||||
If you want to override any association, you can use:
|
||||
|
||||
```ruby
|
||||
class PostSerializer < ActiveModel::Serializer
|
||||
attributes :id, :body
|
||||
|
||||
has_many :comments
|
||||
|
||||
def comments
|
||||
object.comments.active
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
## Overriding attribute methods
|
||||
|
||||
If you want to override any attribute, you can use:
|
||||
|
||||
```ruby
|
||||
class PostSerializer < ActiveModel::Serializer
|
||||
attributes :id, :body
|
||||
|
||||
has_many :comments
|
||||
|
||||
def body
|
||||
object.body.downcase
|
||||
end
|
||||
end
|
||||
```
|
||||
@@ -1,3 +1,5 @@
|
||||
[Back to Guides](../README.md)
|
||||
|
||||
# How to add pagination links
|
||||
|
||||
### JSON API adapter
|
||||
@@ -8,6 +10,10 @@ the resource is paginated and if you are using the ```JsonApi``` adapter.
|
||||
If you want pagination links in your response, use [Kaminari](https://github.com/amatsuda/kaminari)
|
||||
or [WillPaginate](https://github.com/mislav/will_paginate).
|
||||
|
||||
Although the others adapters does not have this feature, it is possible to
|
||||
implement pagination links to `JSON` adapter. For more information about it,
|
||||
please see in our docs
|
||||
|
||||
###### Kaminari examples
|
||||
|
||||
```ruby
|
||||
@@ -33,7 +39,7 @@ render json: @posts
|
||||
```
|
||||
|
||||
```ruby
|
||||
ActiveModel::Serializer.config.adapter = :json_api
|
||||
ActiveModelSerializers.config.adapter = :json_api
|
||||
```
|
||||
|
||||
ex:
|
||||
@@ -61,7 +67,7 @@ ex:
|
||||
}
|
||||
```
|
||||
|
||||
AMS pagination relies on a paginated collection with the methods `current_page`, `total_pages`, and `size`, such as are supported by both [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate).
|
||||
ActiveModelSerializers pagination relies on a paginated collection with the methods `current_page`, `total_pages`, and `size`, such as are supported by both [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate).
|
||||
|
||||
|
||||
### JSON adapter
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# How to add root key
|
||||
|
||||
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 ```Attributes``` which doesn't have the root key, so your response is something similar to:
|
||||
Add the root key to your API is quite simple with ActiveModelSerializers. The **Adapter** is what determines the format of your JSON response. The default adapter is the ```Attributes``` which doesn't have the root key, so your response is something similar to:
|
||||
|
||||
```json
|
||||
{
|
||||
@@ -13,10 +13,10 @@ Add the root key to your API is quite simple with AMS. The **Adapter** is what d
|
||||
In order to add the root key you need to use the ```JSON``` Adapter, you can change this in an initializer:
|
||||
|
||||
```ruby
|
||||
ActiveModel::Serializer.config.adapter = :json
|
||||
ActiveModelSerializers.config.adapter = :json
|
||||
```
|
||||
|
||||
You can also specify a class as adapter, as long as it complies with the AMS adapters interface.
|
||||
You can also specify a class as adapter, as long as it complies with the ActiveModelSerializers adapters interface.
|
||||
It will add the root key to all your serialized endpoints.
|
||||
|
||||
ex:
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
## Using AMS Outside Of A Controller
|
||||
[Back to Guides](../README.md)
|
||||
|
||||
### Serializing a resource
|
||||
## Using ActiveModelSerializers Outside Of A Controller
|
||||
|
||||
In AMS versions 0.10 or later, serializing resources outside of the controller context is fairly simple:
|
||||
### Serializing a resource
|
||||
|
||||
In ActiveModelSerializers versions 0.10 or later, serializing resources outside of the controller context is fairly simple:
|
||||
|
||||
```ruby
|
||||
# Create our resource
|
||||
@@ -16,14 +18,14 @@ serializable_resource = ActiveModel::SerializableResource.new(post, options)
|
||||
|
||||
# Convert your resource into json
|
||||
model_json = serializable_resource.as_json
|
||||
```
|
||||
```
|
||||
|
||||
### Retrieving a Resource's Active Model Serializer
|
||||
### Looking up the Serializer for a Resource
|
||||
|
||||
If you want to retrieve a serializer for a specific resource, you can do the following:
|
||||
|
||||
```ruby
|
||||
# Create our resource
|
||||
# Create our resource
|
||||
post = Post.create(title: "Another Example", body: "So much fun.")
|
||||
|
||||
# Optional options parameters
|
||||
@@ -33,10 +35,24 @@ options = {}
|
||||
serializer = ActiveModel::Serializer.serializer_for(post, options)
|
||||
```
|
||||
|
||||
You could also retrieve the serializer via:
|
||||
You could also retrieve the serializer via:
|
||||
|
||||
```ruby
|
||||
ActiveModel::SerializableResource.new(post, options).serializer
|
||||
ActiveModel::SerializableResource.new(post, options).serializer
|
||||
```
|
||||
|
||||
Both approaches will return an instance, if any, of the resource's serializer.
|
||||
Both approaches will return an instance, if any, of the resource's serializer.
|
||||
|
||||
## Serializing before controller render
|
||||
|
||||
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
|
||||
the resource you want to be serialized and call `.as_json`.
|
||||
|
||||
```ruby
|
||||
def create
|
||||
message = current_user.messages.create!(message_params)
|
||||
message_json = ActiveModel::SerializableResource.new(message).as_json
|
||||
MessageCreationWorker.perform(message_json)
|
||||
head 204
|
||||
end
|
||||
```
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
[Back to Guides](../README.md)
|
||||
|
||||
# Integrating with Ember and JSON API
|
||||
|
||||
- [Preparation](./ember-and-json-api.md#preparation)
|
||||
@@ -10,7 +12,7 @@
|
||||
|
||||
Note: This guide assumes that `ember-cli` is used for your ember app.
|
||||
|
||||
The JSON API specification calls for hyphens for multi-word separators. AMS uses underscores.
|
||||
The JSON API specification calls for hyphens for multi-word separators. ActiveModelSerializers uses underscores.
|
||||
To solve this, in Ember, both the adapter and the serializer will need some modifications:
|
||||
|
||||
### Server-Side Changes
|
||||
@@ -86,7 +88,7 @@ export default DS.JSONAPISerializer.extend({
|
||||
## Including Nested Resources
|
||||
|
||||
Previously, `store.find` and `store.findRecord` did not allow specification of any query params.
|
||||
The AMS default for the `include` parameter is to be `nil` meaning that if any associations are defined in your serializer, only the `id` and `type` will be in the `relationships` structure of the JSON API response.
|
||||
The ActiveModelSerializers default for the `include` parameter is to be `nil` meaning that if any associations are defined in your serializer, only the `id` and `type` will be in the `relationships` structure of the JSON API response.
|
||||
For more on `include` usage, see: [The JSON API include examples](./../general/adapters.md#JSON-API)
|
||||
|
||||
With the above modifications, you can execute code as below in order to include nested resources while doing a find query.
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
[Back to Guides](../README.md)
|
||||
|
||||
[](http://jsonapi.org/)
|
||||
|
||||
## JSON API Requests
|
||||
|
||||
Reference in New Issue
Block a user