Skip to content

Commit

Permalink
Add work to a collection if it is already created (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkadel authored Sep 10, 2021
1 parent 0447f7d commit 4d6fbea
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
9 changes: 0 additions & 9 deletions app/views/zizia/csv_imports/_import_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@
<%= render "file_upload", form: form %>
</div>
</div>
<div class="well well-sm" style="display: none">
<h4><span class="bullet">Z</span></h4>
<div class="step">
<b>Choose a collection</b> that you'd like your new works to be associated with.
</div>
<div class="text-center">
<%= render "collection_selection", form: form %>
</div>
</div>
<div class="well well-sm">
<h4><span class="bullet">B</span></h4>
<div class="step">
Expand Down
17 changes: 15 additions & 2 deletions lib/zizia/hyrax/hyrax_record_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,21 @@ def initialize(attributes: {})
# These attributes are persisted in the CsvImportDetail model
@csv_import_detail = attributes[:csv_import_detail]
@deduplication_field = csv_import_detail.deduplication_field
@collection_id = csv_import_detail.collection_id if csv_import_detail.collection_id.present?
@collection_id = find_collection_id(csv_import_detail: csv_import_detail, record: nil)
@batch_id = csv_import_detail.batch_id
@success_count = csv_import_detail.success_count
@failure_count = csv_import_detail.failure_count
find_depositor(csv_import_detail.depositor_id)
end

def find_collection_id(csv_import_detail:, record:)
if csv_import_detail&.collection_id&.present?
csv_import_detail.collection_id
elsif record&.parent&.first
Collection.where(deduplication_key: "def/123")&.first&.id
end
end

# "depositor" is a required field for Hyrax. If
# it hasn't been set, set it to the Hyrax default
# batch user.
Expand Down Expand Up @@ -184,7 +192,12 @@ def process_attrs(record:)
}

attrs = record.attributes.merge(additional_attrs)
attrs = attrs.merge(member_of_collections_attributes: { '0' => { id: collection_id } }) if collection_id
if collection_id
attrs = attrs.merge(member_of_collections_attributes: { '0' => { id: collection_id } })
elsif find_collection_id(csv_import_detail: nil, record: record)
attrs = attrs.merge(member_of_collections_attributes: { '0' => { id: find_collection_id(csv_import_detail: nil, record: record) } })
end

# Ensure nothing is passed in the files field,
# since this is reserved for Hyrax and is where uploaded_files will be attached
attrs.delete(:files)
Expand Down
9 changes: 7 additions & 2 deletions spec/dummy/app/models/collection.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Generated by hyrax:models
class Collection < ActiveFedora::Base
include ::Hyrax::CollectionBehavior
# You can replace these metadata if they're not suitable
include Hyrax::BasicMetadata

property :deduplication_key, predicate: ::RDF::Vocab::BF2::identifiedBy, multiple: false do |index|
index.as :stored_searchable
end
self.indexer = Hyrax::CollectionWithBasicMetadataIndexer
# This must be included at the end, because it finalizes the metadata
# schema (by adding accepts_nested_attributes)
include Hyrax::BasicMetadata
end
2 changes: 1 addition & 1 deletion spec/dummy/spec/fixtures/csv_import/good/all_fields.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
parent,object type,identifier,type,license,deduplication_key,visibility,location,keyword,rights statement,creator,title,files
,w,abc/123,work,https://creativecommons.org/licenses/by/4.0/,abc/123,PUBlic,http://www.geonames.org/5667009/montana.html|~|http://www.geonames.org/6252001/united-states.html,Clothing stores $z California $z Los Angeles|~|Interior design $z California $z Los Angeles,http://rightsstatements.org/vocab/InC/1.0/,"Connell, Will, $d 1898-1961","Interior view of The Bachelors haberdashery designed by Julius Ralph Davidson, Los Angeles, circa 1929",dog.jpg
def/123,w,abc/123,work,https://creativecommons.org/licenses/by/4.0/,abc/123,PUBlic,http://www.geonames.org/5667009/montana.html|~|http://www.geonames.org/6252001/united-states.html,Clothing stores $z California $z Los Angeles|~|Interior design $z California $z Los Angeles,http://rightsstatements.org/vocab/InC/1.0/,"Connell, Will, $d 1898-1961","Interior view of The Bachelors haberdashery designed by Julius Ralph Davidson, Los Angeles, circa 1929",dog.jpg
,C,,,,7,Public,,,,,Test collection,
26 changes: 25 additions & 1 deletion spec/dummy/spec/system/import_from_csv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'rails_helper'
include Warden::Test::Helpers

RSpec.describe 'Importing records from a CSV file', :perform_jobs, :clean, type: :system, js: true do
RSpec.describe 'Importing records from a CSV file', :perform_jobs, clean: true, type: :system, js: true do
before do
allow(CharacterizeJob).to receive(:perform_later)
ENV['IMPORT_PATH'] = File.join(fixture_path, 'images')
Expand Down Expand Up @@ -43,6 +43,7 @@
before do
test_strategy.switch!(:new_zizia_ui, true)
Collection.destroy_all
Work.destroy_all
end

it 'starts the import' do
Expand Down Expand Up @@ -225,6 +226,29 @@
expect(page).to have_content('haberdashery')
expect(page).to have_content('Date Created')
end

context 'with an existing collection' do
let(:collection) { FactoryBot.build(:collection, title: ['Testing Collection'], deduplication_key: 'def/123') }
before do
collection.save!
end

it 'adds the work to the parent collection' do
visit '/csv_imports/new'
select 'Update Existing Metadata, create new works', from: "csv_import[update_actor_stack]"

attach_file('csv_import[manifest]', csv_file, make_visible: true)
click_on 'Preview Import'
click_on 'Start Import'

# Let the background jobs run, and check that the expected number of records got created.
expect(Work.count).to eq 1
# Ensure that all the fields got assigned as expected
work = Work.where(title: "*haberdashery*").first
expect(collection.deduplication_key).to eq 'def/123'
expect(work.member_of_collection_ids).to eq [collection.id]
end
end
end
context 'using the old UI' do
let(:collection) { FactoryBot.build(:collection, title: ['Testing Collection']) }
Expand Down

0 comments on commit 4d6fbea

Please sign in to comment.