Method: Valkyrie::Persistence::Fedora::QueryService#find_inverse_references_by

Defined in:
lib/valkyrie/persistence/fedora/query_service.rb

#find_inverse_references_by(resource: nil, id: nil, property:, model: nil) ⇒ Array<Valkyrie::Resource>

Get all resources which link to a resource with a given property. Find all resources referencing a given resource (e. g. parents) *This is done by iterating through the ID of each resource referencing the resource in the query, and requesting each resource over the HTTP* *Also, an initial request is made to find the URIs of the resources referencing the resource in the query*

Parameters:

  • resource (Valkyrie::Resource) (defaults to: nil)

    The resource which is being referenced by other resources. Requires either resource or id parameter to be specified.

  • id (Valkyrie::ID) (defaults to: nil)

    ID of the resource which is being reference by other resources. Requires either resource or id parameter to be specified.

  • property (Symbol)

    The property which, on other resources, is referencing the given ‘resource`

  • model (Class) (defaults to: nil)

    Filter results to include only instances of this model. (optional)

Returns:

  • (Array<Valkyrie::Resource>)

    All resources in the persistence backend which have the ID of the given ‘resource` in their `property` property. Not in order.

Raises:

  • (ArgumentError)

    Raised when the ID is not in the persistence backend.



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 119

def find_inverse_references_by(resource: nil, id: nil, property:, model: nil)
  raise ArgumentError, "Provide resource or id" unless resource || id
  ensure_persisted(resource) if resource
  resource ||= find_by(id: id)
  ids = find_inverse_reference_ids_by_unordered(resource: resource, property: property).uniq
  objects_from_unordered = ids.lazy.map { |ref_id| find_by(id: ref_id) }
  objects_from_ordered = find_inverse_references_by_ordered(resource: resource, property: property, ignore_ids: ids)
  objects = [objects_from_unordered, objects_from_ordered].lazy.flat_map(&:lazy)
  return objects unless model
  objects.select { |obj| obj.is_a?(model) }
end