3
votes

In development mode, the Grape API files in our Rails app aren't reloading as they should. We've tried the suggested approach in the Grape README, and referred to what seems to be the key SO post on it (Ruby on Rails 3 - Reload lib directory for each request) but it's not working for some reason.

In the config/application.rb:

config.paths.add File.join("app", "api"), glob: File.join("**", "*.rb")
config.autoload_paths += Dir[Rails.root.join("app", "api", "*")]

And in initializers/reload_api.rb:

if Rails.env == "development"
  api_reloader = ActiveSupport::FileUpdateChecker.new(Dir[Rails.root.join('app', 'api', '**', '*.rb')]) do
    puts ">>>> RELOADING!"
    Rails.application.reload_routes!
  end

  ActionDispatch::Callbacks.to_prepare do
    api_reloader.execute_if_updated
  end
end

When I change a file under app/api, I can see the "RELOADING!" message in the log so I know the api_reloader callback is being invoked. But the changes are not taking effect. This makes me suspect the config.autoload_paths, but it all looks correct.

This is using Rails 3.2. Suggestions would be fantastic, since obviously it's super-annoying to not have reloading working on this stuff.

1
How are you mounting the API?dB.
The API is mounted with mount Api => '/api' in routes.rb, and then in the main /app/api/api.rb: class Api < Grape::API; cascade false; mount Acme::MyApi; endMasonoise

1 Answers

0
votes

If you're doing that just for the routes, I found a workaround for that. I'm using an rake task to show just the routes of GRAPE.

Link of the rake task: https://gist.github.com/pragmaticivan/9a9d925e7bbcc8df72f6