5

I need some help to get Grape::API up and running with Rails 4. I get a Unable to autoload constant Base even though a puts tells me that the class is loaded. What am I doing wrong?

app/api/api.rb

class API < Grape::API
  prefix 'api'
  format :json
  default_format :json
  mount V1::Base   # Everything loads perfectly until I add this line.
end

app/api/v1/base.rb

module V1
  class Base < API
    puts "=== DEBUG - in Base"
    version 'v1', using: :path, vendor: 'orwapp', cascade: false

    mount Users

  end
end

$ rspec spec/api

12:58:29 - INFO - Run all
12:58:29 - INFO - Running all specs
=== DEBUG - in Base
/dependencies.rb:481:in `load_missing_constant': 
Unable to autoload constant Base,
 expected /Users/martins/Work/myapp/app/api/v1/base.rb to define it (LoadError)
        from /Users/martins/Work/myapp/app/api/api.rb:9:in `<class:API>'
        from /Users/martins/Work/myapp/app/api/api.rb:3:in `<top (required)>'

spec/api/users_spec.rb

describe 'GET /api/v1/users/:id', focus: true do
  let(:user) { Fabricate :user }

  it 'returns that specific user' do
    get "/api/v1/users/#{ user.id }", {}, https_and_authorization
    response.status.should eq 200
    parse_response_for(:user)['email'].should eq user.email
  end
end

The versions I'm using

$ ack grape Gemfile.lock
      remote: git://github.com/intridea/grape.git
        grape (0.9.1)
        grape-entity (0.4.4)
        grape-swagger (0.8.0)
          grape
          grape-entity
martins
  • 9,669
  • 11
  • 57
  • 85

2 Answers2

1

Try having Base inherit from Grape::API instead of API:

module V1
  class Base < Grape::API
  ...

By having it inherit from API you're creating a circular dependency: The interpreter can't know the definition of V1::Base until it knows the definition of API, but for that it would first need to know the definition of V1::Base, and so on.

  • 1
    Thanks for the suggestion. It didn't make any difference, though. I'm not having any problems with circular dependencies. The error I get is **Unable to autoload constant Base** – martins Sep 10 '14 at 14:05
0

Changing to mount ::V1::Base fixed it.

martins
  • 9,669
  • 11
  • 57
  • 85