grape-entity icon indicating copy to clipboard operation
grape-entity copied to clipboard

present_collection issue

Open giedriusr opened this issue 9 years ago • 5 comments

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

image

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

giedriusr avatar Feb 05 '16 08:02 giedriusr

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.

dblock avatar Feb 05 '16 12:02 dblock

I have a singular entity for Event.

giedriusr avatar Feb 05 '16 14:02 giedriusr

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.

dblock avatar Feb 05 '16 14:02 dblock

:+1:

giedriusr avatar Feb 05 '16 14:02 giedriusr

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).

pre avatar Aug 12 '16 13:08 pre