簡體   English   中英

Rails:在environment.rb中獲取被稱為rake任務的名稱?

[英]Rails: get name of called rake task in environment.rb?

我嘗試為rake任務“ gems:install ”禁用我的environment.rb中的插件自動加載,因為它可能導致無法解決的依賴關系和錯誤(請閱讀http://blog.joopp.com/2009/01/26/plugin -gem-dependencies-your-environmentrb /中的詳細信息)。

盡管實現這種“黑客”,我很快發現變量$ rails_gem_installer,如果寶石應設置為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]

與其走在讓rake gems:install正常工作的道路上(這似乎在每種情況下都無法正常工作),不如將您的依賴項移植到bundler可能更好。 盡管這是默認配置Rails 3的方式,但是它可以在任何ruby項目中使用,並且做得更好。

Gemfile還可以作為依賴項的人類可讀清單,這種東西並不總是很容易從Rails配置文件中提取出來。

在他們的站點上有一個將bundler與Rails 2.3一起使用的示例: http : //gembundler.com/rails23.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM