Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to temporarily change output path when precompiling assets, Sprockets / Rails assets pipeline, 3.1.0

I'm trying to update this code to work with the released Rails 3.1.0:

  # temporarily set the static assets location from public/assets to our spec directory
  ::Rails.application.assets.static_root = Rails.root.join("spec/javascripts/generated/assets")

  ::Rake.application['assets:clean'].invoke
  ::Rake.application['assets:precompile'].invoke

Now that Sprockets::Environment#static_root has been removed, what's the best way temporarily change the sprockets output directory?

Edit: Also I'd like to be able to clean the assets in my custom output directory :)

like image 216
Gabe Kopley Avatar asked Aug 31 '11 17:08

Gabe Kopley


People also ask

How do you Precompile rails assets?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What does rake assets Clean do?

rake assets:clean Only removes old assets (keeps the most recent 3 copies) from public/assets . Useful when doing rolling deploys that may still be serving old assets while the new ones are being compiled.


1 Answers

You can use config.assets.prefix, but this will still put the assets in the public directory (see here for the rake task, which joins the public_path and the prefix).

In your case, this should work:

Rails.application.config.assets.prefix = "../spec/javascripts/generated/assets"
Rails.application.config.assets.manifest = File.join(Rails.public_path, config.assets.prefix)

I had to specify the manifest path because of the weird load order of the sprockets railtie. Without doing it, it gets stuck at public/assets, which doesn't exist and blows up the rake task. YMMV.

Side note: I tried this in the development environment at first, but the config.assets.prefix refused to change. I suspect putting config.assets.enabled to true would have fixed this, but I haven't got around to testing it yet.

As a bonus, the assets:clean works perfectly with this solution (you can see it for yourself in the rake task)

like image 148
Benoit Garret Avatar answered Sep 21 '22 11:09

Benoit Garret