Class: CsvBuilder::TemplateHandler
- Inherits:
-
ActionView::Template::Handler
- Object
- ActionView::Template::Handler
- CsvBuilder::TemplateHandler
- Includes:
- ActionView::Template::Handlers::Compilable
- Defined in:
- lib/csv_builder/template_handler.rb
Overview
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...
You can set the default filename for that a browser will use for ‘save as’ by setting @filename
instance variable in your controller’s action method e.g.
@filename = 'report.csv'
You can also set the input encoding and output encoding by setting @input_encoding
and @output_encoding
instance variables. These default to ‘UTF-8’ and ‘LATIN1’ respectively. e.g.
@output_encoding = 'UTF-8'
Instance Method Summary collapse
Instance Method Details
#compile(template) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/csv_builder/template_handler.rb', line 29 def compile(template) <<-EOV begin output = CsvBuilder::CSV_LIB.generate(@csv_options || {}) do |faster_csv| csv = CsvBuilder::TransliteratingFilter.new(faster_csv, @input_encoding || 'UTF-8', @output_encoding || 'LATIN1') #{template.source} end unless defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base) @filename ||= "\#{controller.action_name}.csv" if controller.request.env['HTTP_USER_AGENT'] =~ /msie/i controller.response.headers['Pragma'] = 'public' controller.response.headers["Content-type"] = "text/plain" controller.response.headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0' controller.response.headers['Content-Disposition'] = "attachment; filename=\#{@filename}" controller.response.headers['Expires'] = "0" else controller.response.headers["Content-Type"] ||= 'text/csv' controller.response.headers["Content-Disposition"] = "attachment; filename=\#{@filename}" controller.response.headers["Content-Transfer-Encoding"] = "binary" end end output rescue Exception => e Rails.logger.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV") raise e end EOV end |