Skip to content

Commit

Permalink
Issue #15 working forc csv files with empty lines
Browse files Browse the repository at this point in the history
  • Loading branch information
irisvogel committed Nov 8, 2018
1 parent 9e0af4e commit 762aae6
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 6 deletions.
13 changes: 13 additions & 0 deletions app/importers/csv_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@ def initialize(file)
end
def import
CSV.foreach(@file) do |row|
next if row.empty?
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
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false

config.active_job.queue_adapter = :inline
# Do not eager load code on boot.
config.eager_load = false

Expand Down
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true

config.active_job.queue_adapter = :inline
# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
Expand Down
13 changes: 13 additions & 0 deletions lib/tasks/csv_import_task.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
Binary file modified spec/fixtures/images/birds.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified spec/fixtures/images/cat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified spec/fixtures/images/dog.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 20 additions & 4 deletions spec/importers/csv_importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,28 @@
DatabaseCleaner.clean
ActiveFedora::Cleaner.clean!
end

it "imports a csv" do
expect { CsvImporter.new(three_line_example).import }.to change { Image.count }.by 3
end
it "puts the title into the title field" do
CsvImporter.new(one_line_example).import
imported_image = Image.first
expect(imported_image.title.first).to eq 'A Cute Dog'
end


end
it "puts the url into the source field" do
CsvImporter.new(one_line_example).import
imported_image = Image.first
expect(imported_image.source.first).to eq 'https://www.pexels.com/photo/animal-blur-canine-close-up-551628/'
end
it "creates publicly visible objects" do
CsvImporter.new(one_line_example).import
imported_image = Image.first
expect(imported_image.visibility).to eq 'open'
end
it "attaches a file" do
CsvImporter.new(one_line_example).import
imported_image = Image.first
expect(imported_image.members.first).to be_instance_of(FileSet)
expect(imported_image.members.first.title.first).to eq 'dog.jpg'
end
end

0 comments on commit 762aae6

Please sign in to comment.