Skip to content
This repository has been archived by the owner on Jan 5, 2021. It is now read-only.

Commit

Permalink
Use alternate ids to specify collection membership
Browse files Browse the repository at this point in the history
When importing works from csv, we want to be able to use the
library-specific identifier as well as the internal Valkyrie one.
  • Loading branch information
awead committed Oct 16, 2018
1 parent 5dcaa39 commit 4a170c0
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 10 deletions.
12 changes: 10 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-09-27 09:48:27 -0400 using RuboCop version 0.52.1.
# on 2018-10-15 21:54:28 -0400 using RuboCop version 0.52.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -24,7 +24,7 @@ RSpec/ExpectActual:
- 'spec/cho/work/import/routing_spec.rb'
- 'spec/cho/work/submissions/routing_spec.rb'

# Offense count: 1
# Offense count: 3
RSpec/LetSetup:
Exclude:
- 'spec/cho/controlled_vocabulary/collections_spec.rb'
Expand All @@ -45,6 +45,14 @@ RSpec/NestedGroups:
- 'spec/cho/shared/item_factory_spec.rb'
- 'spec/cho/work/submissions/change_set_spec.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: Whitelist.
# Whitelist: find_by_sql
Rails/DynamicFindBy:
Exclude:
- 'app/cho/validation/member.rb'

# Offense count: 1
Rails/FilePath:
Exclude:
Expand Down
7 changes: 7 additions & 0 deletions app/blacklight/solr_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class SolrDocument
# csv extension
use_extension(Document::Csv)

# @note Override {::find} to retrieve documents using alternate identifiers
def self.find(id)
solr_response = repository.search(fq: "id:#{id} OR alternate_ids_ssim:id-#{id}")
raise Blacklight::Exceptions::RecordNotFound if solr_response.documents.empty?
solr_response.documents.first
end

def internal_resource
internal_resource_name.constantize
end
Expand Down
21 changes: 17 additions & 4 deletions app/cho/validation/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,23 @@ def initialize(id)
end

def exists?
Valkyrie.config.metadata_adapter.query_service.find_by(id: id)
true
rescue Valkyrie::Persistence::ObjectNotFoundError
false
exists_by_id? || exists_by_alternate_id?
end

private

def exists_by_id?
Valkyrie.config.metadata_adapter.query_service.find_by(id: id)
true
rescue Valkyrie::Persistence::ObjectNotFoundError
false
end

def exists_by_alternate_id?
Valkyrie.config.metadata_adapter.query_service.find_by_alternate_identifier(alternate_identifier: id)
true
rescue Valkyrie::Persistence::ObjectNotFoundError
false
end
end
end
34 changes: 33 additions & 1 deletion spec/blacklight/solr_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

RSpec.describe SolrDocument, type: :model do
before(:all) do
class MyResource; end
class MyResource < Valkyrie::Resource
attribute :alternate_ids, Valkyrie::Types::Array
end
end

after(:all) do
Expand Down Expand Up @@ -133,4 +135,34 @@ class MyResource; end
its(:label) { is_expected.to eq('Collection') }
end
end

describe '::find' do
before do
Valkyrie::MetadataAdapter.find(:index_solr).persister.save(resource: sample_resource)
end

context 'with an id' do
subject { described_class.find('abc1234') }

let(:sample_resource) { MyResource.new(id: Valkyrie::ID.new('abc1234')) }

its(:id) { is_expected.to eq('abc1234') }
end

context 'with an alternate id' do
subject { described_class.find('pdq6789') }

let(:sample_resource) { MyResource.new(id: Valkyrie::ID.new('abc1234'), alternate_ids: ['id-pdq6789']) }

its(:id) { is_expected.to eq('abc1234') }
end

context 'with a non-existent id' do
let(:sample_resource) { MyResource.new }

it 'raises an error' do
expect { described_class.find('nothere') }.to raise_error(Blacklight::Exceptions::RecordNotFound)
end
end
end
end
29 changes: 29 additions & 0 deletions spec/cho/validation/member_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Validation::Member, type: :model do
describe '#exists?' do
context 'with an id' do
subject { described_class.new(collection.id.to_s) }

let(:collection) { create(:archival_collection) }

its(:exists?) { is_expected.to be(true) }
end

context 'with an alternate id' do
subject { described_class.new(collection.alternate_ids.first.to_s) }

let(:collection) { create(:archival_collection, alternate_ids: ['xyz_1234']) }

its(:exists?) { is_expected.to be(true) }
end

context 'with neither a non-existent id' do
subject { described_class.new('non-existent-id') }

its(:exists?) { is_expected.to be(false) }
end
end
end
6 changes: 3 additions & 3 deletions spec/cho/work/import/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_helper'

RSpec.describe 'Preview of CSV Import', type: :feature do
let(:collection) { create :library_collection, title: 'my collection' }
let!(:collection) { create :library_collection, title: 'my collection', alternate_ids: ['xyz_1234'] }

let(:bag) do
ImportFactory::Bag.create(
Expand All @@ -20,7 +20,7 @@
let(:csv_file) do
CsvFactory::Generic.new(
alternate_ids: ['work1', 'work2', 'work3'],
member_of_collection_ids: [collection.id, collection.id, collection.id],
member_of_collection_ids: ['xyz_1234', 'xyz_1234', collection.id],
work_type: ['Generic', 'Generic', 'Generic'],
title: ['My Work 1', 'My Work 2', 'My Work 3'],
batch_id: ['batch1_2018-07-12', 'batch1_2018-07-12', 'batch1_2018-07-12']
Expand All @@ -31,7 +31,7 @@
ImportFactory::Zip.create(bag)
end

it 'successfully imports the csv' do
it 'successfully imports the csv using both ids and alternate ids' do
visit(csv_create_path)
expect(page).to have_selector('h1', text: 'CSV Import')
attach_file('work_import_csv_file_file', csv_file.path)
Expand Down

0 comments on commit 4a170c0

Please sign in to comment.