Friday, February 27, 2009

Caesar: growing up

I've been working on Caesar for a little bit now and can say two things:
  1. Live Queries kick ass
  2. Rails routing is very well designed (only one minor quibble so far)
First the Rails routing: it's easy to extend and very well thought out. I'm really surprised at how little work I've had to do to implement Live Query handling. What I've got so far is basically a second version of Rails' default route - with url helpers for all models.

:controller(:send_back)

That's the route used by all Live Queries. There is an optional :qualifiers parameter, but it defaults to 'id/:id'. You can pass a :path_prefix as you would with a normal Rails route (this is actually how Rome does it).

The really interesting part is all the extra stuff that happens when that route get created. . .

First, a whole slew of url helper methods get defined. Caesar (like web_home and Rome) is built around the CRUD pattern. The following default named routes are created:

  • create POST :controller(:send_back) { :action => :create, :qualifiers => 'id/:id' }
  • read GET :controller(:send_back) { :action => :read, :qualifiers => 'id/:id' }
  • update PUT :controller(:send_back) { :action => :update, :qualifiers => 'id/:id' }
  • delete DELETE :controller(:send_back) { :action => :delete, :qualifiers => 'id/:id' }
Just like all Rails named routes, all these get _url and _path methods created. I didn't want to stop there though. . . after all, the great thing about Rails is all that sugar they pour everywhere! To keep with convention, Caesar also creates helper methods for all four actions (create, read, update, and delete) for every model you have defined. These methods simply delegate to the generic helpers after populating the :controller and :action parameters.

The next step is to add a default value for :send_back. This means that if the :send_back parameter is not passed to the helpers, it'll default to returning all properties for the specified model. This means that you'll have three options when using Caesar's create url helpers:
  1. create :controller => :photos, :send_back => ['thumbnail', 'name']
  2. create_photos_url :send_back => ['thumbnail', 'name']
  3. create_photos_url
And you'll have even more options for the read, update, and delete helpers since you could pass in either an :id or :qualifiers parameter. As a final layer of sweetness, I'm going to add both singular and plural versions of the url helpers. Some may call this overkill - and perhaps it is - but my thought is that it would at least provide a clear separation between single and batch invocations.

Caesar's coming along nicely. Once I get the sugar slathered on I plan on implementing the God Controller. With the way Live Queries work, I'm thinking I should be ale to have one Controller that handles all requests. . . I get chills thinking about how cool that would be :-)

Oh, yeah, my one quibble with Rails routing: NamedRouteCollection would be a LOT easier to subclass if it's private methods were protected. I plan on submitting a ticket for this and patching it. Other than that, Rails routing has been a lot of fun!

No comments:

Post a Comment