Skip to content

Added 'only' option that selects which attributes should be returned #114

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

Closed
wants to merge 1 commit into from

Conversation

estevaoam
Copy link
Contributor

This PR intends to add an option named only, when representing an object or collection.

The motivation for this is that we use both grape and grape-entity intending to creating a well-structured and performant API.

Some great service APIs, like Facebook and Github ones allow the consumer for the API to choose which fields he wants in this request. Also, this article is a very interesting read.

Based on this, I developed this feature into grape-entity, and I would love to check some opinion about this.

Thanks. 😃

expose :user, using: UserEntity
end

data = Entity.represent(model, only: 'name,user.name,user.email')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really think that only should take an array or a single symbol, not a comma-delimited string.

@dblock
Copy link
Member

dblock commented Mar 13, 2015

I really like this, except for the only DSL. I think it should be strongly typed, example:

Entity.represent(model, only: [:name, user: [:name, :email]])

It's easy to turn a comma-delimited string above into something like this, and if you accept such a string you should be really validating its syntax, which I feel is outside of the scope of this gem.

You also don't need to support a comma-delimited version at all, Rack supports passing arrays in, so you could make a query like only[]=name&only[user][]=name&only[user][]=email. It feels a bit ugly but it's standard (and you can make it less ugly by having a middleware that does the parsing into params.

@estevaoam
Copy link
Contributor Author

@dblock thanks for your comment. I agree with you, I was blind in the mindset of an API paying attention about how I needed to pass to grape-entity so I can have this feature.

You're right, it makes more sense that some middleware takes care of it instead of the gem.

I'm gonna work on those changes. Thanks for your insight!

@estevaoam
Copy link
Contributor Author

@dblock When you have a chance, could you take a look on my modifications? Thanks!

@@ -2,6 +2,7 @@
============

* Your contribution here.
* [#114](https://github.com/intridea/grape-entity/pull/114): Added 'only' option that selects which attributes should be returned
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swap this with the line above.

@dblock
Copy link
Member

dblock commented Mar 15, 2015

This looks good. Please fix the build and squash the commits. To fix the build just do a rubocop --auto-gen-config.

@estevaoam
Copy link
Contributor Author

@dblock done. 👍

@dblock
Copy link
Member

dblock commented Mar 16, 2015

Merged via 6375f15.

For next time, Rubocop's workflow is like this:

rubocop -a # auto-correct
rubocop --auto-gen-config # update config with any unfixed violations

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

Successfully merging this pull request may close these issues.

2 participants