Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add README.
- Loading branch information
1 parent
62c1ccc
commit 5334396
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
= 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(1) | ||
@thing = @thing.as_json | ||
@thing[:other_things][:name] = thing.other_thing.name | ||
@thing[:other_related_thing][:name] = thing.other_related_thing.name | ||
|
||
respond_with(@thing) | ||
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(1) | ||
|
||
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. | ||
|
||
module OtherThingFilter | ||
include CerealBox | ||
|
||
def attributes(base) | ||
{ :name => base.other_thing.name } | ||
end | ||
end | ||
|
||
== 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}[mailto:cmeiklejohn@swipely.com] from {Swipely, Inc.}[http://www.swipely.com]. |