mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
* Fix #1759, Grape integration, adds serialization_context - `serialization_context` is added in grape formatter so grape continues to render models without an explicit call to the `render` helper method - Made it straightforward for subclasses to add other serializer options (such as `serialization_scope`). * Updated Grape tests to include: - paginated collections - implicit Grape serializer (i.e. without explicit invocation of `render` helper method) * Update Changelog with fixes.
33 lines
1.1 KiB
Ruby
33 lines
1.1 KiB
Ruby
# A Grape response formatter that can be used as 'formatter :json, Grape::Formatters::ActiveModelSerializers'
|
|
#
|
|
# Serializer options can be passed as a hash from your Grape endpoint using env[:active_model_serializer_options],
|
|
# or better yet user the render helper in Grape::Helpers::ActiveModelSerializers
|
|
|
|
require 'active_model_serializers/serialization_context'
|
|
|
|
module Grape
|
|
module Formatters
|
|
module ActiveModelSerializers
|
|
def self.call(resource, env)
|
|
serializer_options = build_serializer_options(env)
|
|
::ActiveModelSerializers::SerializableResource.new(resource, serializer_options).to_json
|
|
end
|
|
|
|
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
|
|
end
|
|
end
|
|
end
|