Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Beautiful DSL for creating CSV output in Ruby & Rails
Latest commit 6b29483 @paulspringett Bump version to 1.2.0

CSV Shaper

Beautiful DSL for creating CSV output in Ruby & Rails.

Creating CSV files in Ruby is painful! CSV Shaper makes life easier! It's ideal for converting database backed models with attributes into CSV output. It can be used without Rails, but works great with ActiveRecord models and even comes with support for its own template handling.

Build Status Code Climate

Annotated source:

Example Usage

csv_string = CsvShaper.encode do |csv|
  csv.headers :name, :age, :gender, :pet_names

  csv.rows @users do |csv, user|
    csv.cells :name, :age, :gender

    if user.pets.any?
      csv.cell :pet_names


Requires Ruby 1.9+

Install using Rubygems

$ gem install csv_shaper

Or if you want to use it in your Rails app, add the following line to your Gemfile

gem 'csv_shaper'

and then run

$ bundle install


Everything goes inside the encode block, like so

csv_string = CsvShaper::Shaper.encode do |csv|

Usage in Rails 3.0+

When using it in Rails your view template is rendered inside the encode block so you can just call the csv object directly.

In Rails the example at the top of the README would simply be:

csv.headers :name, :age, :gender, :pet_names

csv.rows @users do |csv, user|
  csv.cells :name, :age, :gender

  if user.pets.any?
    csv.cell :pet_names

Create a Rails view, set the content-type to csv and the handler to shaper. For the view of the index action the filename would be:


then just start defining your headers and rows as per the examples.


You must define the headers for your CSV output. This can be done in one of 3 ways.

Standard attribute list

csv.headers :name, :age, :location

This would create headers like so:


Using the attribute names of a Class

Say you have a User ActiveRecord class with attributes of :name, :age, :location. Simply pass the class to the headers method

csv.headers User

Using a block to define headers and custom mappings

csv.headers do |csv|
  csv.columns :name, :age, :location
  csv.mappings name: 'Full name', location: 'Region'

This would create headers like so:

Full name,Age,Region

The mappings are useful for pretty-ing up the names when creating the CSV. When creating cells below you should still use the column names, not the mapping names. eg. :name not 'Full name'

Specify the header inflector method

Sometimes you may wish to control how headers are transformed from the symbol form. The default inflector is set to :humanize.

csv.headers do |csv|
  csv.columns :full_name, :age, :full_address
  csv.inflector :titleize

This would create headers like so:

Full Name,Age,Full Address

Rows & Cells

CSV Shaper allows you to define rows and cells in a variety of ways.

Basic row without a model

csv.row do |csv|
  csv.cell :name, "Joe"
  csv.cell :age, 24

Passing a model and attributes

csv.row @user, :name, :age, :location

This will call the column names (name, age...) on @user and assign them to the correct cells. The output from the above Ruby might look like:

Paul,27,United Kingdom

Passing a model to a block

csv.row @user do |csv, user|
  csv.cells :name, :age
  if user.show_gender?
    csv.cell :gender

  csv.cell :exported_at,

Any calls here to cell without a second argument are called on the model (user), otherwise the second parameter is used as a static value.

The cells method only takes a list of Symbols that are called as methods on the model (user).

The output from the above Ruby might look like:


Multiple Rows

You can pass an Enumerable and a block to csv.rows like so

csv.rows @users do |csv, user|
  csv.cells :name, :age, :location, :gender
  csv.cell :exported_at,

Don't worry about missing cells

There's no need to pad missing cells with nil

This Ruby code will produce the CSV output below

csv.headers :name, :age, :gender

csv.row do |csv|
  csv.cell :name, 'Paul'
  # no age cell
  csv.cell :gender, 'M'

csv.row do |csv|
  csv.cell :name 'Joe'
  csv.cell :age, 34
  # no gender cell

Further Rails integration

Customise the filename of the CSV download by defining a @filename instance variable in your controller action.

respond_to :html, :csv

def index
  @users = User.all
  @filename = "All users - #{}.csv"

CSV configuration

To configure how the CSV output is formatted you can define a configure block, like so:

CsvShaper.configure do |config|
  config.col_sep = "\t"
  config.write_headers = false

Inside the block you can pass any of the standard library CSV DEFAULT_OPTIONS, as well as a write_headers option (default: true). Setting this to false will exclude the headers from the final CSV output.

If you're using Rails you can put this in an initializer.

To configure CSV output locally to change global behavior you can define a configure hash, like so:

CsvShaper.encode(col_sep: "\t") do |csv|


  1. Fork it
  2. Create a semantically named feature branch
  3. Write your feature
  4. Add some tests for it
  5. Commit your changes & push to GitHub (do not change the gem's version number)
  6. Submit a pull request with relevant details
Hat tips

Copyright (c) Paul Springett 2012

Something went wrong with that request. Please try again.