ruby-grape / ruby-grape/grape-entity

Always expose with the default entity

Open
#185 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Ruby
Stars
729
Forks
154
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the entity-organization documentation and the nested User::Entity and Address::Entity example in the issue. Reproduce the collection presentation with and without expose :addresses, with: Address::Entity, then trace how the default entity is selected. Done means the behavior is explained and the nested address uses the intended entity, with a regression test if the repository has coverage for this path.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.