-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4910 from samvera/fix/3820_AF_where
replace ActiveFedora where with Valkyrized where
- Loading branch information
Showing
6 changed files
with
148 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
module Hyrax | ||
# Methods in this class search solr to get the ids and then use the query service to find the objects. | ||
class FindObjectsViaSolrService | ||
class_attribute :solr_query_builder, :solr_service, :query_service | ||
self.solr_query_builder = Hyrax::SolrQueryBuilderService | ||
self.solr_service = Hyrax::SolrService | ||
self.query_service = Hyrax.query_service | ||
|
||
class << self | ||
# Find objects matching search criteria. | ||
# @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 [Array<ActiveFedora|Valkyrie::Resource>] 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) | ||
query_service.find_many_by_ids(ids: ids).to_a | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
require 'spec_helper' | ||
|
||
RSpec.describe Hyrax::FindObjectsViaSolrService do | ||
describe ".find_for_model_by_field_pairs", clean_repo: true do | ||
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) } | ||
|
||
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 | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters