diff --git a/lib/grape_entity/entity.rb b/lib/grape_entity/entity.rb index 90b4fbc0..44de4b8f 100644 --- a/lib/grape_entity/entity.rb +++ b/lib/grape_entity/entity.rb @@ -391,9 +391,17 @@ def self.present_collection(present_collection = false, collection_name = :items # @option options :only [Array] all the fields that should be returned # @option options :except [Array] all the fields that should not be returned def self.represent(objects, options = {}) - if objects.respond_to?(:to_ary) && ! @present_collection - root_element = root_element(:collection_root) - inner = objects.to_ary.map { |object| new(object, options.reverse_merge(collection: true)).presented } + collection_method_name = if objects.respond_to?(:to_ary) + :to_ary + elsif ! objects.nil? && ! objects.is_a?(Hash) && objects.respond_to?(:to_a) + :to_a + end + + if collection_method_name && ! @present_collection + root_element = root_element(:collection_root) + inner = objects.send(collection_method_name).map do |object| + new(object, options.reverse_merge(collection: true)).presented + end else objects = { @collection_name => objects } if @present_collection root_element = root_element(:root)