Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reloading not working correctly (Rails engine) #358

Closed
lucaspiller opened this issue Mar 7, 2013 · 12 comments · Fixed by #849
Closed

Reloading not working correctly (Rails engine) #358

lucaspiller opened this issue Mar 7, 2013 · 12 comments · Fixed by #849

Comments

@lucaspiller
Copy link

I have grape set up within a Rails engine for better separation of concerns, however the reloading suggestion in the documentation doesn't work for me. I also have to set ActiveSupport::Dependencies.explicitly_unloadable_constants.

In the engine config I have this:

# auto reload api in development mode
initializer 'reload_api' do
  if Rails.env.development?
    ActiveSupport::Dependencies.explicitly_unloadable_constants << "API::Resources"

    ["app/resources/**/*.rb"].each do |glob|
      files = Dir["#{API::Engine.root}/#{glob}"]
      reloader = ActiveSupport::FileUpdateChecker.new(files) do
        Rails.logger.info ">> Reloading API"
        Rails.application.reload_routes!
      end
      ActionDispatch::Callbacks.to_prepare do
        reloader.execute_if_updated
      end
    end
  end
end

I have no idea why this works, but heh. Might be worth adding this to the readme or wiki :)

@dblock
Copy link
Member

dblock commented Mar 8, 2013

I would love this added to the README, but I do also want an explanation of what explicitly_unloadable_constants really does :)

@lucaspiller
Copy link
Author

Sorry I forgot about this.

From the Rails docs:

"An array of constant names that need to be unloaded on every request. Used to allow arbitrary constants to be marked for unloading."

https://github.com/rails/rails/blob/3-2-stable/activesupport/lib/active_support/dependencies.rb#L51-54

Again, why it works I do not know. It doesn't reload them on every request, only when the resources have changed - but without that line nothing is reloaded.

@dblock
Copy link
Member

dblock commented Apr 24, 2013

Ok, this makes at least some sense. Could you make a pull request with the README update, please?

@changx
Copy link

changx commented Jun 5, 2013

finally ActiveSupport::Dependencies.explicitly_unloadable_constants << "API" rescued me

@dblock
Copy link
Member

dblock commented Jun 5, 2013

@ccpp0 Can you PR this into the README, please?

@dblock
Copy link
Member

dblock commented Mar 7, 2014

@lucaspiller Bump?

1 similar comment
@pusewicz
Copy link

@lucaspiller Bump?

@tomconroy
Copy link
Contributor

Using require_dependency instead of require when mounting other API classes seems to be important

@dblock
Copy link
Member

dblock commented Nov 21, 2014

This seems relevant: #658. Can someone take a look and finish this?

@dblock
Copy link
Member

dblock commented Dec 14, 2014

I made a change based on all of this in #849, if something is not right there, please open a new issue / PR.

@AndrewRayCode
Copy link

AndrewRayCode commented Feb 4, 2019

I'm having the same problem in Rails 5. I have this in an initializer:

# See https://github.com/ruby-grape/grape#reloading-api-changes-in-development
if Rails.env.development? || Rails.env.test?
  ActiveSupport::Dependencies.explicitly_unloadable_constants << 'MyEngine::API'

  api_files = Dir[MyEngine::Engine.root.join('lib', '**', '*.rb')]

  api_reloader = ActiveSupport::FileUpdateChecker.new(api_files) do
    Rails.application.reload_routes!
  end

  ActiveSupport::Reloader.to_prepare do
    api_reloader.execute_if_updated
  end
end

And in my engines routes.rb file I have, fairly vanilla:

MyEngine::Engine.routes.draw do
  mount MyEngine::API => 'api'
end

When I first load the app the grape api works fine and the API result is returned. If i change smoething in api.rb it appears to auto-reload fine.

However, when I visit the main application's routes, then it errors with:

LoadError (Unable to autoload constant MyEngine::API, expected /app/my_engine/my_api/lib/my_engine/api.rb to define it):

The weird thing is that file does exit, and does correctly autoload the first time. After visiting the main app's route, the nested Grape API no longer works either.

This still errors even if I explicitly require above:

MyEngine::Engine.routes.draw do
  require_dependency MyEngine::Engine.root.join('lib', 'my_engine', 'api.rb').to_s
  mount MyEngine::API => 'coverage'
end

The require_dedpendency works fine, but the next line errors after a reload

@mileslane
Copy link

ActiveSupport::Dependencies private API has been deleted in Rails 7. What should we do now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants