Class: Exportable::ExportMethods::XlsExporter::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/exportable/export_methods/xls_exporter.rb

Overview

Exporter class for XLS Exporter

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Exporter

Returns a new instance of Exporter.



17
18
19
20
# File 'lib/exportable/export_methods/xls_exporter.rb', line 17

def initialize(model)
  @book = Spreadsheet::Workbook.new
  @model = model
end

Instance Method Details

#export(options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/exportable/export_methods/xls_exporter.rb', line 22

def export(options)
  sheet = @book.create_worksheet
  i = 0
  if options[:header]
    sheet.row(i).concat(options[:fields].map(&:to_s))
    i += 1
  end
  @model.where(nil).find_each do |record|
    sheet.row(i).concat options[:fields].map { |attr| record.send(attr).to_s }
    i += 1
  end
  write_io_output
end

#write_io_outputObject



36
37
38
39
40
# File 'lib/exportable/export_methods/xls_exporter.rb', line 36

def write_io_output
  output = StringIO.new
  @book.write output
  output.string
end