Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add importer #15

Merged
merged 1 commit into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions app/importers/csv_importer.rb
Original file line number Diff line number Diff line change
@@ -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
Empty file removed lib/tasks/.keep
Empty file.
16 changes: 16 additions & 0 deletions lib/tasks/csv_import.rake
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions spec/importers/csv_importer_spec.rb
Original file line number Diff line number Diff line change
@@ -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