class CsvBuilder::TemplateHandler

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'

Public Instance Methods

call(template) click to toggle source
# File lib/csv_builder/template_handler.rb, line 30
    def call(template)
      <<-EOV
      begin;
        self.output_buffer = String.new;
        csv = CsvBuilder::CsvProxy.new(self.output_buffer, @csv_builder || {});
        #{template.source};
        if @csv_filename;
          controller.response.headers['Content-Disposition'] = %Q{attachment; filename="\#{@csv_filename}"};
        end;
        self.output_buffer;
      rescue Exception => e;
        Rails.logger.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV");
        raise e;
      end;
      EOV
    end