Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

24 Commits

Repository files navigation

This simple plugin gives you the ability to call to_csv to a collection of activerecords. The builder options are the same as to_json / to_xml, except for the :include.

@users = User.all## defaults are export headers and all fields#@users.to_csv@users.to_csv(:only=> [:last_name, :role]) # This will also set the order@users.to_csv(:headers=>false)
@users.to_csv(:except=> [:last_name, :role])
@users.to_csv(:methods=>:admin?)
@users.to_csv({:methods=>:admin?}, {:col_sep=>'\t'}) # Send csv generation options

In the controller where you want to export to csv, add the format.csv line (as of rails 2.1 it is not necessary to register the csv myme_type)

classUserController<ApplicationControllerdefindex@users = User.allrespond_todo|format|format.htmlformat.xml { render:xml=>@users }
format.csv { send_data@users.to_csv }
endendend
None if using ruby >= 1.9
Else, install fastercsv:
sudo gem install fastercsv
./script/plugin install git://github.com/arydjmal/to_csv.git

I got ideas and influence from Mike Clarks recipe #35 in Rails Recipes book, some anonymous pastie, and whoever wrote to_xml/to_json builders.

Does not work on a single activerecord, ie, User.first.to_csv.

Copyright © 2008-2011 Ary Djmal, released under the MIT license

About

This Rails plugin gives you the ability to call to_csv to a collection of activerecords. The builder options are the same as to_json / to_xml, except for the :include.

Resources

Stars

94 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages