Simple APIs using SerializeWithOptions

Viget Labs rolls out a Rails plugin to cleanup your API’s code.

Instead of doing this in the controller:

@speaker.to_xml(
  :methods => [:average_rating, :avatar_url],
  :except  => [:email, :claim_code],
  :include => {
    :talks => {
      :methods => :average_rating,
      :except  => :creator_id
    }
  }
)

You move it to the class like this:

class Speaker < ActiveRecord::Base
  serialize_with_options do
    methods   :average_rating, :avatar_url
    except    :email,
              :claim_code
    includes  :talks
  end
end

Great way to DRY up your code.

Posted on July 9, 2009
Tagged with: , , , , ,

Leave A Comment

Ajax-loader