Fork me on GitHub

Cbuilder

lightweight csv templating

  Cbuilder.encode do |csv|
    csv.set_collection!(@orders) do |order|
      csv.col 'Customer Name',        order.customer.name
      csv.col 'Products Purchased',   products_count(order)
      csv.col 'Product Names',        names(order.products)
    end
  end

Cbuilder is an ultralight (< 100 LOC) CSV builder/template handler inspired by dhh's excellent Jbuilder. The goal is a simple DSL for creating CSV files that's better than creating massive custom arrays.

If you're doing any more than 4-5 columns, generating CSV files can be a real pain. God forbid you're not simply outputting the attributes of a simple array, and need helpers or other niceties.

You don't need to re-invent the wheel with CSV. Cbuilder weighs in at less than 100 LOC.

Usage

Just drop into your Rails Gemfile and start adding templates with *.csv.cbuilder.

If you'd like the CSV to be downloaded as an attachment, add the following to your controller action:

response.headers["Content-Disposition"] =
  'attachment; filename="my_spreadsheet.csv'

Cbuilder was lovingly coded in small batches with an artisanal eye by Craft Coffee.

Take me to the repo!