Class: Decisive::TemplateHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/decisive/template_handler.rb

Class Method Summary collapse

Class Method Details

.call(template, source = template.source) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/decisive/template_handler.rb', line 12

def self.call template, source=template.source
  <<~RUBY
    extend Decisive::DSL
    context = (#{source})

    response.headers["Content-Transfer-Encoding"] = "binary"
    response.headers["Content-Disposition"] = %(attachment; filename="\#{context.filename}")

    if context.csv?
      response.headers["Content-Type"] = "text/csv"

      if controller.is_a?(ActionController::Live)
        begin
          context.each do |row|
            response.stream.write row.to_csv(force_quotes: true)
          end
          raise if Rails.env.test? # WTF WTF without this the stream isn't closed in test mode??? WTF WTF
        ensure
          response.stream.close
        end
        ""
      else
        context.to_csv(force_quotes: true)
      end

    else
      response.headers["Content-Type"] = "application/vnd.ms-excel"
      context.to_xls
    end
  RUBY
end

.registerObject



8
9
10
# File 'lib/decisive/template_handler.rb', line 8

def self.register
  ActionView::Template.register_template_handler 'decisive', self
end