evome/biceps — pull request #2 by dmathieu (2011-10-13 15:23:27+00:00)
Title: Add versioning on the models serializer

Versioning routing is cool. But it's not enough !
So we've given a thought at model serializers versioning.

The idea is quite simple. For a model User :

class User < ActiveRecord::Base
end

You can add serializers, one per version. Serializers need a specific namespace : `Serializers::ModelName::Vx` where `ModelName` is your class's name, and `x` is the API version number.

So, for example, I could do :

class Serializers::Users::V1 < Biceps::Serializer
def as_json(opts={})
{:fullname => "#{parent.firstname} #{parent.lastname}"}
end
end

With this, when someone requests your API at it's V1 to get a user, it will receive the following json :

{"fullname":"John Doe"}

For this to properly work, you need to include the biceps' responder into your application's responders. You can do it the following way :

class ApplicationController < ActionController::Base
def self.responder
MyApp::Responder
end
end

class MyApp:Responder < ActionController::Responder
include Biceps::ModelVersioning
end

For now, we don't include automatically the responder anywhere.
But it might change someday so it gets easier to setup.