Module: Axlsx::Ar::SingletonMethods

Defined in:
lib/acts_as_xlsx/ar.rb

Overview

Singleton methods for the mixin

Instance Method Summary collapse

Instance Method Details

#to_xlsx(options = {}) ⇒ Object

Maps the AR class to an Axlsx package options are passed into AR find

Parameters:

  • columns (Array, Array) —

    as an array of symbols or a symbol that defines the attributes or methods to render in the sheet.

  • options (Hash) (defaults to: {}) —

    a customizable set of options

Options Hash (options):

  • header_style (Integer) —

    to apply to the first row of field names

  • types (Array, Symbol) —

    an array of Axlsx types for each cell in data rows or a single type that will be applied to all types.

  • style (Integer, Array) —

    The style to pass to Worksheet#add_row

  • i18n (String) —

    The path to i18n attributes. (usually activerecord.attributes)

  • package (Package) —

    An Axlsx::Package. When this is provided the output will be added to the package as a new sheet. # @option options [String] name This will be used to name the worksheet added to the package. If it is not provided the name of the table name will be humanized when i18n is not specified or the I18n.t for the table name.

See Also:

  • Worksheet#add_row


48
49
50
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/acts_as_xlsx/ar.rb', line 48

def to_xlsx(options = {})
  if self.xlsx_columns.nil?
    self.xlsx_columns = self.column_names.map { |c| c = c.to_sym }
  end

  row_style = options.delete(:style)
  header_style = options.delete(:header_style) || row_style
  types = [options.delete(:types) || []].flatten

  i18n = if options.key?(:i18n)
           options.delete(:i18n)
         else
           self.xlsx_i18n
         end

  # columns => [["column_name", "header"], [...], ...]
  columns = Array.wrap(options.delete(:columns) || self.xlsx_columns).flat_map { |col|
    col.is_a?(Hash) ? col.to_a : [[col, nil]]
  }

  headers = options.delete(:headers)
  headers||= columns.map { |(col_name, col_header)|
    if col_header
      col_header
    elsif i18n == true
      self.human_attribute_name(col_name)
    elsif i18n
      I18n.t("#{i18n_key}.#{self.name.underscore}.#{col_name}", default: col_name.to_s.humanize)
    else
      col_name.to_s.humanize
    end
  }

  p = options.delete(:package) || Package.new
  row_style = p.workbook.styles.add_style(row_style) unless row_style.nil?
  header_style = p.workbook.styles.add_style(header_style) unless header_style.nil?
  i18n_key = i18n == true ? 'activerecord.attributes' : i18n
  sheet_name = options.delete(:name) || (i18n ? (i18n == true ? self.model_name.human : I18n.t("#{i18n_key}.#{table_name.underscore}", default: table_name.humanize)) : table_name.humanize)
  data = options.delete(:data) || where(options[:where]).order(options[:order]).to_a
  data = data.compact.flatten


  return p if data.empty?
  p.workbook.add_worksheet(:name=>sheet_name) do |sheet|
    sheet.add_row headers, :style=>header_style
    
    data.each do |r|
      row_data = columns.map do |(column_name, _column_header)|
        v = r
        column_name.to_s.split('.').each do |method|
          !v.nil? ? v = v.send(method) : v = nil
        end
        v
      end
      sheet.add_row row_data, :style=>row_style, :types=>types
    end
  end
  p
end