Class: AbAdmin::Utils::XlsDocument
- Includes:
- EvalHelpers
- 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 ⇒ Object
- #workbook ⇒ Object
- #worksheet ⇒ Object
- #worksheet_name ⇒ Object
Methods included from EvalHelpers
#call_method_or_proc_on, #evaluate_method
Constructor Details
#initialize(source, options = {}) ⇒ XlsDocument
Returns a new instance of XlsDocument.
17 18 19 20 21 22 |
# File 'lib/ab_admin/utils/xls_document.rb', line 17 def initialize(source, = {}) @source = source @options = @compiled = false @io = ::StringIO.new end |
Instance Method Details
#add_worksheet(*args) ⇒ Object
32 33 34 |
# File 'lib/ab_admin/utils/xls_document.rb', line 32 def add_worksheet(*args) @worksheet = workbook.add_worksheet(*args) end |
#column_data ⇒ Object
40 41 42 |
# File 'lib/ab_admin/utils/xls_document.rb', line 40 def column_data @columns_names ||= @options[:column_data] || default_columns end |
#columns_names ⇒ Object
44 45 46 |
# File 'lib/ab_admin/utils/xls_document.rb', line 44 def columns_names (@options[:column_names] || default_columns).map { |column| column.is_a?(Symbol) ? @klass.human_attribute_name(column) : column } end |
#default_columns ⇒ Object
36 37 38 |
# File 'lib/ab_admin/utils/xls_document.rb', line 36 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
48 49 50 |
# File 'lib/ab_admin/utils/xls_document.rb', line 48 def filename @filename ||= [@options[:filename] || "#{@klass.model_name.plural}-#{Time.now.strftime('%Y-%m-%d')}", '.xlsx'].join end |
#render ⇒ Object
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 52 def render date_format = workbook.add_format(num_format: 'dd.mm.yyyy') time_format = workbook.add_format(num_format: 'dd.mm.yyyy HH:MM') each_with_index do |item, index| row = index + 1 column_data.each_with_index do |column, num| value = call_method_or_proc_on(item, column, exec: false) 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 bold = workbook.add_format(bold: 1) worksheet.write('A1', columns_names, bold) workbook.close @io.string end |
#workbook ⇒ Object
24 25 26 |
# File 'lib/ab_admin/utils/xls_document.rb', line 24 def workbook @workbook ||= ::WriteXLSX.new(@io) end |
#worksheet ⇒ Object
28 29 30 |
# File 'lib/ab_admin/utils/xls_document.rb', line 28 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 |