diff --git a/app/services/hyrax/find_objects_via_solr_service.rb b/app/services/hyrax/find_objects_via_solr_service.rb index 38c7be1434..21045fc0cf 100644 --- a/app/services/hyrax/find_objects_via_solr_service.rb +++ b/app/services/hyrax/find_objects_via_solr_service.rb @@ -9,18 +9,18 @@ class FindObjectsViaSolrService class << self # Find objects matching search criteria. - # @param model [Class] + # @param model [Class] if not using Valkyrie, this is expected to be an ActiveFedora::Base object that supports #where # @param field_pairs [Hash] a list of pairs of property name and values # @param join_with [String] the value we're joining the clauses with (default: ' OR ' for backward compatibility with ActiveFedora where) # @param type [String] The type of query to run. Either 'raw' or 'field' (default: 'field') # @param use_valkyrie [Boolean] if true, return Valkyrie resource(s); otherwise, return ActiveFedora object(s) - # @return [ActiveFedora | Valkyrie::Resource] objects matching the query + # @return [Array] objects matching the query def find_for_model_by_field_pairs(model:, field_pairs:, join_with: ' OR ', type: 'field', use_valkyrie: Hyrax.config.use_valkyrie?) + return model.where(field_pairs).to_a unless use_valkyrie query = solr_query_builder.construct_query_for_model(model, field_pairs, join_with, type) results = solr_service.query(query) ids = results.map(&:id) - return query_service.find_many_by_ids(ids: ids) if use_valkyrie - ids.map { |id| query_service.find_by_alternate_identifier(alternate_identifier: id.to_str, use_valkyrie: false) } + query_service.find_many_by_ids(ids: ids).to_a end end end diff --git a/spec/services/hyrax/find_objects_via_solr_service_spec.rb b/spec/services/hyrax/find_objects_via_solr_service_spec.rb index 6e3afedade..69e88ef416 100644 --- a/spec/services/hyrax/find_objects_via_solr_service_spec.rb +++ b/spec/services/hyrax/find_objects_via_solr_service_spec.rb @@ -3,18 +3,42 @@ RSpec.describe Hyrax::FindObjectsViaSolrService do describe ".find_for_model_by_field_pairs", clean_repo: true do - let(:collection1) { valkyrie_create(:hyrax_collection, title: ['Foo']) } - let(:collection2) { valkyrie_create(:hyrax_collection, title: ['Too']) } let(:collection_ids) { [collection1.id, collection2.id] } let(:field_pairs) do { id: collection_ids.map(&:to_s) } end + subject(:results) { described_class.find_for_model_by_field_pairs(model: collection1.class, field_pairs: field_pairs, use_valkyrie: use_valkyrie) } - it "returns ActiveFedora objects matching the query" do - expect(described_class.find_for_model_by_field_pairs(model: ::Collection, field_pairs: field_pairs, use_valkyrie: false).map(&:title)).to match_array [['Foo'], ['Too']] + context "when use_valkyrie is false" do + let(:use_valkyrie) { false } + let(:collection1) { create(:collection_lw, title: ['Foo']) } + let(:collection2) { create(:collection_lw, title: ['Too']) } + it "returns ActiveFedora objects matching the query" do + expect(results).to be_kind_of Array + expect(results.map(&:title)).to match_array [['Foo'], ['Too']] + end end - it "returns Valkyrie::Resources matching the query" do - expect(described_class.find_for_model_by_field_pairs(model: ::Collection, field_pairs: field_pairs, use_valkyrie: true).map(&:title)).to match_array [['Foo'], ['Too']] + + context "when use_valkyrie is true" do + let(:use_valkyrie) { true } + context "and objects were created with ActiveFedora" do + let(:collection1) { create(:collection_lw, title: ['Foo']) } + let(:collection2) { create(:collection_lw, title: ['Too']) } + + it "returns Valkyrie::Resources matching the query" do + expect(results).to be_kind_of Array + expect(results.map(&:title)).to match_array [['Foo'], ['Too']] + end + end + context "and objects were created with Valkryie" do + let(:collection1) { valkyrie_create(:hyrax_collection, title: ['Foo']) } + let(:collection2) { valkyrie_create(:hyrax_collection, title: ['Too']) } + + it "returns Valkyrie::Resources matching the query" do + expect(results).to be_kind_of Array + expect(results.map(&:title)).to match_array [['Foo'], ['Too']] + end + end end end end