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(context, options = {}) ⇒ 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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ab_admin/utils/xls_document.rb', line 88 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(context, options = {}) ⇒ 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 81 82 |
# File 'lib/ab_admin/utils/xls_document.rb', line 52 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 = call_method_or_proc_on(item, column, context: context) 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
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
84 85 86 |
# File 'lib/ab_admin/utils/xls_document.rb', line 84 def worksheet_name @worksheet_name ||= (@options[:worksheet_name] || @klass.model_name.human) end |