Sha256: 0512a5e6fd89bca3a1b0f33613b7cbcc6e69a4533ada977d7f9204f895269285

Contents?: true

Size: 1.8 KB

Versions: 17

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

17 entries across 17 versions & 1 rubygems

Version Path
blacklight-spotlight-3.0.0.alpha.10 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.0.0.alpha.9 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.0.0.alpha.8 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.0.0.alpha.7 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.0.0.alpha.6 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.0.0.alpha.5 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.0.0.alpha.4 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.0.0.alpha.3 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.0.0.alpha.2 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-3.0.0.alpha.1 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-2.13.0 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-2.12.1 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-2.12.0 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-2.11.0 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-2.10.0 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-2.9.0 app/controllers/spotlight/resources/csv_upload_controller.rb
blacklight-spotlight-2.8.0 app/controllers/spotlight/resources/csv_upload_controller.rb