How can I run SOME initializers when doing a Rails assets:precompile?

前端 未结 5 1832
無奈伤痛
無奈伤痛 2021-02-04 03:55

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. Th

5条回答
  •  鱼传尺愫
    2021-02-04 04:16

    Here's what I came up with. In the assets that need app configuration, I place this line at the very beginning:

    <% require "#{Rails.root}/config/initializers/load_config.rb" unless defined?(AppConfig) %>
    

    ... and add .erb to the file name, so that video_player.js.coffee becomes video_player.js.coffee.erb. Then I can safely use AppConfig['somekey'] afterwards.

    During the asset pre-compilation, it loads app config despite the initialize_on_precompile set to false, and does it only once (which avoids constant redefinition issues).

    Yes, it's a kludge, but many times nicer than embedding configs in asset files.

提交回复
热议问题