Adapter Options adapter_opts
Adapter Options are passed to new Adapters and are specified in ActiveModelSerializers::SerializableResource::ADAPTER_OPTIONS
adapter
PR please :)
fields
- To restrict the fields returned, you should use
fields
option.
For example, if you have a serializer like this
class UserSerializer < ActiveModel::Serializer
attributes :access_token, :first_name, :last_name
end
and in a specific controller, you want to return access_token
only, fields
will help you:
class AnonymousController < ApplicationController
def create
render json: User.create(activation_state: 'anonymous'), fields: [:access_token], status: 201
end
end
Note that this is only valid for the json
and attributes
adapter. For the json_api
adapter, you would use
render json: @user, fields: { users: [:access_token] }
Where users
is the JSONAPI type.
key_transform
render json: posts, each_serializer: PostSerializer, key_transform: :camel_lower
See Global Configurations options for more about setting key transform globally
meta
A meta
member can be used to include non-standard meta-information. meta
can
be utilized in several levels in a response.
Top-level
To set top-level meta
in a response, specify it in the render
call.
render json: @post, meta: { total: 10 }
The key can be customized using meta_key
option.
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
, e.g., JsonApi
and Json
adapters. The default adapter,
Attributes
does not have root
.
Resource-level
To set resource-level meta
in a response, define meta in a serializer with one
of the following methods:
As a single, static string.
meta stuff: 'value'
As a block containing a Hash.
meta do
{
rating: 4,
comments_count: object.comments.count
}
end
links
(JSON API Adapter)
If you wish to use Rails url helpers for link generation, e.g., link(:resources) { resources_url }
, ensure your application sets
Rails.application.routes.default_url_options
.
Top-level
JsonApi supports a links object to be specified at top-level, that you can specify in the render
:
links_object = {
href: "http://example.com/api/posts",
meta: {
count: 10
}
}
render json: @posts, links: links_object
That's the result:
{
"data": [
{
"type": "posts",
"id": "1",
"attributes": {
"title": "JSON API is awesome!",
"body": "You should be using JSON API",
"created": "2015-05-22T14:56:29.000Z",
"updated": "2015-05-22T14:56:28.000Z"
}
}
],
"links": {
"href": "http://example.com/api/posts",
"meta": {
"count": 10
}
}
}
This feature is specific to JsonApi, so you have to use the use the JsonApi Adapter
Resource-level
In your serializer, define each link in one of the following methods:
As a static string
link :link_name, 'https://example.com/resource'
As a block to be evaluated. When using Rails, URL helpers are available.
Ensure your application sets Rails.application.routes.default_url_options
.
link :link_name_ do
"https://example.com/resource/#{object.id}"
end
link(:link_name) { "https://example.com/resource/#{object.id}" }
link(:link_name) { resource_url(object) }
link(:link_name) { url_for(controller: 'controller_name', action: 'index', only_path: false) }