-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Present with multiple entities #241
Comments
It's a common pattern that a lot of people like in their API. I would extend the existing |
Do you like the array syntax? present [[ @coupons, with: Entities::Users], [ @pagination, with: Entities::Pagination, root: 'pagination' ]] |
Not as much as I like hashes :) Seems like all those array structures are redundant. This could be a 2-parameter function def present(variable, * options)
...
end present @coupons, with: Entities::Users, root: 'pagination' |
How would you know when the first entities options ends and the second begins? |
In Ruby this is called splat. It's one of my personal favorite features :) |
I'm familiar with splat. So please excuse my ignorance, but I'm not sure I follow how the syntax would work with multiple variables and options? My array syntax lets you customize the present for each variable separately. Can you convert this example? present [[ @users, with: Entities::Users, root: 'users' ], [ @pagination, with: Entities::Pagination, root: 'pagination' ]] |
I guess I didn't include the most important part in my answer :) {
users: present @users, with: ...
pagination: present @pagination, with ...
...
} Rather than a top-level present. |
That makes a lot more sense :) Definitely cleaner then mine. I'll see if I have some time over the next week or so to implement. |
@adamgotterer I'll be releasing a new Grape version soon, LMK how close/far this is. |
I haven't had a chance to work on this yet, been pretty busy. When is the next version out? |
@dblock @adamgotterer Are you thinking that in the resource you'd have get '/users' do
{
users: present @users, with: UserEntity,
pagination: present @pagination, with: PaginationEntity
}
end Just trying to get a feel for this feature. |
Yes, that would look good! |
I feel this breaks a few things in terms of "who's responsible for what". The grape entities, from my point of view, is the view layer in grape. While this syntax makes sense, I feel like it's giving mixed responsibility for the resource. Because now it relies on how you format data in 2 places. I think a better solution could be an entity method that excepts "top-level" exposures OR present @users, with: UserEntity
present @pagination, with: PaginationEntity Would yield:
|
What about an optional symbol/string first argument that defines a key on a root hash, e.g. present :users, @users, with: UserEntity
present :pagination, @pagination, with: PaginationEntity Of course existing functionality should remain working as-is. |
Or: present @users, as: :users, with: UserEntity
present @pagination, as: :pagination, with: PaginationEntity |
Without the |
And note that this is not a compilable hash, you can't make a key a function. {
present :users, @users, with: UserEntity,
present :pagination, @pagination, with: PaginationEntity
} |
hey guys, any progress here? |
I've been insanely busy and haven't found any time to make contributions the last two months :( It's still on my todo, but if someone wants to take a stab at it, it's fine with me. Sorry for the delay, I know people are looking forward to this. |
Hi guys, I came to this recently. When using other gems , like As grape-entity doing this by introduce a Entity {
page: 1,
users: users.map {|user| UserEntity.new(user)}
} a little verbose..... so I want a DSL, I prefer to what @mbleigh describe
It's easy to implement, and can be compatible.But if the result is
we had to write three lines
If choosing the hash way {
page: 1 ,
total_page: 2,
users: present @users, with: ...
} each present in the hash value may need I can do the two implementions in the next few days, leaving the choice to the developer if nobody had working with this |
Implemented on HEAD. Thanks @niedhui. |
Sorry to ping on this old issue, but i have one more question about Yes, now i can place multiple |
@calfzhou I don't think I understand. Can you restate it in a new thread on the Grape mailing list? |
I'm sorry to comment on this closed issue but I'm running into the same issue that @calfzhou had. Was there a solution discussed for it? Thank you! |
The response would be the combination of two entities. I've run into this issue with things like pagination blocks. I've had to hack around with body to get something like:
Would be interesting to have a "present multi" feature. Maybe something along the lines of:
Would love to start a discussion and hear other peoples thoughts?
The text was updated successfully, but these errors were encountered: