Class: SpotlightSearch::ExportJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/spotlight_search/export_job.rb

Instance Method Summary collapse

Instance Method Details

#create_excel(records, klass, columns) ⇒ Object

Creating excel with the passed records Keys as headers and values as row



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/jobs/spotlight_search/export_job.rb', line 53

def create_excel(records, klass, columns)
  size_arr = []
  columns.size.times { size_arr << 22 }
  xl = Axlsx::Package.new
  xl.workbook.add_worksheet do |sheet|
    sheet.add_row columns, b: true
    records.each do |record|
      sheet.add_row columns.map { |column| record.send(column) }
    end
    sheet.column_widths *size_arr
  end
  file_location = "#{Rails.root}/public/export_#{klass}_#{Time.now.to_s}.xls"
  xl.serialize(file_location)
  file_location
end

#create_excel_v2(records, class_name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/jobs/spotlight_search/export_job.rb', line 69

def create_excel_v2(records, class_name)
  flattened_records = records.map { |record| SpotlightSearch::Utils.flatten_hash(record) }
  columns = flattened_records[0].keys
  size_arr = []
  columns.size.times { size_arr << 22 }
  xl = Axlsx::Package.new
  xl.workbook.add_worksheet do |sheet|
    sheet.add_row columns&.map(&:titleize), b: true
    flattened_records.each do |record|
      sheet.add_row(columns.map { |column| record[column] })
    end
    sheet.column_widths(*size_arr)
  end
  file_location = "#{Rails.root}/public/export_#{class_name}_#{Time.now.to_s}.xls"
  xl.serialize(file_location)
  file_location
end

#get_records(klass, columns, filters, sort) ⇒ Object



22
23
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
# File 'app/jobs/spotlight_search/export_job.rb', line 22

def get_records(klass, columns, filters, sort)
  records = klass
  if filters.present?
    filters.each do |scope, scope_args|
      records = records.send(scope, scope_args)
    end
  end
  if klass.default_filters.present?
    klass.default_filters.each do |scope|
      records = records.send(scope)
    end
  end
  if sort.present?
    records = records.order("#{sort['sort_column']} #{sort['sort_direction']}")
  end
  if filters.blank? && sort.blank?
    records = records.all
  end
  case SpotlightSearch.exportable_columns_version
  when :v1
    columns = columns.map(&:to_sym)
    records.select(*columns)
  when :v2
    deserialized_columns = SpotlightSearch::Utils.deserialize_csv_columns(columns, :as_json_params)
    # This includes isn't recursve, a full solution should be recursive
    records.includes(deserialized_columns[:include].keys).find_each.as_json(deserialized_columns)
  end
end

#perform(klass_name, email, columns = [], filters = {}, sort = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/jobs/spotlight_search/export_job.rb', line 5

def perform(klass_name, email, columns = [], filters = {}, sort = {})
  klass = klass_name.constantize
  records = get_records(klass, columns, filters, sort)
  file_path =
    case SpotlightSearch.exportable_columns_version
    when :v1
      create_excel(records, klass.name, columns)
    when :v2
      create_excel_v2(records, klass.name)
    end
  subject = "#{klass.name} export at #{Time.now}"
  ExportMailer.send_excel_file(email, file_path, subject).deliver_now
  File.delete(file_path)
rescue StandardError => e
  ExportMailer.send_error_message(email, e).deliver_now
end