如何强制我的插件重新加载每个请求?

据我了解,在开发模式下,每个请求都不会在Rails中重新加载插件。 这是有道理的,因为通常你会在你的应用程序中添加插件,而这正是你正在开发的应用程序。

但是,如果您正在开发插件,则必须在每次更改插件时重新启动服务器,这会产生很大的开销。

有没有办法让Rails在开发过程中重新加载你的插件,重新加载模型和控制器的方式?

如果在插件代码更改时自动重新启动服务器是一个可以接受的解决方案,那么你可以使用Mike Clark / topfunky的rstakeout ,或者更新的watchr听起来像是做同样的事情。

你会做这样的事情:

 rstakeout 'touch tmp/restart.txt' 'vendor/plugins/**/*' 

我也一直在努力解决这个问题。 这些解决方案都不起作用,包括autoload_pathsautoload_once_paths技巧。 此外,使用FileUpdateChecker和初始化程序的hack也无济于事(检查程序正确触发,但文件未重新加载)。 config.reload_plugins = true相同…

但是,有一个解决方案。 在app/controllers/application_controller.rb添加一行: require_dependency 'your_file_name_here'每次请求都会重新加载应用程序控制器, require_dependency会检查您的文件是否有修改并相应地重新加载。 它适用于我,Apache 2,Passenger 3和Rails 3.0.3。

我是通过使用猎枪gem来做到这一点的。

gem install shotgun

cd /path/to/rails/app

shotgun

响应时间较慢,但重新加载所有rails代码,而不是浪费时间编写autoload_paths

简单的方法,在“app”文件夹内的文件夹中开发插件:

  • 应用
    • 楷模
    • 控制器
    • 助手
    • 意见
    • your_plugin_here

这样,您的所有插件类都将在每个请求上重新加载。

另一种可能性是在application.rb文件中添加路径:

 require File.expand_path('../boot', __FILE__) require 'rails/all' Bundler.require(:default, Rails.env) if defined?(Bundler) module SunspotTutorial class Application < Rails::Application config.autoload_paths += %W{ #{config.root}/plugins/your_plugin_name/lib } #lots of other code end end 

这样你的插件就会一直重新加载。

使用https://github.com/ranmocy/guard-rails ,它非常简单:

 # Gemfile.local gem 'guard-rails' $ bundle $ guard init rails # Guardfile: guard 'rails' do watch('Gemfile.lock') watch(%r{^(config|plugins)/.*}) end $ bundle exec guard 

这个解决方案适用于引擎,适用于Rails 2.3,但附带一个cavaet,它会在每个请求中加载引擎和父应用程序中的所有文件,这将使响应时间变慢。

 # lib/my_engine/engine.rb if ENV['RELOAD_MYENGINE'] && Rails.env.development? config.to_prepare do Rails.logger.debug "RELOADING MY ENGINE" require_dependency MyEngine::Engine.root.join('lib', 'my_engine').to_s end config.after_initialize do Rails.application.config.reload_classes_only_on_change = false end 

然后启动服务器:

 RELOAD_MYENGINE=1 rails server