Jim Neath

Manchester based Ruby on Rails & Facebook App Developer

While the Asset Pipeline introduced in Rails 3.1 is awesome, it can be slow if you are precompiling on deploy time. To get around this, you can compile your assets on your local machine and commit them to your repo.

To automate the process, I wrote a small pre-commitGit hook that runs rake assets:precompile:all if any assets have been changed then adds the results to the current commit.

To use simply copy the code below into .git/hooks/pre-commit inside of your Rails application.

#!/bin/bash

# source rvm and .rvmrc if present
[ -s "$HOME/.rvm/scripts/rvm" ] && . "$HOME/.rvm/scripts/rvm"
[ -s "$PWD/.rvmrc" ] && . "$PWD/.rvmrc"
 
# precompile assets if any have been updated
if git diff-index --name-only HEAD | egrep '^app/assets' >/dev/null ; then
  echo 'Precompiling assets...'
  rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
  git add public/assets/*
fi

Update: As mentioned by jrochkind in the comments, you will probably want to add the following to your config/environments/development.rb:

config.assets.prefix = '/assets_dev'

This will prevent your local application from serving the static assets from public/assets/ when running in development.

blog comments powered by Disqus

I am available for freelance work! Click here to email me.

Jim Neath is a Freelance Ruby on Rails & Facebook app developer from Manchester, UK, currently working for Engine Yard.