Precompile Assets From Rails 3.1 Engines

Today I finally finished the rails 3.1 upgrade for our open source CMS Alchemy.

An issue I ran into was, that while deploying, the assets from inside the engine weren’t precompiled.

I solved it by telling the main app to also precompile Alchemys assets:

# lib/alchemy/engine.rb
module Alchemy
  class Engine < Rails::Engine

    # Config defaults
    config.mount_at = '/'

    # Enabling assets precompiling under rails 3.1
    if Rails.version >= '3.1'
      initializer :assets do |config|
        Rails.application.config.assets.precompile += %w( alchemy/alchemy.js alchemy/alchemy.css alchemy/print.css )
      end
    end

    # Check the gem config
    initializer "check config" do |app|
      # make sure mount_at ends with trailing slash
      config.mount_at += '/'  unless config.mount_at.last == '/'
    end

    initializer :flash_cookie do |config|
      config.middleware.insert_after(
        'ActionDispatch::Cookies',
        Alchemy::Middleware::FlashSessionCookie,
        ::Rails.configuration.session_options[:key]
      )
    end

  end
end

The important part is:

Rails.application.config.assets.precompile += %w( alchemy/alchemy.js alchemy/alchemy.css alchemy/print.css )

Be sure to put it inside an

initializer

block. So the main app gets loaded before changing the setting.

As Argument you should pass an array of all the assets you want to be precompiled together with the main app ones.

 

Dieser Eintrag wurde veröffentlicht in Webdevelopment und getagged , , , , . Bookmarken: Permanent-Link. Kommentieren oder ein Trackback hinterlassen: Trackback-URL.

2 Kommentare

  1. Stanislaw
    Erstellt am 18. Dezember 2011 um 01:28 | Permanent-Link

    Thanks!

  2. Carlos
    Erstellt am 23. Februar 2012 um 10:41 | Permanent-Link

    +1, thanks for sharing!
    I was suspecting something easy like that could be done in the engine.rb .

Ihr Kommentar

Ihre E-Mail wird niemals veröffentlicht oder verteilt. Benötigte Felder sind mit * markiert

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Yeah! I am cool.
I develop OpenSource Software ;)

Follow me on:

GitHub Logo