Verbose Logging

software development with some really amazing hair

T + G I F R

Easy Sinatra Metal For Rails 3.x

· · Posted in Programming
Tagged with

I was doing some optimizing the other day on a Rails 2.3.x application with a metal controller. I didn't feel like writing a straight rack application, so I used sinatra. A pretty good explanation of how this works can be seen in Adam Wiggins' slides:

Rails Metal, Rack, and Sinatra from Adam Wiggins

In Rails 3, they still have metal controllers, in that the ActionController::Metal class is essentially a really thin controller, and you can use it like any controller in Rails. They took out the way Rails 2.x did metal on the grounds that you should just use rack middleware.

This was all good, except Sinatra is super nice for writing quick handlers for things, and the Rails 2.x way of doing it was pretty slick. The metal was just a rack application that was called before the regular application, and if it returned a 404, the request continued on to the main rails application.

I wanted this same technique for Rails 3.x stuff, since that's how I could nicely integrate sinatra-bundles into Rails 3.x1

Anyway, it's stupid simple, but here it is.

Drop that in your lib directory. You can make a class, inherit from Rack::Sinatra, doll it up like any Sinatra application, and the use that as rack middleware. Now it basically works just like using Sinatra for metal in Rails 2.x.

  1. WIth Rails 2.x, you can just drop in a metal controller in the app/metal directory.