Skip to content

Commit

Permalink
Add importer
Browse files Browse the repository at this point in the history
---

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
  • Loading branch information
jendiamond committed Sep 11, 2019
1 parent 8bda60b commit 50e3672
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
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.
13 changes: 13 additions & 0 deletions lib/tasks/csv_import.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 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

0 comments on commit 50e3672

Please sign in to comment.