Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cmeiklejohn/cereal_box
Serialization filters for active record.
https://github.com/cmeiklejohn/cereal_box
Last synced: 2 months ago
JSON representation
Serialization filters for active record.
- Host: GitHub
- URL: https://github.com/cmeiklejohn/cereal_box
- Owner: cmeiklejohn
- License: mit
- Created: 2011-10-14T00:46:42.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2011-10-26T15:30:41.000Z (about 13 years ago)
- Last Synced: 2024-10-06T17:01:28.667Z (3 months ago)
- Language: Ruby
- Homepage: http://github.com/cmeiklejohn/cereal_box
- Size: 101 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.rdoc
- License: LICENSE
Awesome Lists containing this project
README
= Cereal box.
Serialization filters for active record.
== Motivation
Ever have a controller that looks like this?
class ThingController < ActionController::Base
respond_to :jsondef 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.namerespond_with(@thing_json)
end
endSpecifically 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 :jsondef 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 CerealBoxdef attributes(base)
{ :name => base.other_thing.name }
end
endFilters 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}[mailto:[email protected]] from {Swipely, Inc.}[http://www.swipely.com].