-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
replace ActiveFedora where with Valkyrized where #4910
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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] | ||
# @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 | ||
def find_for_model_by_field_pairs(model:, field_pairs:, join_with: ' OR ', type: 'field', use_valkyrie: Hyrax.config.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) } | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,11 +53,15 @@ def check(collection_ids:, include_current_members: false) | |
def single_membership_collections(collection_ids) | ||
return [] if collection_ids.blank? | ||
|
||
::Collection.where(:id => collection_ids, Hyrax.config.collection_type_index_field.to_sym => collection_type_gids_that_disallow_multiple_membership) | ||
field_pairs = { | ||
:id => collection_ids, | ||
Hyrax.config.collection_type_index_field.to_sym => collection_type_gids_that_disallow_multiple_membership | ||
} | ||
Hyrax::FindObjectsViaSolrService.find_for_model_by_field_pairs(model: ::Collection, field_pairs: field_pairs, use_valkyrie: true) | ||
end | ||
|
||
def collection_type_gids_that_disallow_multiple_membership | ||
Hyrax::CollectionType.gids_that_do_not_allow_multiple_membership | ||
Hyrax::CollectionType.gids_that_do_not_allow_multiple_membership.map(&:to_s) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's going on here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests initially failed when refactoring for the new approach which required this change. But the final refactor really doesn't need this. So I set it back to what it was. |
||
end | ||
|
||
def build_error_message(problematic_collections) | ||
|
@@ -74,7 +78,7 @@ def collection_type_title_from_gid(gid) | |
end | ||
|
||
def collection_titles_from_list(collection_list) | ||
collection_list.each do |collection| | ||
collection_list.map do |collection| | ||
collection.title.first | ||
end.to_sentence | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
require 'spec_helper' | ||
|
||
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 | ||
|
||
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']] | ||
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']] | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the advantage of using the valkyrie query service?
would there be fewer risks if we stuck with the the existing
ActiveFedora::Base#where
implementation in the case where what we want is to query AF objects?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated to use AF where unless use_valkyrie is true.