From 805f039767d6f4e1b37a1610440330b9e9a80f7d Mon Sep 17 00:00:00 2001 From: jendiamond Date: Wed, 11 Sep 2019 16:24:14 -0700 Subject: [PATCH] Add importer --- Changes to be committed: + new file: app/importers/csv_importer.rb + deleted: lib/tasks/.keep + new file: lib/tasks/csv_import.rake + new file: spec/importers/csv_importer_spec.rb --- app/importers/csv_importer.rb | 27 +++++++++++++++++++++++++++ lib/tasks/.keep | 0 lib/tasks/csv_import.rake | 16 ++++++++++++++++ spec/importers/csv_importer_spec.rb | 19 +++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 app/importers/csv_importer.rb delete mode 100644 lib/tasks/.keep create mode 100644 lib/tasks/csv_import.rake create mode 100644 spec/importers/csv_importer_spec.rb diff --git a/app/importers/csv_importer.rb b/app/importers/csv_importer.rb new file mode 100644 index 0000000..8e0238a --- /dev/null +++ b/app/importers/csv_importer.rb @@ -0,0 +1,27 @@ +require 'csv' + +class CsvImporter + def initialize(file) + @file = file + @user = ::User.batch_user + end + + def import + CSV.foreach(@file) do |row| + image = Image.new + image.title = [row[1]] + image.source = [row[2]] + image.visibility = "open" + image.depositor = @user.email + # Attach the image file and run it through the actor stack + # Try entering Hyrax::CurationConcern.actor on a console to see all of the + # actors this object will run through. + image_binary = File.open("#{::Rails.root}/spec/fixtures/images/#{row[0]}") + uploaded_file = Hyrax::UploadedFile.create(user: @user, file: image_binary) + attributes_for_actor = { uploaded_files: [uploaded_file.id] } + env = Hyrax::Actors::Environment.new(image, ::Ability.new(@user), attributes_for_actor) + Hyrax::CurationConcern.actor.create(env) + image_binary.close + end + end +end diff --git a/lib/tasks/.keep b/lib/tasks/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/lib/tasks/csv_import.rake b/lib/tasks/csv_import.rake new file mode 100644 index 0000000..2ecf636 --- /dev/null +++ b/lib/tasks/csv_import.rake @@ -0,0 +1,16 @@ +# frozen_string_literal: true +CSV_FILE = "#{::Rails.root}/spec/fixtures/three_line_example.csv" + + namespace :sample do + + desc 'Import the three line sample CSV' + task :import_three_line_csv => [:environment] do |_task| + CsvImporter.new(CSV_FILE).import + end + + desc 'Import a different CSV' + task :import, [:filename] => [:environment] do |_task, args| + csv_file = args[:filename] + CsvImporter.new(csv_file).import + end +end diff --git a/spec/importers/csv_importer_spec.rb b/spec/importers/csv_importer_spec.rb new file mode 100644 index 0000000..49c3b55 --- /dev/null +++ b/spec/importers/csv_importer_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'active_fedora/cleaner' + +RSpec.describe CsvImporter do + let(:one_line_example) { 'spec/fixtures/one_line_example.csv' } + let(:three_line_example) { 'spec/fixtures/three_line_example.csv' } + let(:user) { ::User.batch_user } + + before do + DatabaseCleaner.clean + ActiveFedora::Cleaner.clean! + end + + it 'imports a csv' do + expect { CsvImporter.new(three_line_example).import }.to change { Image.count } + end +end