intridea/grape — Issue #73: Question! Entities vs. Models
Author: micahbrich
Created: 2011-11-19 21:58:29+00:00
State: open

Hey, I have more of a conceptual question than an issue, really. The current Entity mechanism is detached from the models I already have, right? There's a separate model I have to define, and then in a Grape method, I have to say `present` my model with this `Entity` model.

But before the Entity stuff, I was actually achieving a similar result just by making a method in my model that outputs a hash with the attributes I want, e.g.

```ruby
# my model
class User
field :email
field :username
field :firstname
field :lastname
def api
{ email: self.email, username: self.username }
end
end

# my api
class API < Grape::API
version 'v1'
get '/users/:id' do
user = User.first
user.api.to_json
end
end

````

So my imagination is thinking, what if the Entity mechanism were more like:

```ruby
# my model
class User
field :email
field :username
field :firstname
field :lastname

include ExposableAttributes
expose :email, :username
end

# my api
class API < Grape::API
version 'v1'
get '/users/:id' do
user = User.first
user.api.to_json
end
end

````

So, it were just an includable Module, with the `expose` method, that assembles a hash, it seems like it'd be less code with a prettier result – would there be any big downsides to something like that, as opposed to the current way?