Compiling non-digest assets with asset_sync gem in Rails 4.0


In Rails 4, non-digest asset compilation has been disabled. If you ask me, I think this decision make Rails too opinionated.

For some of my applications (The Feedback Button) there is a strong need to compile non-digest versions of assets, as they are referenced outside of the application. Digest version of the assets have a hash of the content in the name. There is no easy way to figure out the proper name of the compiled assets, so there is a strong need to also compile the non-digest version (without the MD5 in the name) of the asset.

There are some monkey-patches or rake tasks available, but they all fail to address the issue when having to put your assets on the Amazaon S3 with CloudFront using the asset_sync gem.

Luckily there is a gem to fix this, and the name of this hero is non-stupid-digest-assets. Simply by adding this gem, you are no longer required to monkey-patch or create a rake task. Just one line in your Gemfile. Beautiful!

There is actually one more thing you need to do, and that is to set your manifest option to false like so:


AssetSync.configure do |config|
config.manifest = false
end

This is to prevent the asset_sync gem from reading the manifest file, which would cause to exclude non-digest assets. This line will prevent the upload from ignoring the assets we actually want to upload.

So, the fix actually took two lines of code in the Gemfile and in config/initilizers/asset_sync.rb.

Also as an extension, I also added an option to overwrite existing files because the default setup ignores the files if they are already present in the bucket (this works on initial upload, but you want your code to stay updated!).


config.existing_remote_files = 'ignore'

The final initialiser should look like this: