【问题标题】:Rails: get name of called rake task in environment.rb?Rails:获取 environment.rb 中被调用 rake 任务的名称?
【发布时间】:2011-05-08 12:32:48
【问题描述】:

我尝试在我的 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]

【问题讨论】:

  • 我不确定您要做什么。但是如果你需要像这样破解默认的 Rails 行为,你可能做错了。
  • 如果您曾经遇到过插件(供应商/插件)依赖于 gem 的情况,您可能会理解这个问题以及为什么我需要使用 capistrano 部署该解决方法

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


【解决方案1】:

与其走上让rake gems:install 正常工作的道路,这似乎在任何情况下都无法正常工作,不如将你的依赖项移植到bundler 上。尽管这是 Rails 3 的默认配置方式,但它可以用于任何 ruby​​ 项目并且做得更好。

Gemfile 还用作人类可读的依赖项清单,这并不总是容易从 Rails 配置文件中提取。

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

【讨论】:

  • 谢谢 - 我们正在定义向 Rails 3 atm 的迁移路径,也许我们应该首先迁移到 Bundler...
  • 我们现在已成功迁移到 Bundler :) 再次感谢您朝着正确的方向推进
猜你喜欢
  • 2020-09-07
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
  • 2011-12-03
  • 2016-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多