Rails 4

This is just a new PR containing all the hard work of @zeiv, @gordonbisnor and @aepstein.

It's basically a combination of #193 and #194, with a new nudge to get this merged back upstream :)