mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-04-27 14:57:43 +00:00
Ensures there is a root available for arrays
This commit is contained in:
parent
7933a1796b
commit
3b02d16211
2 changed files with 7 additions and 1 deletions
|
|
@ -13,6 +13,11 @@ module Grape
|
||||||
@endpoint = env["api.endpoint"]
|
@endpoint = env["api.endpoint"]
|
||||||
options = endpoint.namespace_options.merge(endpoint.route_options)
|
options = endpoint.namespace_options.merge(endpoint.route_options)
|
||||||
|
|
||||||
|
if resource.is_a?(Array) && !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)
|
serializer = ::ActiveModel::Serializer.build_json(endpoint, resource, options)
|
||||||
|
|
||||||
if serializer
|
if serializer
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@
|
||||||
#
|
#
|
||||||
module Grape
|
module Grape
|
||||||
module EndpointExtension
|
module EndpointExtension
|
||||||
|
attr_accessor :controller_name
|
||||||
|
|
||||||
def namespace_options
|
def namespace_options
|
||||||
settings[:namespace] ? settings[:namespace].options : {}
|
settings[:namespace] ? settings[:namespace].options : {}
|
||||||
end
|
end
|
||||||
|
|
@ -18,7 +20,6 @@ module Grape
|
||||||
def serialization_scope; end
|
def serialization_scope; end
|
||||||
def _serialization_scope; end
|
def _serialization_scope; end
|
||||||
def url_options; end
|
def url_options; end
|
||||||
def controller_name; end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Endpoint.send(:include, EndpointExtension)
|
Endpoint.send(:include, EndpointExtension)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue