Class: CsvBuilder::TemplateHandler

Inherits:
ActionView::Template::Handler
  • Object
show all
Includes:
ActionView::Template::Handlers::Compilable
Defined in:
lib/csv_builder/template_handler.rb,
lib/csv_builder/template_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(template, source = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/csv_builder/template_handler.rb', line 91

def self.call(template, source = nil)
  source ||= template.source

  <<-EOV
  begin

    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
        response.headers['Pragma'] = 'must-revalidate'
        response.headers["Content-type"] = "text/plain"
        response.headers['Cache-Control'] = 'must-revalidate, post-check=0, pre-check=0'
        response.headers['Content-Disposition'] = "attachment; filename=\\"\#{@filename}\\""
        response.headers['Expires'] = "0"
      else
        response.headers["Content-Type"] ||= 'text/csv'
        response.headers["Content-Disposition"] = "attachment; filename=\\"\#{@filename}\\""
        response.headers["Content-Transfer-Encoding"] = "binary"
      end
    end

    if @streaming
      template = Proc.new {|csv|
        #{source}
      }
      CsvBuilder::Streamer.new(template)
    else
      output = CsvBuilder::CSV_LIB.generate(**(@csv_options || {})) do |faster_csv|
        csv = CsvBuilder::TransliteratingFilter.new(faster_csv, @input_encoding || 'UTF-8', @output_encoding || 'ISO-8859-1')
        #{source}
      end
      output
    end
  rescue Exception => e
    Rails.logger.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV")
    raise e
  end
  EOV
end

Instance Method Details

#compile(template) ⇒ Object



131
132
133
# File 'lib/csv_builder/template_handler.rb', line 131

def compile(template)
  self.class.call(template)
end