Guides > Global Configurations

Application-Wide Configurations

All the following configuration options can be set on ActiveModelSerializers.config, preferably in an initializer. For example, to setup the default adapter:

# config/initializers/ams.rb
ActiveModelSerializers.config.adapter = ActiveModelSerializers::Adapter::JsonApi
# or
ActiveModelSerializers.config.adapter = :json_api
# or
ActiveModelSerializers.config.adapter = :json

General Configurations

Configuration Definition Possible Values
adapter Which adapter to use
  • :attributes (default)
  • :json
  • :json_api
serializer_lookup_enabled Enable automatic serializer lookup
  • true (default)
  • false
key_transform Modifys keys in serialized responses
  • :camel "ExampleKey"
  • :camel_lower "exampleKey"
  • :dashed "example-key" (JsonApi adapter default)
  • :unaltered (Json adapter default)
  • :underscore "example_key"
  • nil Use the adapter default

You can pass a :key_transform option to a specific render action. That will have priority over the global key_transform configuration setting for the action.

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.

Adapter-Specific Configurations

JSON API Adapter

Configuration Definition Possible Values
jsonapi_resource_type Whether type of the resource should be singularized or pluralized when not specified by the serializer
  • :singular
  • :plural (default)
jsonapi_include_toplevel_object Include a top level jsonapi member in the response document
  • true
  • false (default)
jsonapi_version Version of the spec to which the API conforms '1.0' (default)
jsonapi_toplevel_meta Top-level metadata. Not included if empty {} (default)

jsonapi_version & jsonapi_toplevel_meta are only used when jsonapi_include_toplevel_object is true

Hooks

To run a hook when ActiveModelSerializers is loaded, in the initializer use

ActiveSupport.on_load(:action_controller) do
...
end

results matching ""

    No results matching ""