Class: XlsReporter::DataExport
- Inherits:
-
Object
- Object
- XlsReporter::DataExport
- Defined in:
- lib/xls_reporter.rb
Constant Summary collapse
- NUMBER_OF_RETRIES =
5
Instance Method Summary collapse
-
#generate_excel(collection, header_params, body_params, filename = "report_#{Time.now.strftime("%Y%m%d%H%M")}.xlsx", filepath = default_path, batch_iteration = true, retry_methods = true) ⇒ Object
generate_excel: Main method to generate file —————————————————————————- Valid header_params format: header_params = [ => 0, :title => I18n.t(model.attributes), => 1, :title => “Event”, => 2, :title => “SKU”, ] —————————————————————————- Valid body_params format: body_params = [ => 0, :object_methods => [ {:method_name=>:values_by_ids, :params_values => [1,2,4], :get_color , =>:set_size, :params_values => “large”] }, {:index => 1, :object_methods => {:get_event => Time.now } }, {:index => 2, :object_methods => :get_sku }, ].
Instance Method Details
#generate_excel(collection, header_params, body_params, filename = "report_#{Time.now.strftime("%Y%m%d%H%M")}.xlsx", filepath = default_path, batch_iteration = true, retry_methods = true) ⇒ Object
generate_excel: Main method to generate file
Valid header_params format:
header_params = [
{:index => 0, :title => I18n.t(model.attributes)},
{:index => 1, :title => "Event"},
{:index => 2, :title => "SKU"},
]
Valid body_params format:
body_params = [
{:index => 0, :object_methods => [ {:method_name=>:values_by_ids, :params_values => [1,2,4]}, :get_color , {:method_name =>:set_size, :params_values => "large"}] },
{:index => 1, :object_methods => {:get_event => Time.now } },
{:index => 2, :object_methods => :get_sku },
]
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/xls_reporter.rb', line 24 def generate_excel collection, header_params, body_params, filename="report_#{Time.now.strftime("%Y%m%d%H%M")}.xlsx", filepath = default_path, batch_iteration = true, retry_methods = true fullpath= File.join(filepath, filename) puts "#{Time.now} Generating #{fullpath}" workbook = WriteXLSX.new(fullpath) worksheet = workbook.add_worksheet row = 0 # Set Headers if header_params header_params.each do |param| worksheet.write(row, param[:index], param[:title]) end row = row + 1 end # Use batch iteration if ActiveRecord is defined and param is set iterate_method = defined?(ActiveRecord) && collection.is_a?(ActiveRecord::Relation) && batch_iteration ? "find_each" : "each" if body_params collection.send(iterate_method) do |object| body_params.each do |param| # Get object column value depending of datatype of args object_value = if param[:object_methods].is_a? Array #Multiple methods #Chaining methods across the array param[:object_methods].reduce(object) do |method_params, chained_value| call_method chained_value, method_params, retry_methods ? 0 : -1 end else call_method object, param[:object_methods], retry_methods ? 0 : -1 end worksheet.write(row, param[:index], object_value) end row = row + 1 end end workbook.close puts "#{Time.now} Exporting to #{fullpath} done!" end |