Sha256: 96ff7a15d9c3ab990e080145a86047e4c1b13c1e590f38e0c249b9547043ab31

Contents?: true

Size: 1.8 KB

Versions: 43

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

require 'csv'

module Spotlight
  module Resources
    ##
    # Creating new exhibit items from single-item entry forms
    # or batch CSV upload
    class CsvUploadController < ApplicationController
      helper :all

      before_action :authenticate_user!

      load_and_authorize_resource :exhibit, class: Spotlight::Exhibit

      def create
        csv = CSV.parse(csv_io_param, headers: true, return_headers: false).map(&:to_hash)
        Spotlight::AddUploadsFromCsv.perform_later(csv, current_exhibit, current_user)
        flash[:notice] = t('spotlight.resources.upload.csv.success', file_name: csv_io_name)
        redirect_to spotlight.admin_exhibit_catalog_path(current_exhibit)
      end

      def template
        render plain: CSV.generate { |csv| csv << data_param_keys.unshift(:url) }, content_type: 'text/csv'
      end

      private

      def build_resource
        @resource ||= Spotlight::Resources::Upload.new exhibit: current_exhibit
      end

      def csv_params
        params.require(:resources_csv_upload).permit(:url)
      end

      def data_param_keys
        Spotlight::Resources::Upload.fields(current_exhibit).map(&:field_name) + current_exhibit.custom_fields.map(&:field)
      end

      # Gets an IO-like object for the CSV parser to use.
      # @return IO
      def csv_io_param
        file_or_io = csv_params[:url]
        io = if file_or_io.respond_to?(:to_io)
               file_or_io.to_io
             else
               file_or_io
             end

        io.set_encoding('utf-8')
      end

      def csv_io_name
        file_or_io = csv_params[:url]

        if file_or_io.respond_to? :original_filename
          file_or_io.original_filename
        else
          t('spotlight.resources.upload.csv.anonymous_file')
        end
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
blacklight-spotlight-4.2.0 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-4.1.2 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-4.1.1 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-4.1.0 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-4.0.3 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-4.0.2 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-4.0.1 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-4.0.0 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.6.0.beta10 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.6.0.beta9 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.6.0.beta8 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.5.0.4 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.6.0.beta7 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.5.0.3 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.6.0.beta6 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.6.0.beta5 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.6.0.beta4 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.6.0.beta3 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.6.0.beta1 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.5.0.2 app/controllers/spotlight/resources/csv_upload_controller.rb