Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Sequel datasets #189

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think storing the method name is strange, or at least not idiomatic. How's this?

ary = if objects.respond_to?(:to_ary)
  objects.to_ary
elsif ! objects.is_a?(Hash) && objects.respond_to?(:to_a) #nil? is redundant here
  objects.to_a
end

if ary && !@present_collection
  root_element = root_element(:collection_root)
  inner = ary.map ...
elsif @present_collection
  objects = { @collection_name => objects }
end

root_element = root_element(:root)
Expand Down