What I discovered recently when doing a new Rails 3 app was that the RESTful CRUD stuff is all boilerplate. This is true about Sinatra apps, too: Every GET, POST, PUT and DELETE are just wrappers around find, create update and destroy. I used Rabl for generating JSON, so it was possible to write a single serialization for all objects. So why not just boil everything down to the minimal code required?
Something like this:
handle '/blog'
That's it. This makes some assumptions:* That /blog maps to a class Blog which it can load and save. * That you either use "fat models" (with callbacks that munge data and perform actions when data changes) or external observers (like ActiveRecord's observers). * That the model supports all four CRUD actions.
When you want additional verbs:
handle 'GET /blog/search' do
MySearchEngine.search(params[:q])
end
Or object-specific ones: handle 'GET /blog/:id/search' do |blog|
MySearchEngine.search(params[:q], :blog_id => blog.id)
end
In fact, aside from extra verbs, you could get away with no code at all, since you could introspect the database layer to discover which classes are mappable.My app is a JavaScript-based single-page HTML5 web application using Backbone, so there is almost no server-side frontend code, meaning my app is basically a server-side database anyway.