mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-04-16 12:25:48 +00:00
Adding methods into the Grape::Endpoint class that ActiveModel::Serializers are relying on from ActionController as well as a few convenience methods for Route and Namespace options. I believe we can expand on some of the empty methods here to allow for higher (api) level option setting and defaults. This is a first step in that direction.
27 lines
662 B
Ruby
27 lines
662 B
Ruby
require 'active_record'
|
|
|
|
module Grape
|
|
module Formatter
|
|
module ActiveModelSerializers
|
|
class << self
|
|
attr_accessor :infer_serializers
|
|
attr_reader :endpoint
|
|
|
|
ActiveModelSerializers.infer_serializers = true
|
|
|
|
def call(resource, env)
|
|
@endpoint = env["api.endpoint"]
|
|
options = endpoint.namespace_options.merge(endpoint.route_options)
|
|
|
|
serializer = ::ActiveModel::Serializer.build_json(endpoint, resource, options)
|
|
|
|
if serializer
|
|
serializer.to_json
|
|
else
|
|
Grape::Formatter::Json.call resource, env
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|