Search code examples
ruby-on-railsruby-on-rails-3.1sprocketsasset-pipeline

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


Solution

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