active_model_serializers/docs/general/configuration_options.md
L. Preston Sego III 6de3f31b6e Namespace separator setting for json-api and tests (#1874)
Adds jsonapi_namespace_separator configuration

Also:

* Enable getting type from record class without serializer

Needs Followup:

- https://github.com/rails-api/active_model_serializers/pull/1874#discussion_r74607042 
- https://github.com/rails-api/active_model_serializers/pull/1874#discussion_r74607734
2016-08-12 12:54:42 -05:00

2.8 KiB

Back to Guides

Configuration Options

The following configuration options can be set on ActiveModelSerializers.config, preferably inside an initializer.

General

adapter

The adapter to use.

Possible values:

  • :attributes (default)
  • :json
  • :json_api
serializer_lookup_enabled

Enable automatic serializer lookup.

Possible values:

  • true (default)
  • false

When false, serializers must be explicitly specified.

key_transform

The key transform to use.

Option Result
:camel ExampleKey
:camel_lower exampleKey
:dash example-key
:unaltered the original, unaltered key
:underscore example_key
nil use the adapter default

Each adapter has a default key transform configured:

Adapter Default Key Transform
Json :unaltered
JsonApi :dash

config.key_transform is a global override of the adapter default. Adapters still prefer the render option :key_transform over this setting.

NOTE: Key transforms can be expensive operations. If key transforms are unnecessary for the application, setting config.key_transform to :unaltered will provide a performance boost.

default_includes

What relationships to serialize by default. Default: '*', which includes one level of related objects. See includes for more info.

JSON API

jsonapi_resource_type

Sets whether the type of the resource should be singularized or pluralized when it is not explicitly specified by the serializer

Possible values:

  • :singular
  • :plural (default)
jsonapi_namespace_separator

Sets separator string for namespaced models to render type attribute.

Separator Example: Admin::User
'-' (default) 'admin-users'
'--' (recommended) 'admin--users'

See Recommendation for dasherizing (kebab-case-ing) namespaced object, such as Admin::User for more discussion.

jsonapi_include_toplevel_object

Include a top level jsonapi member in the response document.

Possible values:

  • true
  • false (default)
jsonapi_version

The latest version of the spec to which the API conforms.

Default: '1.0'.

Used when jsonapi_include_toplevel_object is true

jsonapi_toplevel_meta

Optional top-level metadata. Not included if empty.

Default: {}.

Used when jsonapi_include_toplevel_object is true

Hooks

To run a hook when ActiveModelSerializers is loaded, use ActiveSupport.on_load(:action_controller) do end