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



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
# File 'lib/decisive/template_handler.rb', line 13

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 @stream
        begin
          context.each do |row|
            response.stream.write row.to_csv(force_quotes: true)
          end
        ensure
          response.stream.close
        end
        ""
      else
        context.to_csv(force_quotes: true)
      end

    else
      response.headers["Content-Type"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
      context.to_xls
    end
  RUBY
end

.registerObject



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

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