From d754171a571b3c6e89d9db4af8c33b0bb54b3333 Mon Sep 17 00:00:00 2001 From: Eliot Jordan Date: Tue, 15 Mar 2022 12:19:21 -0500 Subject: [PATCH] Enable PcdmMemberPresenterFactory to work with ActiveFedora --- app/indexers/hyrax/file_set_indexer.rb | 1 + app/indexers/hyrax/work_indexer.rb | 1 + .../hyrax/pcdm_member_presenter_factory.rb | 4 +- .../hyrax/generic_work_indexer_spec.rb | 1 + .../pcdm_member_presenter_factory_spec.rb | 209 +++++++++++++----- 5 files changed, 157 insertions(+), 59 deletions(-) diff --git a/app/indexers/hyrax/file_set_indexer.rb b/app/indexers/hyrax/file_set_indexer.rb index 6a36bcc708..5c02ea4cc9 100644 --- a/app/indexers/hyrax/file_set_indexer.rb +++ b/app/indexers/hyrax/file_set_indexer.rb @@ -30,6 +30,7 @@ def generate_solr_document # rubocop:disable Metrics/AbcSize, Metrics/MethodLeng solr_doc['original_checksum_tesim'] = object.original_checksum solr_doc['alpha_channels_ssi'] = object.alpha_channels solr_doc['original_file_id_ssi'] = original_file_id + solr_doc['generic_type_si'] = 'FileSet' end end diff --git a/app/indexers/hyrax/work_indexer.rb b/app/indexers/hyrax/work_indexer.rb index 4c072b33c6..3937a6641a 100644 --- a/app/indexers/hyrax/work_indexer.rb +++ b/app/indexers/hyrax/work_indexer.rb @@ -11,6 +11,7 @@ def generate_solr_document solr_doc['member_of_collections_ssim'] = object.member_of_collections.map(&:first_title) solr_doc['member_of_collection_ids_ssim'] = object.member_of_collections.map(&:id) solr_doc['generic_type_sim'] = ['Work'] + solr_doc['generic_type_si'] = 'Work' # This enables us to return a Work when we have a FileSet that matches # the search query. While at the same time allowing us not to return Collections diff --git a/app/presenters/hyrax/pcdm_member_presenter_factory.rb b/app/presenters/hyrax/pcdm_member_presenter_factory.rb index da4508f26f..2997c3465e 100644 --- a/app/presenters/hyrax/pcdm_member_presenter_factory.rb +++ b/app/presenters/hyrax/pcdm_member_presenter_factory.rb @@ -99,6 +99,8 @@ def presenter_for(document:, ability:) case document['has_model_ssim'].first when Hyrax::FileSet.name Hyrax::FileSetPresenter.new(document, ability) + when ::FileSet.name + Hyrax::FileSetPresenter.new(document, ability) else Hyrax::WorkShowPresenter.new(document, ability) end @@ -111,7 +113,7 @@ def query_docs(generic_type: nil, ids: object.member_ids) query += "{!term f=generic_type_si}#{generic_type}" if generic_type Hyrax::SolrService - .post(query, rows: 10_000) + .post(q: query, rows: 10_000) .fetch('response') .fetch('docs') end diff --git a/spec/indexers/hyrax/generic_work_indexer_spec.rb b/spec/indexers/hyrax/generic_work_indexer_spec.rb index 556a12067d..c32b6b15a6 100644 --- a/spec/indexers/hyrax/generic_work_indexer_spec.rb +++ b/spec/indexers/hyrax/generic_work_indexer_spec.rb @@ -35,6 +35,7 @@ it 'indexes member work and file_set ids' do expect(solr_document['member_ids_ssim']).to eq work.member_ids expect(solr_document['generic_type_sim']).to eq ['Work'] + expect(solr_document['generic_type_si']).to eq 'Work' expect(solr_document.fetch('thumbnail_path_ss')).to eq "/downloads/#{file.id}?file=thumbnail" expect(subject.fetch('hasRelatedImage_ssim').first).to eq file.id expect(subject.fetch('hasRelatedMediaFragment_ssim').first).to eq file.id diff --git a/spec/presenters/hyrax/pcdm_member_presenter_factory_spec.rb b/spec/presenters/hyrax/pcdm_member_presenter_factory_spec.rb index e113c6c526..d87301cf14 100644 --- a/spec/presenters/hyrax/pcdm_member_presenter_factory_spec.rb +++ b/spec/presenters/hyrax/pcdm_member_presenter_factory_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe Hyrax::PcdmMemberPresenterFactory, index_adapter: :solr_index, valkyrie_adapter: :test_adapter do +RSpec.describe Hyrax::PcdmMemberPresenterFactory do subject(:factory) { described_class.new(solr_doc, ability) } let(:ability) { :FAKE_ABILITY } let(:ids) { [] } @@ -9,8 +9,7 @@ RSpec::Matchers.define :be_presenter_for do |expected| match do |actual| actual.id == expected.id && - (actual.solr_document['has_model_ssim'].first == - expected.class.name) + actual.model_name.name == expected.model_name.name end end @@ -33,92 +32,186 @@ end end - describe '#file_set_presenters' do - it 'is empty' do - expect(factory.file_set_presenters.to_a).to be_empty + context 'with ActiveFedora index adapter' do + describe '#file_set_presenters' do + it 'is empty' do + expect(factory.file_set_presenters.to_a).to be_empty + end + + context 'with members' do + include_context 'with members' + + it 'builds only file_set presenters' do + expect(factory.file_set_presenters) + .to contain_exactly(*file_sets.map { |fs| be_presenter_for(fs) }) + end + + it 'gives members in order' do + expect(factory.file_set_presenters.map(&:id)).to eq file_sets.map(&:id) + end + end end - context 'with members' do - include_context 'with members' + describe '#member_presenters' do + it 'is empty' do + expect(factory.member_presenters.to_a).to be_empty + end - it 'builds only file_set presenters' do - expect(factory.file_set_presenters) - .to contain_exactly(*file_sets.map { |fs| be_presenter_for(fs) }) + it 'accepts bespoke member ids' do + fs = FactoryBot.valkyrie_create(:hyrax_file_set) + Hyrax.index_adapter.save(resource: fs) + + expect(factory.member_presenters([fs.id]).to_a) + .to contain_exactly(be_presenter_for(fs)) end - it 'gives members in order' do - expect(factory.file_set_presenters.map(&:id)).to eq file_sets.map(&:id) + it 'raises an error if given an unindexed id' do + expect { factory.member_presenters(['FAKE_ID']).to_a } + .to raise_error ArgumentError end - end - end - describe '#member_presenters' do - it 'is empty' do - expect(factory.member_presenters.to_a).to be_empty - end + context 'with members' do + include_context 'with members' - it 'accepts bespoke member ids' do - fs = FactoryBot.valkyrie_create(:hyrax_file_set) - Hyrax.index_adapter.save(resource: fs) + it 'builds all member presenters' do + members = file_sets + works - expect(factory.member_presenters([fs.id]).to_a) - .to contain_exactly(be_presenter_for(fs)) - end + expect(factory.member_presenters) + .to contain_exactly(*members.map { |fs| be_presenter_for(fs) }) + end - it 'raises an error if given an unindexed id' do - expect { factory.member_presenters(['FAKE_ID']).to_a } - .to raise_error ArgumentError + it 'builds member presenters with appropriate classes' do + expect(factory.member_presenters) + .to contain_exactly(an_instance_of(Hyrax::WorkShowPresenter), + an_instance_of(Hyrax::WorkShowPresenter), + an_instance_of(Hyrax::FileSetPresenter), + an_instance_of(Hyrax::FileSetPresenter)) + end + + it 'gives members in order' do + expect(factory.member_presenters.map(&:id)).to eq ids + end + end end - context 'with members' do - include_context 'with members' + describe '#ordered_ids' do + its(:ordered_ids) { is_expected.to eq ids } - it 'builds all member presenters' do - members = file_sets + works + context 'with members' do + include_context 'with members' - expect(factory.member_presenters) - .to contain_exactly(*members.map { |fs| be_presenter_for(fs) }) + its(:ordered_ids) { is_expected.to eq ids } end + end - it 'builds member presenters with appropriate classes' do - expect(factory.member_presenters) - .to contain_exactly(an_instance_of(Hyrax::WorkShowPresenter), - an_instance_of(Hyrax::WorkShowPresenter), - an_instance_of(Hyrax::FileSetPresenter), - an_instance_of(Hyrax::FileSetPresenter)) + describe '#work_presenters' do + it 'is empty' do + expect(factory.work_presenters.to_a).to be_empty end - it 'gives members in order' do - expect(factory.member_presenters.map(&:id)).to eq ids + context 'with members' do + include_context 'with members' + + it 'builds only work presenters' do + expect(factory.work_presenters) + .to contain_exactly(*works.map { |fs| be_presenter_for(fs) }) + end + + it 'gives members in order' do + expect(factory.work_presenters.map(&:id)).to eq works.map(&:id) + end end end end - describe '#ordered_ids' do - its(:ordered_ids) { is_expected.to eq ids } + context 'with Valkyrie index adapter', index_adapter: :solr_index, valkyrie_adapter: :test_adapter do + describe '#file_set_presenters' do + it 'is empty' do + expect(factory.file_set_presenters.to_a).to be_empty + end - context 'with members' do - include_context 'with members' + context 'with members' do + include_context 'with members' - its(:ordered_ids) { is_expected.to eq ids } + it 'builds only file_set presenters' do + expect(factory.file_set_presenters) + .to contain_exactly(*file_sets.map { |fs| be_presenter_for(fs) }) + end + + it 'gives members in order' do + expect(factory.file_set_presenters.map(&:id)).to eq file_sets.map(&:id) + end + end end - end - describe '#work_presenters' do - it 'is empty' do - expect(factory.work_presenters.to_a).to be_empty + describe '#member_presenters' do + it 'is empty' do + expect(factory.member_presenters.to_a).to be_empty + end + + it 'accepts bespoke member ids' do + fs = FactoryBot.valkyrie_create(:hyrax_file_set) + Hyrax.index_adapter.save(resource: fs) + + expect(factory.member_presenters([fs.id]).to_a) + .to contain_exactly(be_presenter_for(fs)) + end + + it 'raises an error if given an unindexed id' do + expect { factory.member_presenters(['FAKE_ID']).to_a } + .to raise_error ArgumentError + end + + context 'with members' do + include_context 'with members' + + it 'builds all member presenters' do + members = file_sets + works + + expect(factory.member_presenters) + .to contain_exactly(*members.map { |fs| be_presenter_for(fs) }) + end + + it 'builds member presenters with appropriate classes' do + expect(factory.member_presenters) + .to contain_exactly(an_instance_of(Hyrax::WorkShowPresenter), + an_instance_of(Hyrax::WorkShowPresenter), + an_instance_of(Hyrax::FileSetPresenter), + an_instance_of(Hyrax::FileSetPresenter)) + end + + it 'gives members in order' do + expect(factory.member_presenters.map(&:id)).to eq ids + end + end end - context 'with members' do - include_context 'with members' + describe '#ordered_ids' do + its(:ordered_ids) { is_expected.to eq ids } + + context 'with members' do + include_context 'with members' - it 'builds only work presenters' do - expect(factory.work_presenters) - .to contain_exactly(*works.map { |fs| be_presenter_for(fs) }) + its(:ordered_ids) { is_expected.to eq ids } end + end + + describe '#work_presenters' do + it 'is empty' do + expect(factory.work_presenters.to_a).to be_empty + end + + context 'with members' do + include_context 'with members' + + it 'builds only work presenters' do + expect(factory.work_presenters) + .to contain_exactly(*works.map { |fs| be_presenter_for(fs) }) + end - it 'gives members in order' do - expect(factory.work_presenters.map(&:id)).to eq works.map(&:id) + it 'gives members in order' do + expect(factory.work_presenters.map(&:id)).to eq works.map(&:id) + end end end end