Skip to content

Instantly share code, notes, and snippets.

@crafterm
Created February 23, 2009 23:30
Show Gist options
  • Save crafterm/69262 to your computer and use it in GitHub Desktop.
Save crafterm/69262 to your computer and use it in GitHub Desktop.
# demo of adding a CSV description to any arbitrary object using the 'comma' gem
#
# http://github.com/crafterm/comma for more info
class Book < ActiveRecord::Base
# ================
# = Associations =
# ================
has_many :pages
has_one :isbn
belongs_to :publisher
# ===============
# = CSV support =
# ===============
comma do
name
description
pages :size => 'Pages'
publisher :name
isbn :number_10 => 'ISBN-10', :number_13 => 'ISBN-13'
url 'Website'
end
end
# ... and if you're using Rails, render supports a :csv modifier
class BooksController < ApplicationController
def index
render :csv => Book.limited(50)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment