Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 598ea6d37dfdc3656435b7a330981dffcd173e59 Joel Chippindale committed Jun 26, 2008
Showing with 44 additions and 0 deletions.
  1. +3 −0 init.rb
  2. +41 −0 lib/csv_builder.rb
@@ -0,0 +1,3 @@
+require 'csv_builder'
+
+ActionView::Template.register_template_handler 'csvbuilder', ActionView::TemplateHandlers::CsvBuilder
@@ -0,0 +1,41 @@
+require 'fastercsv'
+
+module ActionView # :nodoc:
+ module TemplateHandlers
+ # Template handler for csv templates
+ #
+ # Add rows to your CSV file in the template by pushing arrays of columns into csv
+ #
+ # # First row
+ # csv << [ 'cell 1', 'cell 2' ]
+ # # Second row
+ # csv << [ 'another cell value', 'and another' ]
+ # # etc...
+
+ class CsvBuilder < TemplateHandler
+ include Compilable
+
+ def self.line_offset
+ 7
+ end
+
+
+ def compile(template)
+ <<-EOV
+ unless defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base)
+ @filename ||= "\#{controller.action_name}.csv"
+ controller.response.headers["Content-Type"] ||= 'text/csv'
+ controller.response.headers['Content-Disposition'] = "attachment; filename=\#{@filename}"
+ end
+
+ result = FasterCSV.generate do |csv|
+ #{template.source}
+ end
+
+ result
+ EOV
+ end
+
+ end
+ end
+end

0 comments on commit 598ea6d

Please sign in to comment.