CLOSER-Cohorts/archivist

View on GitHub
lib/importers.rb

Summary

Maintainability
A
35 mins
Test Coverage
module Importers
  module Controller
    extend ActiveSupport::Concern
    included do
      def member_imports
        imports = params[:imports].nil? ? [] : params[:imports]
        head :ok, format: :json if imports.empty?
        begin
          imports.each do |import|
            doc = Document.new file: import[:file]
            doc.save_or_get

            type = import[:type]&.downcase&.to_sym

            class_variable_get(:@@map)[type].perform_async(doc.id, @object)
          end
          head :ok, format: :json
        rescue  => e
          render json: {message: e}, status: :bad_request
        end
      end
    end

    class_methods do
      def has_importers(map)
        class_variable_set :@@map, map
      end
    end
  end
end