I think you'll find this simpler and more flexible. First, you'll
need to download the FasterCSV gem ('gem install fastercsv'). Then
you can use:
class FasterCSVExport
require 'fastercsv'
def create_report( field_names = , data_rows = )
FasterCSV.generate do |csv|
csv << field_names.map {|fn| fn.humanize.titleize }
data_rows.each {|row| csv << row }
end
end
end
I've given it to you as a stand-alone class but the create_report
method can also be added to each model directly or as part of a module
that gets mixed in (which is how I do it). Then in your controller
you can just have:
field_names = ['id', 'firstname', 'lastname']
table_rows = YourModel.find(:all).collect {|item| [item.id,
item.firstname, item.lastname]
export_data = FasterCSVExport.create_report(field_names, table_rows)
send_data(export_data, :type => "text/csv; charset=utf-8;
header=present",
:disposition => 'attachment', :filename => "exported_data.csv")
Even easier, you can pass your field_names to the "select" option when
performing the find to quickly have both the table header and the
table rows contain the same attributes. Something like:
field_names = ['id', 'firstname', 'lastname']
table_rows = YourModel.find(:all, :select => field_names)
export_data = FasterCSVExport.create_report(field_names, table_rows)
send_data(export_data, :type => "text/csv; charset=utf-8;
header=present",
:disposition => 'attachment', :filename => "exported_data.csv")
Or if you opt to put the 'create_report' method into your model
directly, you can push the find to the model too and let it handle
both steps at once.
export_data = YourModel.create_report(['id', 'firstname', 'lastname'])
send_data(export_data, :type => "text/csv; charset=utf-8;
header=present",
:disposition => 'attachment', :filename => "exported_data.csv")
From there you can start to add bells and whistles. Tim, something
like:
field_names = YourModel.column_names if field_names.blank?
might be what you want to grab all the column names.
(I'm typing this from scratch without testing it so it's quite
possible I've typo'd something...)
HTH,
Kevin Skoglund
http://www.nullislove.com