如何在开发模式下的每个请求上重新加载gem?

时间:2011-05-31 18:07:35

标签: ruby-on-rails-3

我有一个Rails应用程序,它使用我正积极开发的gem。如何指示应用程序在每次请求时重新加载gem?

5 个答案:

答案 0 :(得分:18)

This solution几乎可以正常工作,但由于某种原因,我必须将其放入application.rb而不是environments/development.rb,否则无法识别autoload_path。

我添加了一些自动获取路径的其他内容。

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

可以使用gem 'my_gem', :path => '../my_gem'

添加本地宝石

答案 1 :(得分:5)

您可以在应用的autoload路径中添加gem的路径。

因此,在config / application.rb中,在class Application < Rails::Application ... end块中添加:

config.autoload_paths += %W(#{config.root}/vendor/gems/my_gem/lib)
config.autoload_paths += Dir["#{config.root}/vendor/gems/my_gem/lib/**/"]

然后,在运行开发服务器时,中的所有文件都应重新加载。

答案 2 :(得分:2)

对于引擎:

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

答案 3 :(得分:2)

我刚刚找到了很棒的https://github.com/colinyoung/gem_reloader - 它对我有用!

答案 4 :(得分:-3)

超级简单的修复:

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