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 :)
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With