如何在执行Rails资产时运行SOME初始化器:预编译? [英] How can I run SOME initializers when doing a Rails assets:precompile?

查看:111
本文介绍了如何在执行Rails资产时运行SOME初始化器:预编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景



我有一个应用程序,我最近更新到Rails 3.2.1(来自Rails 3.0.x),并重构了JS和CSS资源以供使用新的资产管道。该应用程序在Celaon Cedar堆栈上托管在Heroku上。

应用程序配置



一个名为app_config.yml的YAML文件,并使用初始化程序将其加载到全局APP_CONFIG变量中:

 #config / initializers / load_app_config。 rb 

app_config_contents = YAML.load_file(#{Rails.root.to_s} /config/app_config.yml)
app_config_contents [default] || = {}
APP_CONFIG = app_config_contents [default]。merge(
app_config_contents [Rails.env] || {}).symbolize_keys



Heroku资产编译



Heroku支持Cedar堆栈中内置的Rails资产管道。当您将应用程序推送到Heroku时,它会自动在服务器上调用 rake资产:预编译作为部署过程中的一个步骤。但是,它在没有数据库访问或正常ENV变量的沙盒环境中执行此操作。



如果允许应用程序在资产预编译期间正常初始化,则会尝试连接到数据库。这可以通过将以下内容添加到application.rb文件中轻松解决:

 #预编译资源时不要加载整个应用程序
config.assets.initialize_on_precompile = false




我的问题



当设置 initialize_on_precompile = false 时,运行code> config / initializers / * 。我遇到的问题是我需要APP_CONFIG变量在资产预编译期间可用。



如何在资产编译期间加载 load_app_config.rb 而无需初始化整个应用程序?我可以使用传递给Rails :: Application.initialize的 group 参数做些什么! ?

解决方案

Rails允许您仅在特定组中注册初始值设定项,但您需要使用Railtie API:

 #在config / application.rb中

模块AssetsInitializers
class Railtie< Rails :: Railtie
初始化器assets_initializers.initialize_rails,
:group => :assets do | app |
需要#{Rails.root} /config/initializers/load_config.rb
end
end
end

您不需要检查AppConfig是否已定义,因为它只能在资产组中运行。



你可以(也应该)继续使用 initialize_on_precompile = false 。当初始化应用程序时(因为它在 config / initializers 中时,load_config.rb初始化器将在未进行初始化的情况下进行预编译(因为上面的代码)。


Background

I have an app that I recently updated to Rails 3.2.1 (from Rails 3.0.x) and have refactored the JS and CSS assets to make use of the new asset pipeline. The app is hosted on Heroku with the Celadon Cedar stack.

App Config

I keep application specific configuration in a YAML file called app_config.yml and load it into a global APP_CONFIG variable using an initializer:

# config/initializers/load_app_config.rb

app_config_contents = YAML.load_file("#{Rails.root.to_s}/config/app_config.yml")
app_config_contents["default"] ||= {}
APP_CONFIG = app_config_contents["default"].merge(
                       app_config_contents[Rails.env] || {} ).symbolize_keys

Asset Compilation on Heroku

Heroku has support for the Rails asset pipeline built into the Cedar stack. When you push an app to Heroku it automatically calls rake assets:precompile on the server as a step in the deploy process. However it does this in a sandboxed environment without database access or normal ENV vars.

If the application is allowed to initialize normally during asset precompilation an error is thrown trying to connect to the database. This is easily solved by adding the following to the application.rb file:

    # Do not load entire app when precompiling assets
    config.assets.initialize_on_precompile = false


My Problem

When initialize_on_precompile = false is set, none of the initializers in config/initializers/* are run. The problem I am running into is that I need the APP_CONFIG variable to be available during asset precompilation.

How can I get load_app_config.rb to be loaded during asset compilation without initializing the entire app? Can I do something with the group parameter passed to Rails::Application.initialize! ?

解决方案

Rails lets you register initializers only in certain groups, but you need to use the Railtie API:

# in config/application.rb

module AssetsInitializers
  class Railtie < Rails::Railtie
    initializer "assets_initializers.initialize_rails",
                :group => :assets do |app|
      require "#{Rails.root}/config/initializers/load_config.rb"
    end
  end
end

You don't need to check if AppConfig is defined since this will only run in the assets group.

And you can (and should) continue to use initialize_on_precompile = false. The load_config.rb initializer will be run when initializing the app (since it's in config/initializers) and when pre-compiling without initializing (because of the above code).

这篇关于如何在执行Rails资产时运行SOME初始化器:预编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆