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

前端 未结 5 1831
無奈伤痛
無奈伤痛 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:05

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

提交回复
热议问题