Module: Grape::Formatters::ActiveModelSerializers

Defined in:
lib/grape/formatters/active_model_serializers.rb

Class Method Summary collapse

Class Method Details

.build_serializer_options(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/grape/formatters/active_model_serializers.rb', line 16

def self.build_serializer_options(env)
  ams_options = env[:active_model_serializer_options] || {}

  # Add serialization context
  ams_options.fetch(:serialization_context) do
    request = env['grape.request']
    ams_options[:serialization_context] = ::ActiveModelSerializers::SerializationContext.new(
      request_url: request.url[/\A[^?]+/],
      query_parameters: request.params
    )
  end

  ams_options
end

.call(resource, env) ⇒ Object



11
12
13
14
# File 'lib/grape/formatters/active_model_serializers.rb', line 11

def self.call(resource, env)
  serializer_options = build_serializer_options(env)
  ::ActiveModelSerializers::SerializableResource.new(resource, serializer_options).to_json
end