Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I run SOME initializers when doing a Rails assets:precompile?
    text
    copied!<h1>Background</h1> <p>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.</p> <h3>App Config</h3> <p>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:</p> <pre><code># 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 </code></pre> <h3>Asset Compilation on Heroku</h3> <p>Heroku has support for the Rails asset pipeline built into the Cedar stack. When you push an app to Heroku it automatically calls <code>rake assets:precompile</code> 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.</p> <p>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:</p> <pre><code> # Do not load entire app when precompiling assets config.assets.initialize_on_precompile = false </code></pre> <p><br/></p> <h1>My Problem</h1> <p>When <code>initialize_on_precompile = false</code> is set, none of the initializers in <code>config/initializers/*</code> are run. The problem I am running into is that I need the APP_CONFIG variable to be available during asset precompilation. </p> <p>How can I get <code>load_app_config.rb</code> to be loaded during asset compilation without initializing the entire app? Can I do something with the <code>group</code> parameter passed to Rails::Application.initialize! ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload