Module: Hyrax::Collections::NestedCollectionQueryService

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

Overview

A query service handling nested collection queries.

Defined Under Namespace

Classes: NestingAttributes

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:



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

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:



54
55
56
57
58
# File 'app/services/hyrax/collections/nested_collection_query_service.rb', line 54

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

.clean_lucene_error(builder:) ⇒ Blacklight::Solr::Request

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

clean query for {!lucene} error

Parameters:

Returns:

  • (Blacklight::Solr::Request)

    cleaned and functional query



111
112
113
114
115
116
117
118
119
120
# File 'app/services/hyrax/collections/nested_collection_query_service.rb', line 111

def self.clean_lucene_error(builder:)
  # TODO: Need to investigate further to understand why these particular queries
  # using the where cause fail when others in the app apparently work
  #
  # Perhaps see <https://github.com/projectblacklight/blacklight/blob/064302f73eee4baae4d2abf863c68317d3efb5b7/lib/blacklight/solr/search_builder_behavior.rb#L84-L102>.
  # This can be averted by using #with in at least some cases?
  query = builder.query.to_hash
  query['q'] = query['q'].gsub('{!lucene}', '') if query.key?('q')
  query
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



139
140
141
142
143
144
145
# File 'app/services/hyrax/collections/nested_collection_query_service.rb', line 139

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)


71
72
73
74
75
76
# File 'app/services/hyrax/collections/nested_collection_query_service.rb', line 71

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

.valid_combined_nesting_depth?(parent:, child: nil, scope:) ⇒ Boolean

Does the nesting depth fall within defined limit?

Parameters:

  • parent (::Collection)
  • child (nil, ::Collection) (defaults to: nil)

    will be nil if we are nesting a new collection under the parent

  • 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



158
159
160
161
162
163
164
# File 'app/services/hyrax/collections/nested_collection_query_service.rb', line 158

def self.valid_combined_nesting_depth?(parent:, child: nil, scope:)
  # We limit the total depth of collections to the size specified in the samvera-nesting_indexer configuration.
  child_depth = child_nesting_depth(child: child, scope: scope)
  parent_depth = parent_nesting_depth(parent: parent, scope: scope)
  return false if parent_depth + child_depth > Samvera::NestingIndexer.configuration.maximum_nesting_depth
  true
end