Skip to content

Commit 50c460a

Browse files
author
Simeon Bateman
committed
changed represent to check for objects that respond to to_ary instead of checking to see if its an array. updated spec
1 parent 6d8793f commit 50c460a

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/grape/entity.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,16 @@ def self.root(plural, singular=nil)
143143
#  entity. Pass nil or false to represent the object or objects with no
144144
# root name even if one is defined for the entity.
145145
def self.represent(objects, options = {})
146-
inner = if objects.is_a?(Array)
147-
objects.map{|o| self.new(o, {:collection => true}.merge(options))}
146+
inner = if objects.respond_to?(:to_ary)
147+
objects.to_ary().map{|o| self.new(o, {:collection => true}.merge(options))}
148148
else
149149
self.new(objects, options)
150150
end
151151

152152
root_element = if options.has_key?(:root)
153153
options[:root]
154154
else
155-
objects.is_a?(Array) ? @collection_root : @root
155+
objects.respond_to?(:to_ary) ? @collection_root : @root
156156
end
157157
root_element ? { root_element => inner } : inner
158158
end

spec/grape/entity_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
subject.represent(Object.new).should be_kind_of(subject)
4747
end
4848

49+
it 'should return a single entity if called with a hash' do
50+
subject.represent(Hash.new).should be_kind_of(subject)
51+
end
52+
4953
it 'should return multiple entities if called with a collection' do
5054
representation = subject.represent(4.times.map{Object.new})
5155
representation.should be_kind_of(Array)

0 commit comments

Comments
 (0)