mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-04-27 14:57:43 +00:00
Refactors Formatter
This commit is contained in:
parent
811b6ce583
commit
dddd528ea4
2 changed files with 17 additions and 9 deletions
|
|
@ -105,6 +105,7 @@ See "Writing Tests" in https://github.com/intridea/grape.
|
||||||
|
|
||||||
Enjoy :)
|
Enjoy :)
|
||||||
|
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
1. Fork it
|
1. Fork it
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,7 @@ module Grape
|
||||||
module ActiveModelSerializers
|
module ActiveModelSerializers
|
||||||
class << self
|
class << self
|
||||||
def call(resource, env)
|
def call(resource, env)
|
||||||
endpoint = env['api.endpoint']
|
serializer = fetch_serializer(resource, env)
|
||||||
options = endpoint.namespace_options.merge(endpoint.route_options)
|
|
||||||
|
|
||||||
if resource.respond_to?(:to_ary) && !resource.empty?
|
|
||||||
# ensure we have an root to fallback on
|
|
||||||
endpoint.controller_name = resource.first.class.name.underscore.pluralize
|
|
||||||
end
|
|
||||||
|
|
||||||
serializer = ::ActiveModel::Serializer.build_json(endpoint, resource, options)
|
|
||||||
|
|
||||||
if serializer
|
if serializer
|
||||||
serializer.to_json
|
serializer.to_json
|
||||||
|
|
@ -19,6 +11,21 @@ module Grape
|
||||||
Grape::Formatter::Json.call resource, env
|
Grape::Formatter::Json.call resource, env
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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 = resource.first.class.name.underscore.pluralize
|
||||||
|
end
|
||||||
|
::ActiveModel::Serializer.build_json(endpoint, resource, options)
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_options_from_endpoint(endpoint)
|
||||||
|
endpoint.namespace_options.merge(endpoint.route_options)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue