Module: Axlsx::Ar::SingletonMethods
- Defined in:
- lib/acts_as_xlsx/ar.rb
Overview
Singleton methods for the mixin
Instance Method Summary collapse
-
#to_xlsx(options = {}) ⇒ Object
Maps the AR class to an Axlsx package options are passed into AR find.
Instance Method Details
#to_xlsx(options = {}) ⇒ Object
Maps the AR class to an Axlsx package options are passed into AR find
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 |
# File 'lib/acts_as_xlsx/ar.rb', line 48 def to_xlsx( = {}) row_style = .delete(:style) header_style = .delete(:header_style) || row_style types = [.delete(:types) || []].flatten i18n = .delete(:i18n) || self.xlsx_i18n columns = .delete(:columns) || self.xlsx_columns p = .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 = self.xlsx_i18n == true ? 'activerecord.attributes' : i18n sheet_name = .delete(:name) || (i18n ? I18n.t("#{i18n}.#{table_name.underscore}") : table_name.humanize) data = .delete(:data) || [*find(:all, )] data.compact! data.flatten! return p if data.empty? p.workbook.add_worksheet(:name=>sheet_name) do |sheet| col_labels = if i18n columns.map { |c| I18n.t("#{i18n}.#{self.name.underscore}.#{c}") } else columns.map { |c| c.to_s.humanize } end sheet.add_row col_labels, :style=>header_style data.each do |r| row_data = columns.map do |c| if c.to_s =~ /\./ v = r; c.to_s.split('.').each { |method| v = v.send(method) }; v else r.send(c) end end sheet.add_row row_data, :style=>row_style, :types=>types end end p end |