grape-entity
grape-entity copied to clipboard
present_collection issue
Hello,
Reading http://www.rubydoc.info/github/intridea/grape-entity/Grape%2FEntity.present_collection and I think there might be a bug in documentation. Definition
class Users < Grape::Entity
present_collection true
expose :items, as: 'users', using: API::Entities::Users
end
Usage
module API
class Users < Grape::API
version 'v2'
# this will render { "users" : [ { "id" : "1" }, { "id" : "2" } ], "version" : "v2" }
get '/users' do
@users = User.all
present @users, with: API::Entities::Users
end
end
Shouldn't expose :items, as: 'users', using: API::Entities::Users
have singular class name?
Now I get stack level too deep
error, but when I change from Users to User, it returns null.
Please advise. Thank you
My code:
api/events.rb
desc 'Recently booked'
get 'recently_booked' do
events = Event.active
present events, with: API::Entities::Events
end
api/entities/events.rb
module API
module Entities
class Events < Grape::Entity
present_collection true
expose :location
expose :events, using: API::Entities::Events
end
end
end
Ruby: 2.3.0
Rails: 4.2.5.1
Gems: 2.5.1
$ bundle | grep 'grape'
Using grape-entity 0.4.8
Using grape 0.14.0
Using grape-swagger 0.10.4
Using grape-kaminari 0.1.8
You have a class Users
that exposes items
as itself it seems, that is indeed why. You need a User
class to expose each entity.
I have a singular entity for Event.
First, I think you're right there's a bug in the documentation, but I didn't try. In your code you cannot expose :events
as API::Entities::Events
because that's the same class :) You should have another singular event.
First, lets fix this here. Take the README example and turn it into a spec here, if it doesn't already exist. Fix the spec and the README example, make a pull request. I can help with that if you get stuck.
:+1:
I just came here to bump the issue: documentation states
expose :items, as: 'users', using: API::Entities::Users
in plural even though it should refer to API::Entities::User
(singular).