开发模式下的 Rails 自动重新加载插件 [英] Rails auto-reloading plug in development mode
问题描述
我正在尝试开发一个插件,每次更改代码时我都会开发自动重新加载,模拟 Rail 开发模式中正常发生的相同自动重新加载.我的插件主要是一个 ActiveRecord 混合模块.我已经尝试了所有可以在相关 Google 搜索中找到的建议.一切都还没有奏效.
I am trying to have a plugin I am developing auto-reload every time I change my code, emulating the same auto-reloading that happens normally in Rail's development mode. My plugin is primarily an ActiveRecord mixin module. I have tried all suggestions I have been able to find in related Google searches. Nothing has worked yet.
在我插件的 init.rb 中:
In my plugin's init.rb:
require 'activesupport' unless defined? ActiveSupport
require 'activerecord' unless defined? ActiveRecord
if RAILS_ENV == 'development'
ActiveSupport::Dependencies.load_once_paths.delete lib_path
ActiveSupport::Dependencies.load_once_paths.delete File.join(lib_path, 'crowd_compass', 'publisher.rb')
ActiveSupport::Dependencies.load_paths << lib_path
ActiveSupport::Dependencies.load_paths << File.join(lib_path, 'crowd_compass', 'publisher.rb')
end
ActiveRecord::Base.send(:include, CrowdCompass::Publisher)
查看 rails 更新日志,我确实注意到自动重新加载所有插件的功能.
Looking in the rails changelog, I did notice the feature to auto reload all plugins.
config.reload_plugins = true if RAILS_ENV == 'development'
当我将它添加到我的 conf/environment.rb 时,这并没有像我预期的那样工作
This did not work as I expected it to when I added it to my conf/environment.rb
我的插件是结构化的,所以所有文件都由命名空间 => 目录自动加载.我这样做是为了避免使用require",因为我认为 require 会阻止我的插件自动重新加载.
My plugin is structured so all files are auto-loaded by namespace => directory. I did this so I could avoid using "require", as I thought require was inhibiting my plugin from being auto-reloaded.
我一直通过 rails 控制台在开发模式下完成我的所有工作,我不知道这与通过 mongrel(或类似 Web 服务器)运行是否有任何不同.
I have been doing all of my work in development mode through the rails console and I do not know if this behaves any different than running through mongrel (or like web server).
插件按预期工作,但每次对代码进行任何更改时都必须重新加载.有谁知道让插件重新加载的方法?
The plugin works as expected, but I have to reload every time I make any change to the code. Does anyone know a way to get plugins to reload?
推荐答案
控制台肯定不会像杂种一样工作.您使用的所有技术都会在每次请求时重新加载,这类似于您每次启动控制台.
The console definitely doesn't work like a mongrel. All of the techniques you're using are made to reload on every request, which is akin to every time you start up the console.
没有调用 reload!
就可以在控制台中重新加载代码的方法.
There isn't a way to reload code in the console without calling reload!
.
这篇关于开发模式下的 Rails 自动重新加载插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!