Module: RailsExporter::Exporter::ClassMethods

Defined in:
lib/rails_exporter/exporter.rb

Instance Method Summary collapse

Instance Method Details

#export_to_csv(records, context = :default, params: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rails_exporter/exporter.rb', line 16

def export_to_csv(records, context=:default, params: nil)
  custom_params = {col_sep: ';', force_quotes: true}.merge(params || {})
  CSV.generate(custom_params) do |csv|
    # HEADER
    csv << get_columns(context).map do |attr|
      attr_name(attr)
    end
    # BODY
    records.each do |record|
      csv << get_values(record, context)
    end
  end
end

#export_to_xls(records, context = :default, params: nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rails_exporter/exporter.rb', line 51

def export_to_xls(records, context=:default, params: nil)
  #TODO: custom params
  #FILE
  file_contents = StringIO.new
  #CHARSET
  Spreadsheet.client_encoding = 'UTF-8'
  #NEW document/spreadsheet
  document = Spreadsheet::Workbook.new
  spreadsheet = document.create_worksheet
  spreadsheet.name = I18n.t(:spreadsheet_name, default: ['Spreadsheet'], scope: [:exporters])
  #HEADER FORMAT
  spreadsheet.row(0).default_format = Spreadsheet::Format.new :weight => :bold
  #HEADER
  get_columns(context).each_with_index do |attr, i|
    spreadsheet.row(0).insert i, attr_name(attr)
  end
  #ROWS
  records.each_with_index do |record, i|
    values = get_values(record, context)
    spreadsheet.row(i+1).push(*values)
  end
  #SAVE spreadsheet
  document.write file_contents
  #RETURN STRING
  file_contents.string.force_encoding('binary')
end

#export_to_xlsx(records, context = :default, params: nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rails_exporter/exporter.rb', line 78

def export_to_xlsx(records, context=:default, params: nil)
  #TODO: custom params
  #NEW document/spreadsheet
  workbook = RubyXL::Workbook.new
  worksheet = workbook[0]
  # worksheet = workbook.add_worksheet(I18n.t(:spreadsheet_name, default: ['Spreadsheet'], scope: [:exporters]))
  worksheet.sheet_name = I18n.t(:spreadsheet_name, default: ['Spreadsheet'], scope: [:exporters])
  #HEADER FORMAT
  worksheet.change_row_bold(0, true)
  #HEADER (ROW=0)
  get_columns(context).each_with_index do |attr, i|
    worksheet.add_cell(0, i, attr_name(attr))
  end
  #ROWS
  records.each_with_index do |record, row_index|
    values = get_values(record, context)
    values.each_with_index {|value, col_index| worksheet.add_cell(row_index+1, col_index, value)}
  end
  #RETURN STRING
  workbook.stream.string.force_encoding('binary')
end

#export_to_xml(records, context = :default, params: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails_exporter/exporter.rb', line 30

def export_to_xml(records, context=:default, params: nil)
  #TODO: custom params
  #File XML
  xml = Builder::XmlMarkup.new(indent: 2)
  #Format
  xml.instruct! :xml, :encoding => "UTF-8"
  xml.records do
    #Records
    records.each do |record|
      get_values = get_values(record, context)
      xml.record do |r|
        i = 0
        get_columns(context).map do |attr|
          xml.tag!(attr[:column], get_values[i], {title: attr_name(attr)})
          i+=1
        end
      end
    end
  end
end