Class: ExportCsvService

Inherits:
Object
  • Object
show all
Defined in:
app/services/export_csv_service.rb

Overview

ExportCsvService

Class Method Summary collapse

Class Method Details

.call(document_ids) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
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
# File 'app/services/export_csv_service.rb', line 11

def self.call(document_ids)
  ActionCable.server.broadcast("export_channel", {progress: 0})

  document_ids = document_ids.flatten
  total = document_ids.size
  count = 0
  slice_count = 100
  csv_file = []

  Rails.logger.debug { "\n\nExportCsvService: #{document_ids.inspect}\n\n" }

  CSV.generate(headers: true) do |_csv|
    Rails.logger.debug do
      "\n\n CSV Fields: #{GeoblacklightAdmin::Schema.instance.exportable_fields.map do |k, _v|
                            k.to_s
                          end} \n\n"
    end

    csv_file << GeoblacklightAdmin::Schema.instance.exportable_fields.map { |k, _v| k.to_s }
    document_ids.each_slice(slice_count) do |slice|
      # Broadcast progress percentage
      count += slice_count
      progress = ((count.to_f / total) * 100).round
      progress = 100 if progress > 100

      ActionCable.server.broadcast("export_channel", {progress: progress})
      slice.each do |doc_id|
        doc = Document.find_by(friendlier_id: doc_id)
        csv_file << doc.to_csv
      rescue NoMethodError
        Rails.logger.debug { "\n\nExport Failed: #{doc_id.inspect}\n\n" }
      end
    end
  end

  csv_file
end

.short_nameObject



7
8
9
# File 'app/services/export_csv_service.rb', line 7

def self.short_name
  "Documents"
end