Skip to content

Commit

Permalink
Restore ability for collections to be direct as well as indirect pare…
Browse files Browse the repository at this point in the history
…nts and children
  • Loading branch information
cjcolvar committed Sep 28, 2022
1 parent a39da92 commit 78a6098
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ class NestedCollectionsSearchBuilder < ::Hyrax::CollectionSearchBuilder
# @param access [Symbol] :edit, :read, :discover - With the given :access what all can
# @param collection [::Collection]
# @param scope [Object] Typically a controller that responds to #current_ability, #blackligh_config
def initialize(access:, collection:, scope:)
# @param nest_direction [Symbol] (:as_parent or :as_child) the direction we are adding nesting to this collection
def initialize(access:, collection:, scope:, nest_direction:)
super(scope)
@collection = collection
@discovery_permissions = extract_discovery_permissions(access)
@nest_direction = nest_direction
end

# Override for Hydra::AccessControlsEnforcement
Expand All @@ -27,8 +29,8 @@ def show_only_other_collections_of_the_same_collection_type(solr_parameters)
solr_parameters[:fq] ||= []
solr_parameters[:fq] += [
Hyrax::SolrQueryBuilderService.construct_query(Hyrax.config.collection_type_index_field => @collection.collection_type_gid),
"-{!graph from=id to=member_of_collection_ids_ssim}id:#{@collection.id}",
"-{!graph to=id from=member_of_collection_ids_ssim}id:#{@collection.id}"
"-{!graph from=id to=member_of_collection_ids_ssim#{' maxDepth=1' if @nest_direction == :as_parent}}id:#{@collection.id}",
"-{!graph to=id from=member_of_collection_ids_ssim#{' maxDepth=1' if @nest_direction == :as_child}}id:#{@collection.id}"
]
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module NestedCollectionQueryService
def self.available_child_collections(parent:, scope:, limit_to_id: nil)
return [] unless nestable?(collection: parent)
return [] unless scope.can?(:deposit, parent)
query_solr(collection: parent, access: :read, scope: scope, limit_to_id: limit_to_id).documents
query_solr(collection: parent, access: :read, scope: scope, limit_to_id: limit_to_id, nest_direction: :as_child).documents
end

##
Expand All @@ -34,7 +34,7 @@ def self.available_child_collections(parent:, scope:, limit_to_id: nil)
def self.available_parent_collections(child:, scope:, limit_to_id: nil)
return [] unless nestable?(collection: child)
return [] unless scope.can?(:read, child)
query_solr(collection: child, access: :deposit, scope: scope, limit_to_id: limit_to_id).documents
query_solr(collection: child, access: :deposit, scope: scope, limit_to_id: limit_to_id, nest_direction: :as_parent).documents
end

##
Expand Down Expand Up @@ -63,11 +63,13 @@ def self.parent_collections(child:, scope:, page: 1)
# to +repository+, +can?+, +blacklight_config+, +current_ability+
# @param limit_to_id [nil, String] Limit the query to just check if the given
# id is in the response. Useful for validation.
def self.query_solr(collection:, access:, scope:, limit_to_id:)
# @param nest_direction [Symbol] :as_child or :as_parent
def self.query_solr(collection:, access:, scope:, limit_to_id:, nest_direction:)
query_builder = Hyrax::Dashboard::NestedCollectionsSearchBuilder.new(
access: access,
collection: collection,
scope: scope
scope: scope,
nest_direction: nest_direction
)

query_builder.where(id: limit_to_id.to_s) if limit_to_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[false, true].each do |test_valkyrie|
context "when test_valkyrie is #{test_valkyrie}" do
let(:builder) do
described_class.new(scope: scope, access: access, collection: collection)
described_class.new(scope: scope, access: access, collection: collection, nest_direction: test_nest_direction)
end
let(:collection_id) { collection.id.to_s }

Expand Down Expand Up @@ -45,13 +45,28 @@

subject { builder.show_only_other_collections_of_the_same_collection_type(solr_params) }

it 'will exclude the given collection and its parents and children' do
subject
expect(solr_params.fetch(:fq)).to contain_exactly(
"_query_:\"{!field f=collection_type_gid_ssim}#{collection.collection_type_gid}\"",
"-{!graph to=id from=member_of_collection_ids_ssim}id:#{collection.id}",
"-{!graph from=id to=member_of_collection_ids_ssim}id:#{collection.id}"
)
context 'when nesting :as_parent' do
it 'will exclude the given collection, its parents, and direct children' do
subject
expect(solr_params.fetch(:fq)).to contain_exactly(
"_query_:\"{!field f=collection_type_gid_ssim}#{collection.collection_type_gid}\"",
"-{!graph to=id from=member_of_collection_ids_ssim}id:#{collection.id}",
"-{!graph from=id to=member_of_collection_ids_ssim maxDepth=1}id:#{collection.id}"
)
end
end

context 'when nesting :as_child' do
let(:test_nest_direction) { :as_child }

it 'will exclude the given collection, its children, and direct parents' do
subject
expect(solr_params.fetch(:fq)).to contain_exactly(
"_query_:\"{!field f=collection_type_gid_ssim}#{collection.collection_type_gid}\"",
"-{!graph to=id from=member_of_collection_ids_ssim maxDepth=1}id:#{collection.id}",
"-{!graph from=id to=member_of_collection_ids_ssim}id:#{collection.id}"
)
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
describe 'it prevents circular nesting' do
it 'returns an array of valid collections of the same collection type' do
expect(scope).to receive(:can?).with(:deposit, coll_c).and_return(true)
expect(described_class).to receive(:query_solr).with(collection: coll_c, access: :read, scope: scope, limit_to_id: nil).and_call_original
expect(subject.map(&:id)).to contain_exactly(another.id)
expect(described_class).to receive(:query_solr).with(collection: coll_c, access: :read, scope: scope, limit_to_id: nil, nest_direction: :as_child).and_call_original
expect(subject.map(&:id)).to contain_exactly(another.id, coll_e.id)
end
end
end
Expand Down Expand Up @@ -175,8 +175,8 @@
describe 'it prevents circular nesting' do
it 'returns an array of collections of the same collection type excluding the given collection' do
expect(scope).to receive(:can?).with(:read, coll_c).and_return(true)
expect(described_class).to receive(:query_solr).with(collection: coll_c, access: :deposit, scope: scope, limit_to_id: nil).and_call_original
expect(subject.map(&:id)).to contain_exactly(another.id)
expect(described_class).to receive(:query_solr).with(collection: coll_c, access: :deposit, scope: scope, limit_to_id: nil, nest_direction: :as_parent).and_call_original
expect(subject.map(&:id)).to contain_exactly(another.id, coll_a.id)
end
end
end
Expand Down

0 comments on commit 78a6098

Please sign in to comment.