From 773082a2e3887df0ed1005e8aacd4bfbc0116d44 Mon Sep 17 00:00:00 2001 From: Martijn Bleeker Date: Fri, 6 Nov 2015 17:38:41 +0100 Subject: [PATCH] Add support for Sequel datasets Sequel dataset do not support the `to_ary` implementation. To add support, check if `objects` is a list. `nil` and `Hash` both implement `to_a` but aren't lists themselves. Fixes #188 --- lib/grape_entity/entity.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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)