02 03 2008

make_resourceful is awesome!

I have started using make_resourceful to DRY up my controller code. If you use the standard RESTful controllers, make_resourceful can reduce your controller to this:
class AccountsController < ApplicationController
  make_resourceful do
    actions :all
  end
end
That’s pretty sweet. Of course often a controller might need to be modified slightly, for example I needed to do some set up work to display a google map for a resource (using the ym4r_gm plugin). To set up the @map variable before rendering the view:
make_resourceful do
  actions :all
  before :show do
    # Set up the @map variable here
  end
end
I then needed to access the names of some related models, rather than just their id’s, and wanted to keep the generated SQL statements efficient. Turns out that’s easy too, Just override the current_objects (for index), or current_object (for show) methods in your controller like so (best to have them as private methods):
private
def current_objects
  @current_objects ||= Location.find(:all, :include => :related_object)
end

Note that ||= is used instead of = in case the object has already been cached.

Depending on how much customisation is required, it might turn out that using the standard REST controller is actually more efficient, but for a lot of the projects that I am working on that is not the case, and using make_resourceful makes my life easier :)

Back to Articles