Module: XlsxtreamRails::Utils

Defined in:
lib/xlsxtream_rails/utils.rb

Class Method Summary collapse

Class Method Details

.i18n_attr(instance, attr) ⇒ Object



30
31
32
33
# File 'lib/xlsxtream_rails/utils.rb', line 30

def self.i18n_attr(instance, attr)
  attr = attr.is_a?(Symbol) ? attr : attr.to_sym
  instance.class.human_attribute_name(attr)
end

.write_xls(xlsx, data) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/xlsxtream_rails/utils.rb', line 3

def self.write_xls(xlsx, data)
  headers = []
  needs_headers = true
  needs_column_types = false
  xlsx.write_worksheet(auto_format: false) do |sheet|
    data.find_each do |instance, index|
      row_data = []
      instance_cols = instance.xlsx_columns
      instance_cols.each_with_index do |x, i|
        if x.is_a?(Array)
          headers.push(i18n_attr(instance, x[0])) if needs_headers
          row_data.push(x[1].is_a?(Symbol) ? instance.send(x[1]) : x[1])
          if needs_column_types
            column_types[i] = x[2]
          end
        else
          headers.push(i18n_attr(instance, x)) if needs_headers
          row_data.push(x.is_a?(Symbol) ? instance.send(x) : x)
        end
      end
      sheet.add_row headers if needs_headers
      needs_headers = false
      sheet.add_row row_data
    end
  end
end