Project

cereal_box

0.0
No commit activity in last 3 years
No release in over 3 years
Serialization filters for active record.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

Cereal box.¶ ↑

Serialization filters for active record.

Motivation¶ ↑

Ever have a controller that looks like this?

class ThingController < ActionController::Base
  respond_to :json

  def show
    @thing = Thing.find(params[:id])
    @thing_json = @thing.as_json 
    @thing_json[:other_things][:name] = @thing.other_thing.name
    @thing_json[:other_related_thing][:name] = @thing.other_related_thing.name

    respond_with(@thing_json)
  end
end

Specifically adding additional related information from other models into the serialized hash of an object? Well, no more!

Doesn’t this look better?

class ThingController < ActionController::Base
  respond_to :json

  def show
    @thing = Thing.find(params[:id])

    respond_with(OtherRelatedThingFilter.new(ThingFilter.new(@thing))
  end
end

Filters¶ ↑

Implement a filter¶ ↑

It’s simple! Just define a module that includes cereal_box and implements an attributes method.

class OtherThingFilter
  include CerealBox

  def attributes(base)
    { :name => base.other_thing.name }
  end
end

Filters support as_xml, as_json and serializable_hash.

License¶ ↑

Cereal Box is Copyright © 2011 Christopher Meiklejohn. It is free software, and may be redistributed under the terms specified in the LICENSE file.

About¶ ↑

The cereal_box gem was written by Christopher Meiklejohn from Swipely, Inc..