Class: Hyrax::CustomQueries::Navigators::ParentWorkNavigator
- Inherits:
-
Object
- Object
- Hyrax::CustomQueries::Navigators::ParentWorkNavigator
- Defined in:
- app/services/hyrax/custom_queries/navigators/parent_work_navigator.rb
Overview
Navigate from a resource to it’s parent work.
Instance Attribute Summary collapse
- #query_service ⇒ Object readonly
Class Method Summary collapse
-
.queries ⇒ Object
Define the queries that can be fulfilled by this navigator.
Instance Method Summary collapse
-
#find_parent_work(resource:) ⇒ Array<Valkyrie::Resource>
Find parent work of a given resource, and map to Valkyrie Resources.
-
#find_parent_work_id(resource:) ⇒ Array<Valkyrie::ID>
Find the id of the parent work of a given resource, and map to Valkyrie Resources IDs.
-
#initialize(query_service:) ⇒ ParentWorkNavigator
constructor
A new instance of ParentWorkNavigator.
Constructor Details
#initialize(query_service:) ⇒ ParentWorkNavigator
Returns a new instance of ParentWorkNavigator.
19 20 21 |
# File 'app/services/hyrax/custom_queries/navigators/parent_work_navigator.rb', line 19 def initialize(query_service:) @query_service = query_service end |
Instance Attribute Details
#query_service ⇒ Object (readonly)
17 18 19 |
# File 'app/services/hyrax/custom_queries/navigators/parent_work_navigator.rb', line 17 def query_service @query_service end |
Class Method Details
.queries ⇒ Object
Define the queries that can be fulfilled by this navigator.
13 14 15 |
# File 'app/services/hyrax/custom_queries/navigators/parent_work_navigator.rb', line 13 def self.queries [:find_parent_work, :find_parent_work_id] end |
Instance Method Details
#find_parent_work(resource:) ⇒ Array<Valkyrie::Resource>
There should be only one parent resource. A warning is logged if more than one resource is found and the first of the resources is returned.
Find parent work of a given resource, and map to Valkyrie Resources
30 31 32 33 34 35 36 37 38 39 |
# File 'app/services/hyrax/custom_queries/navigators/parent_work_navigator.rb', line 30 def find_parent_work(resource:) results = Hyrax.query_service.find_inverse_references_by(resource: resource, property: :member_ids).select(&:work?) if results.count > 1 Hyrax.logger.warn("#{resource.work? ? 'Work' : 'File set'} " \ "#{resource.id} is in #{results.count} works when it " \ "should be in no more than one. Found in #{results.map(&:id).join(', ')}.") end results.first end |
#find_parent_work_id(resource:) ⇒ Array<Valkyrie::ID>
There should be only one parent resource. A warning is logged if more than one resource is found and the first of the resources is returned.
Find the id of the parent work of a given resource, and map to Valkyrie Resources IDs
48 49 50 |
# File 'app/services/hyrax/custom_queries/navigators/parent_work_navigator.rb', line 48 def find_parent_work_id(resource:) find_parent_work(resource: resource)&.id end |