Always expose with the default entity
I'm using grape and grape-entity in a Rails 4.2.1 project, and I'm running into a weird error with presenting using the right model.
According to the documentation, organizing my entities within each model results in this:
Grape will automatically detect the Entity class and use it to present your models.
In addition, the documentation also says:
By default every object of a collection is wrapped into an instance of your Entity class.
Here's the code I have now.
class User < ActiveRecord::Base
class Entity < Grape::Entity
expose :id, :name
expose :addresses
end
end
class Address < ActiveRecord::Base
class Entity < Grape::Entity
expose :id, :street1
end
end
If I don't do expose :addresses, with: Address:Entity, it doesn't work, and still exposes all the fields of the address. Any reason it's not automatically detecting the correct entity?
I think what the documentation means is that Grape will automatically detect the Entity class that corresponds to the name of the model you're currently in. So it would automatically present your User model with the User entity, but since Adress is another model, I assume you'd still have to handle it like a normal nested exposure, i.e. specify which entity you want to present it with.
Right. I think the feature request here is to be able to say that Address is always exposed with Address::Entity unless otherwise specified.
Would that cause issues if the model in question actually has an attribute with the same name as the nested model you want to expose with an entity?
@Morred I think it shouldn't queue of the name, but of the type of the object itself, and would actually allow mixed bags of objects to be presented with different entity types.
Ah, got it -- I thought I was just doing it wrong, not that this was actually a new feature request.
Agreed @dblock on your last comment, it should look at the type of the object to figure out what entity type it should use.