How to reload a gem on every request in Development mode? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 y开发者_开发问答ears ago.
Improve this questionI've got a Rails app which uses a gem I'm actively developing. How can I instruct the app to reload the gem on every request?
This solution almost works but for some reason I have to put it into application.rb
and not in environments/development.rb
otherwise the autoload_paths are not recognized.
I added some additional stuff which fetches the paths automagically.
if Rails.env.development?
reload_gems = %w(my_gem other_gem) # names of gems which should autoreload
config.autoload_paths += Gem.loaded_specs.values.inject([]){ |a,gem| a += gem.load_paths if reload_gems.include? gem.name; a }
require 'active_support/dependencies'
ActiveSupport::Dependencies.explicitly_unloadable_constants += reload_gems.map { |gem| gem.classify }
end
Local gems can be added with gem 'my_gem', :path => '../my_gem'
You could add the path to the gem in the autoload
paths for the app.
So, in config/application.rb, within the class Application < Rails::Application ... end
block add:
config.autoload_paths += %W(#{config.root}/vendor/gems/my_gem/lib)
config.autoload_paths += Dir["#{config.root}/vendor/gems/my_gem/lib/**/"]
Then, when running your development server, all files in there should be reloaded.
For an engine:
module Copycat
class Engine < ::Rails::Engine
if Rails.env.development?
config.to_prepare do
Rails.logger.debug "RELOADING COPYCAT"
require_dependency Copycat::Engine.root.join('lib', 'copycat').to_s
end
config.after_initialize do
# optional, without it will call `to_prepend` only when a file changes,
# not on every request
Rails.application.config.reload_classes_only_on_change = false
end
end
end
end
I've just found awesome https://github.com/colinyoung/gem_reloader - it works for me!
Super simple fix:
In yourApp/config/envirornments/development.rb:
YourApp::Application.configure do
# Make sure both of these two settings are set to false, add them if you can't find them
config.cache_classes = false
config.action_controller.perform_caching = false
#
#
# Other config settings...
#
#
end
精彩评论