module Grape::Formatter::ActiveModelSerializers

Public Class Methods

build_options_from_endpoint(endpoint) click to toggle source
# File lib/grape-active_model_serializers/formatter.rb, line 61
def build_options_from_endpoint(endpoint)
  endpoint.namespace_options.merge(endpoint.route_options)
end
call(resource, env) click to toggle source
# File lib/grape-active_model_serializers/formatter.rb, line 5
def call(resource, env)
  serializer = fetch_serializer(resource, env)

  if serializer
    serializer.to_json
  else
    Grape::Formatter::Json.call resource, env
  end
end
default_root(endpoint) click to toggle source

array root is the innermost namespace name (‘space’) if there is one, otherwise the route name (e.g. get ‘name’)

# File lib/grape-active_model_serializers/formatter.rb, line 67
def default_root(endpoint)
  innermost_scope = endpoint.settings.peek

  if innermost_scope[:namespace]
    innermost_scope[:namespace].space
  else
    endpoint.options[:path][0].to_s.split('/')[-1]
  end
end
fetch_serializer(resource, env) click to toggle source
# File lib/grape-active_model_serializers/formatter.rb, line 15
def fetch_serializer(resource, env)
  endpoint = env['api.endpoint']
  options = build_options_from_endpoint(endpoint)

  if resource.respond_to?(:to_ary) && !resource.empty?
    # ensure we have an root to fallback on
    endpoint.controller_name = default_root(endpoint)
  end

  ::ActiveModel::Serializer.build_json(endpoint,
                                       resource,
                                       options.merge(other_options)
                                      )
end
meta() click to toggle source
# File lib/grape-active_model_serializers/formatter.rb, line 45
def meta
  @meta_content_items || {}
end
meta=(meta_content) click to toggle source
# File lib/grape-active_model_serializers/formatter.rb, line 49
def meta=(meta_content)
  @meta_content_items = { meta: meta_content } if meta_content
end
meta_key() click to toggle source
# File lib/grape-active_model_serializers/formatter.rb, line 53
def meta_key
  @meta_key || {}
end
meta_key=(key) click to toggle source
# File lib/grape-active_model_serializers/formatter.rb, line 57
def meta_key=(key)
  @meta_key = { meta_key: key } if key
end
other_options() click to toggle source
# File lib/grape-active_model_serializers/formatter.rb, line 30
def other_options
  options = {}
  if @meta_content_items
    meta_option = @meta_content_items[:meta]
    @meta_content_items.delete(:meta)
    options[:meta] = meta_option if meta_option
    if @meta_key
     key_option = @meta_key[:meta_key]
      @meta_key.delete(:meta_key)
      options[:meta_key] = key_option if key_option
    end
  end
  options
end