ruby-grape / ruby-grape/grape

Grape::API can't find shared subclass

Open
#1,712 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug?
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

Ruby is so magical that I need help from someone who knows a little better than I do.

Since I have a lot of crud basic endpoints repeated over and over I decided to automate their creations (see my final post in #1709 ).

For the sake of having some basic valid swagger documentation I automated also the creation of the Grape::Entities by adding some custom code to my ApplicationRecord (see github SOLUTION snippet added to this question: https://stackoverflow.com/questions/47371243/inheriting-class-definition-from-parent-class).

With the above 2 customizations I can create API endpoint by simply doing:

V1::Base.show Book

The above method will create an endpoint and it will use the automagically created Entity like so that it gets automagically documented with default values that can be overridden later on if needed in the Book model class:

Book::Entity

There is 1 exception when the above code seems to break: it seems that whenever I interact in an early state during the bootstrap of my Rails application (initializers for example), when I call V1::Base.show Book (to create the api endpoint) which calls Book::Entity the following error is returned:

NameError: uninitialized constant Book::Entity

Could anybody give me a hint on what it could be? the early interaction with the model can be a simple Book.all, it smells like a thread issue.

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

No repository file or test is named. Start by reproducing the initializer or early-bootstrap call to V1::Base.show Book and trace when Book::Entity is looked up, then compare it with a later Book.all call; done means the early interaction no longer raises NameError.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.