Module: Hyrax::Collections::NestedCollectionQueryService

Defined in:
app/services/hyrax/collections/nested_collection_query_service.rb

Overview

A query service handling nested collection queries.

Class Method Summary collapse

Class Method Details

.available_child_collections(parent:, scope:, limit_to_id: nil) ⇒ Array<SolrDocument>

What possible collections can be nested within the given parent collection?

Parameters:

  • parent (::Collection)
  • scope (Object)

    Typically a controller object that responds to ‘repository`, `can?`, `blacklight_config`, `current_ability`

  • limit_to_id (nil, String) (defaults to: nil)

    Limit the query to just check if the given id is in the response. Useful for validation.

Returns:



16
17
18
19
20
# File 'app/services/hyrax/collections/nested_collection_query_service.rb', line 16

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, nest_direction: :as_child).documents
end

.available_parent_collections(child:, scope:, limit_to_id: nil) ⇒ Array<SolrDocument>

What possible collections can the given child be nested within?

Parameters:

  • child (::Collection)
  • scope (Object)

    Typically a controller object that responds to repository, can?, blacklight_config, current_ability

  • limit_to_id (nil, String) (defaults to: nil)

    Limit the query to just check if the given id is in the response. Useful for validation.

Returns:



34
35
36
37
38
# File 'app/services/hyrax/collections/nested_collection_query_service.rb', line 34

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, nest_direction: :as_parent).documents
end

.parent_and_child_can_nest?(parent:, child:, scope:) ⇒ Boolean

TODO:

Consider expanding from same collection type to a lookup table that says “This collection type can have within it, these collection types”

Note:

There is a short-circuit of logic; To be robust, we should ensure that the child and parent are in the corresponding available collections

Is it valid to nest the given child within the given parent?

Parameters:

  • parent (::Collection)
  • child (::Collection)
  • scope (Object)

    Typically a controller object that responds to repository, can?, blacklight_config, current_ability

Returns:

  • (Boolean)

    true if the parent can nest the child; false otherwise



97
98
99
100
101
102
103
# File 'app/services/hyrax/collections/nested_collection_query_service.rb', line 97

def self.parent_and_child_can_nest?(parent:, child:, scope:)
  return false if parent == child # Short-circuit
  return false unless parent.collection_type_gid == child.collection_type_gid
  return false if available_parent_collections(child: child, scope: scope, limit_to_id: parent.id.to_s).none?
  return false if available_child_collections(parent: parent, scope: scope, limit_to_id: child.id.to_s).none?
  true
end

.parent_collections(child:, scope:, page: 1) ⇒ Blacklight::Solr::Response

What collections is the given child nested within?

Parameters:

  • child (::Collection)
  • scope (Object)

    Typically a controller object that responds to repository, can?, blacklight_config, current_ability

  • page (Integer) (defaults to: 1)

    Starting page for pagination

Returns:

  • (Blacklight::Solr::Response)


51
52
53
54
55
# File 'app/services/hyrax/collections/nested_collection_query_service.rb', line 51

def self.parent_collections(child:, scope:, page: 1)
  return [] unless nestable?(collection: child)
  query_builder = Hyrax::NestedCollectionsParentSearchBuilder.new(scope: scope, child: child, page: page)
  scope.blacklight_config.repository.search(query_builder.query)
end