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



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

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



13
14
15
16
# File 'lib/grape/formatters/active_model_serializers.rb', line 13

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