If you're a rails developer you've probably tackled forms which update multiple models and have their own custom validations and/or business logic involved. I'm talking about forms like Signup which may need to create a few other models after the User has successfully been saved.

I've recently had to build a signup form which would create or organization, then create a new postgres schema for the organization, then build the administrative user model and also capture payment. Doing this with simple after_create calls would most likely wreak havoc later in life so I've split this out into a separate Signup model.

Continue Reading

This one is a pretty specific hack. I've had the need to build mini reporting apps, using Sinatra, that aggregate data from multiple databases and produce a report. This is all fine since ActiveRecord is awesome, but I've always missed AR's query caching ability.

After some digging I found that query caching is only enabled on AR's Base connection and all other models must use that awkward Model.cache{} construct -- which is annoying to use. I've hacked up a solution that works, although it relies on :send and instancevariableset which isn't very elegant or solid.

For what it's worth, here is a "simple" way to get Sinatra to use ActiveRecord's native query caching for more than 1 database:

Continue Reading

I use contentfor a lot in Rails code, but a large percentage of my projects are done as a simple Sinatra app, so I find myself missing a lot of features that Rails has. One big one was contentfor -- so I drafted this up to fill the void.

It is similar to how rails 2.x implements it, so in your main layout you can check if that content is there and if so display it, otherwise you can use defaults, or ignore it. This particular gist uses it to create custom sidebars based on what page you are on: if you are logged in, in a particular area, etc.. I like this approach as opposed to having a single sidebar partial with tons of display logic, I just think it keeps thing tidy.

Continue Reading

I was creating a project the other day that used anonymous pages for users and I wanted to make it a little less "guessable" but more human friendly than a SHA1 hash, so I decided to make a small home-brew URL shortener similar in format to Tiny URL.

The neat thing about ruby is that when you turn a Fixnum to a String with tos you can specify the base, and when you turn a String back to a Fixnum you can also specify a base in toi. This lends itself perfectly to turning (large) IDs into a shortened URL thats easier to remember.

In the first gist you can see how this would work with a model to make simple url shortening in rails -- or, you can use randomized values to generate more user friendly "unique" tokens (as opposed to using SHA1 hashes for user confirmation/password reset pages).

Continue Reading

Sinatra is awesome, but Rails has a lot of useful features. One of those is Active Record's Query Caching. I never realized how useful this is until I started to optimize Sinatra apps and felt its absence. Here is a simple way to enable active record query caching in Sinatra.

Continue Reading

I was recently overhauling my permissions for a big rails app and needed to see which filters were on which controllers as an overview. I came up with this little snippet which will go through each of your controllers and print out which filters are on them, as well as their options.

Continue Reading