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

Exposing a List of Entities #186

Open
Linell opened this issue Oct 26, 2015 · 2 comments
Open

Exposing a List of Entities #186

Linell opened this issue Oct 26, 2015 · 2 comments
Labels

Comments

@Linell
Copy link

Linell commented Oct 26, 2015

So I have an Employee entity that looks something like this.

class Employee::Entity < Grape::Entity
  expose :id, :name, :company
end

The Employee has tons of clock ins to the timecard system. The entity for a Clockin is something like:

class Clockin::Entity < Grape::Entity
  expose :id, :year, :date, :code, :description
end

Displaying all of the clock ins for an employee is easy enough. How would I go about segregating them by year, though?

My first thought was adding something like this to the Employee entity

expose :clockins do |employee, options|
  employee.clockins.group_by { |c| c[:year] }
end

That does get the correct data, but it's not presented correctly.

@dblock dblock added the bug? label Oct 26, 2015
@dblock
Copy link
Member

dblock commented Oct 26, 2015

Define doesn't get presented correctly? The structure returned from a group_by is a hash, so you should use a new presenter for it that knows how to deal with it?

@Linell
Copy link
Author

Linell commented Oct 26, 2015

Instead of exposing only the items explicitly exposed in the Clockin entity, I get the entire object.

I've actually tried it this way as well,

expose :clockins, :using => Clockin::Entity do |employee, options|
  employee.clockins.group_by { |c| c[:year] }
end

, which just results in an object full of null values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants