Ensures there is a root available for arrays

This commit is contained in:
John Allen 2013-05-16 18:58:35 -04:00
parent 7933a1796b
commit 3b02d16211
2 changed files with 7 additions and 1 deletions

View file

@ -13,6 +13,11 @@ module Grape
@endpoint = env["api.endpoint"]
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)
if serializer

View file

@ -6,6 +6,8 @@
#
module Grape
module EndpointExtension
attr_accessor :controller_name
def namespace_options
settings[:namespace] ? settings[:namespace].options : {}
end
@ -18,7 +20,6 @@ module Grape
def serialization_scope; end
def _serialization_scope; end
def url_options; end
def controller_name; end
end
Endpoint.send(:include, EndpointExtension)