Class: AbAdmin::Utils::XlsDocument
- Defined in:
- lib/ab_admin/utils/xls_document.rb
Instance Method Summary collapse
- #add_worksheet(*args) ⇒ Object
- #column_data ⇒ Object
- #columns_names ⇒ Object
- #default_columns ⇒ Object
- #each_with_index ⇒ Object
- #filename ⇒ Object
-
#initialize(source, options = {}) ⇒ XlsDocument
constructor
A new instance of XlsDocument.
- #render(context, options = {}) ⇒ Object
- #workbook ⇒ Object
- #worksheet ⇒ Object
- #worksheet_name ⇒ Object
Constructor Details
#initialize(source, options = {}) ⇒ XlsDocument
Returns a new instance of XlsDocument.
15 16 17 18 19 20 |
# File 'lib/ab_admin/utils/xls_document.rb', line 15 def initialize(source, = {}) @source = source @options = @compiled = false @io = ::StringIO.new end |
Instance Method Details
#add_worksheet(*args) ⇒ Object
30 31 32 |
# File 'lib/ab_admin/utils/xls_document.rb', line 30 def add_worksheet(*args) @worksheet = workbook.add_worksheet(*args) end |
#column_data ⇒ Object
38 39 40 |
# File 'lib/ab_admin/utils/xls_document.rb', line 38 def column_data @columns_names ||= @options[:column_data] || default_columns end |
#columns_names ⇒ Object
42 43 44 |
# File 'lib/ab_admin/utils/xls_document.rb', line 42 def columns_names (@options[:column_names] || default_columns).map { |column| column.is_a?(Symbol) ? @klass.human_attribute_name(column) : column } end |
#default_columns ⇒ Object
34 35 36 |
# File 'lib/ab_admin/utils/xls_document.rb', line 34 def default_columns @default_columns ||= @klass.column_names end |
#each_with_index ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ab_admin/utils/xls_document.rb', line 86 def each_with_index count = 0 if @source.is_a?(::ActiveRecord::Relation) @klass ||= @source.klass @source.find_each do |item| yield item, count count += 1 end else items = @source.respond_to?(:to_a) ? @source.to_a : Array.wrap(@source) @klass ||= items.first.class unless items.empty? @klass ||= Default items.each do |item| yield item, count count += 1 end end end |
#filename ⇒ Object
46 47 48 |
# File 'lib/ab_admin/utils/xls_document.rb', line 46 def filename @filename ||= [@options[:filename] || "#{@klass.model_name.plural}-#{Time.now.strftime('%Y-%m-%d')}", '.xlsx'].join end |
#render(context, options = {}) ⇒ Object
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 |
# File 'lib/ab_admin/utils/xls_document.rb', line 50 def render(context, ={}) date_format = workbook.add_format(num_format: 'dd.mm.yyyy') time_format = workbook.add_format(num_format: 'dd.mm.yyyy HH:MM') I18n.with_locale [:locale] do each_with_index do |item, index| row = index + 1 column_data.each_with_index do |column, num| value = column.is_a?(Symbol) ? item.public_send(column) : context.instance_exec(item, &column) case value when Date worksheet.write_string(row, num, value.strftime('%Y-%m-%dT'), date_format) when DateTime, Time worksheet.write_date_time(row, num, value.strftime('%Y-%m-%dT%H:%M:%S.%L'), time_format) when String worksheet.write_string(row, num, value) else worksheet.write(row, num, AbAdmin.pretty_data(value)) end end end end bold = workbook.add_format(bold: 1) worksheet.write('A1', columns_names, bold) workbook.close @io.string end |
#workbook ⇒ Object
22 23 24 |
# File 'lib/ab_admin/utils/xls_document.rb', line 22 def workbook @workbook ||= ::WriteXLSX.new(@io) end |
#worksheet ⇒ Object
26 27 28 |
# File 'lib/ab_admin/utils/xls_document.rb', line 26 def worksheet @worksheet ||= add_worksheet(worksheet_name) end |
#worksheet_name ⇒ Object
82 83 84 |
# File 'lib/ab_admin/utils/xls_document.rb', line 82 def worksheet_name @worksheet_name ||= (@options[:worksheet_name] || @klass.model_name.human) end |