Module: Bulkrax::ErroredEntries

Extended by:
ActiveSupport::Concern
Included in:
CsvParser
Defined in:
app/models/concerns/bulkrax/errored_entries.rb

Instance Method Summary collapse

Instance Method Details

#build_errored_entry_row(headers, errored_entry) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/concerns/bulkrax/errored_entries.rb', line 28

def build_errored_entry_row(headers, errored_entry)
  row = {}
  # Ensure each header has a value, even if it's just an empty string
  headers.each do |h|
    row.merge!("#{h}": nil)
  end
  # Match each value to its corresponding header
  row.merge!(errored_entry..symbolize_keys)

  row.values.to_csv
end

#setup_errored_entries_fileObject



40
41
42
43
# File 'app/models/concerns/bulkrax/errored_entries.rb', line 40

def setup_errored_entries_file
  FileUtils.mkdir_p(File.dirname(importerexporter.errored_entries_csv_path))
  File.open(importerexporter.errored_entries_csv_path, 'w')
end

#write_errored_entries_fileObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/concerns/bulkrax/errored_entries.rb', line 7

def write_errored_entries_file
  if @errored_entries.blank?
    entry_ids = importerexporter.entries.pluck(:id)
    error_statuses = Bulkrax::Status.latest_by_statusable
                                    .includes(:statusable)
                                    .where('bulkrax_statuses.statusable_id IN (?) AND bulkrax_statuses.statusable_type = ? AND status_message = ?', entry_ids, 'Bulkrax::Entry', 'Failed')
    @errored_entries = error_statuses.map(&:statusable)
  end
  return if @errored_entries.blank?

  file = setup_errored_entries_file
  headers = import_fields
  file.puts(headers.to_csv)
  @errored_entries.each do |ee|
    row = build_errored_entry_row(headers, ee)
    file.puts(row)
  end
  file.close
  true
end