If you look around this site you'll notice that I'm hosting all my code snippets on GitHub's awesome gist feature. While this allows me to not maintain syntax highlighter plugins and libraries, as well as lets me change the code snippet without updating the blog is nothing short of awesome. The only thing that kind of sucks is that I/we are losing the ability to have our code be search engine friendly since a gist is a 1-line javascript include. This sucks not so much from the "index me!" perspective, but more of the "if I google a line of code/error, will I find relevant code?"

To tackle this, I ended up with a small project: given a gist ID, generate the embed code along with NOSCRIPT tags so the content is indexable. This will only solve the initial problem, as updates will not propagate to the blog, but... you can always manually update.

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

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