Sha256: 7aeb4913e632a5bf9bf4d2589e2838db7320e8bda411f3b1243ce1cdeb44c31a

Contents?: true

Size: 1023 Bytes

Versions: 9

Compression:

Stored size: 1023 Bytes

Contents

# encoding: utf-8
# frozen_string_literal: true

module Spotlight
  ##
  # Process a CSV upload into new Spotlight::Resource::Upload objects
  class AddUploadsFromCSV < ActiveJob::Base
    queue_as :default

    after_perform do |job|
      csv_data, exhibit, user = job.arguments
      Spotlight::IndexingCompleteMailer.documents_indexed(csv_data, exhibit, user).deliver_now
    end

    def perform(csv_data, exhibit, _user)
      encoded_csv(csv_data).each do |row|
        url = row.delete('url')
        next unless url.present?

        resource = Spotlight::Resources::Upload.new(
          data: row,
          exhibit: exhibit
        )
        resource.build_upload(remote_image_url: url)
        resource.save_and_index
      end
    end

    private

    def encoded_csv(csv)
      csv.map do |row|
        row.map do |label, column|
          [label, column.encode('UTF-8', invalid: :replace, undef: :replace, replace: "\uFFFD")] if column.present?
        end.compact.to_h
      end.compact
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
blacklight-spotlight-2.7.2 app/jobs/spotlight/add_uploads_from_csv.rb
blacklight-spotlight-2.7.1 app/jobs/spotlight/add_uploads_from_csv.rb
blacklight-spotlight-2.7.0 app/jobs/spotlight/add_uploads_from_csv.rb
blacklight-spotlight-2.6.1.1 app/jobs/spotlight/add_uploads_from_csv.rb
blacklight-spotlight-2.6.1 app/jobs/spotlight/add_uploads_from_csv.rb
blacklight-spotlight-2.6.0 app/jobs/spotlight/add_uploads_from_csv.rb
blacklight-spotlight-2.5.2 app/jobs/spotlight/add_uploads_from_csv.rb
blacklight-spotlight-2.5.1 app/jobs/spotlight/add_uploads_from_csv.rb
blacklight-spotlight-2.5.0 app/jobs/spotlight/add_uploads_from_csv.rb