Rails:在environment.rb中获取被调用的rake任务的名称?

时间:2010-11-11 14:47:23

标签: ruby-on-rails rake environment-variables ruby-on-rails-plugins autoload

我尝试在我的 environment.rb 中为rake任务“ gems:install ”禁用插件的自动加载,因为它可能导致无法解决的依赖关系和错误(读取http://blog.joopp.com/2009/01/26/plugin-gem-dependencies-in-your-environmentrb/了解更多信息)。

在实施此“hack”时,我很快注意到变量 $ rails_gem_installer ,如果运行gems:install,未设置,则应设置为true。 (==无)

现在我正在寻找获取有关被叫rake任务的信息的方法,还是有其他可行的解决方案?

我正在运行Rails 2.3.10 / Ruby 1.8.7

以下是来自environment.rb的代码,以便更好地理解:

Rails::Initializer.run do |config|

# fix for plugins dependent on gems
# see http://blog.joopp.com/2009/01/26/plugin-gem-dependencies-in-your-environmentrb/ 

if $rails_gem_installer
 # We stop the initializer to load the files from the /config/initializers dir. This is to disable the usage of plugins or gems in that code.
 puts 'Disabling the application initializers (rails_gem_installer == true)'
 class Rails::Initializer
   def load_application_initializers; end
 end

 # Next, do _only_ load the needed plugins that are not dependent on gems. For example exception_notification since that one is used in application.rb.
 puts 'Not loading all plugins (rails_gem_installer == true)'
 config.plugins = [:exception_notification]
else
 # Otherwise, when we're just loading the environment.. load everything in the right order. So this is YOUR config.plugins = [something]!
 config.plugins = [:all]
end

[... stuff like config.gem and so on]

1 个答案:

答案 0 :(得分:1)

而不是让rake gems:install正常工作,这在某些情况下似乎永远不会正常工作,而是将依赖项移植到bundler可能更好。虽然这是默认配置Rails 3的方式,但它可以用于任何ruby项目并且做得更好。

Gemfile也可作为人类可读的依赖关系清单,并不总是很容易从Rails配置文件中提取。

在他们的网站上有一个使用捆绑器和Rails 2.3的例子:http://gembundler.com/rails23.html