Class: RailsExcelReporter::Base

Inherits:
Object
  • Object
show all
Includes:
Streaming, Styling
Defined in:
lib/rails_excel_reporter/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Streaming

#build_progress_info, #collection_size, included, #should_stream?, #stream_data, #stream_large_dataset, #with_progress_tracking

Methods included from Styling

#apply_style, #build_caxlsx_style, #get_column_style, #get_header_style, included, #merge_styles

Constructor Details

#initialize(collection, worksheet_name: nil, &progress_callback) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
20
# File 'lib/rails_excel_reporter/base.rb', line 15

def initialize(collection, worksheet_name: nil, &progress_callback)
  @collection = collection
  @worksheet_name = worksheet_name || default_worksheet_name
  @progress_callback = progress_callback
  @rendered = false
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



13
14
15
# File 'lib/rails_excel_reporter/base.rb', line 13

def collection
  @collection
end

#progress_callbackObject (readonly)

Returns the value of attribute progress_callback.



13
14
15
# File 'lib/rails_excel_reporter/base.rb', line 13

def progress_callback
  @progress_callback
end

#worksheet_nameObject (readonly)

Returns the value of attribute worksheet_name.



13
14
15
# File 'lib/rails_excel_reporter/base.rb', line 13

def worksheet_name
  @worksheet_name
end

Class Method Details

.attribute(name, options = {}) ⇒ Object



49
50
51
52
# File 'lib/rails_excel_reporter/base.rb', line 49

def self.attribute(name, options = {})
  @attributes ||= []
  @attributes << { name: name.to_sym, header: options[:header] || name.to_s.humanize }
end

.attributes(*attrs) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/rails_excel_reporter/base.rb', line 22

def self.attributes(*attrs)
  if attrs.empty?
    @attributes ||= []
  else
    @attributes = attrs.map { |attr| process_attribute attr }
  end
end

.inherited(subclass) ⇒ Object



54
55
56
57
# File 'lib/rails_excel_reporter/base.rb', line 54

def self.inherited(subclass)
  super
  subclass.instance_variable_set :@attributes, @attributes.dup if @attributes
end

.process_attribute(attr) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/rails_excel_reporter/base.rb', line 30

def self.process_attribute(attr)
  case attr
  when Symbol
    { name: attr, header: attr.to_s.humanize }
  when Hash
    symbolize_hash_keys(attr)
  else
    { name: attr.to_sym, header: attr.to_s.humanize }
  end
end

.symbolize_hash_keys(hash) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/rails_excel_reporter/base.rb', line 41

def self.symbolize_hash_keys(hash)
  result = {}
  hash.each do |key, value|
    result[key.to_sym] = value
  end
  result
end

Instance Method Details

#after_renderObject



101
# File 'lib/rails_excel_reporter/base.rb', line 101

def after_render; end

#after_row(object) ⇒ Object



105
# File 'lib/rails_excel_reporter/base.rb', line 105

def after_row(object); end

#attributesObject



59
60
61
# File 'lib/rails_excel_reporter/base.rb', line 59

def attributes
  self.class.attributes
end

#before_renderObject



99
# File 'lib/rails_excel_reporter/base.rb', line 99

def before_render; end

#before_row(object) ⇒ Object



103
# File 'lib/rails_excel_reporter/base.rb', line 103

def before_row(object); end

#fileObject



63
64
65
66
# File 'lib/rails_excel_reporter/base.rb', line 63

def file
  render unless @rendered
  @tempfile
end

#filenameObject



80
81
82
83
# File 'lib/rails_excel_reporter/base.rb', line 80

def filename
  timestamp = Time.now.strftime(RailsExcelReporter.config.date_format).gsub '-', '_'
  "#{worksheet_name.parameterize}_report_#{timestamp}.xlsx"
end

#save_to(path) ⇒ Object



74
75
76
77
78
# File 'lib/rails_excel_reporter/base.rb', line 74

def save_to(path)
  File.open path, 'wb' do |file|
    file.write to_xlsx
  end
end

#streamObject



85
86
87
# File 'lib/rails_excel_reporter/base.rb', line 85

def stream
  StringIO.new to_xlsx
end

#to_hObject



89
90
91
92
93
94
95
96
97
# File 'lib/rails_excel_reporter/base.rb', line 89

def to_h
  {
    worksheet_name: worksheet_name,
    attributes: attributes,
    data: collection_data,
    collection_size: collection_size,
    streaming: should_stream?
  }
end

#to_xlsxObject



68
69
70
71
72
# File 'lib/rails_excel_reporter/base.rb', line 68

def to_xlsx
  render unless @rendered
  @tempfile.rewind
  @tempfile.read
end