Relationships Development Pattern - samvera/hyrax GitHub Wiki
Back to Patterns
<NOTE: At this writing, all links are to code in Release 3.3.0.
Relationships
General Overview
NOTE: This is a developer oriented document. As such, it only describes how to maintain relationships in code.
The relationships:
- A Collection MAY be in one or more Collections (NOTE: The collection type of both MUST support nesting.)
- A Work MUST be in one and only one Admin Set (NOTE: This is a special type of Collection requiring Work membership.)
- A Work MAY be in one or more Collections (NOTE: Some collection types require a Work, if it is in a Collection of that type, to be in one and only one Collection of that type.)
- A Work, if it is in another work, MUST be in one and only one other Work
- A File Set, if a work has a file, MUST be in one and only one Work
- A File Set MUST contain one uploaded File and MAY contain multiple derivatives of that File
Navigating the Model
Works in an Admin Set
How to find all works in an admin set?
Admin Set:
- has no direct knowledge of works
- must search for the works using the inverse relationship
The following will find works in the admin set:
work_ids = Hyrax.query_service.find_inverse_references_by(resource: admin_set, property: admin_set_id).map(&:id)
works = Hyrax.query_service.find_inverse_references_by(resource: admin_set, property: admin_set_id)
How to find the admin set that a work is in?
Works: have a direct link to the admin set in attribute: :admin_set_id
The following will find a work's admin set:
admin_set_id = work.admin_set_id
admin_set = Hyrax.query_service.find_by(id: work.admin_set_id)
Collections in Collections
How to find child-collections?
Parent Collection:
- has no direct knowledge of children
- must search for the children using the inverse relationship
The following will find child-collections:
child_collection_ids = Hyrax.custom_queries.find_child_collection_ids(resource: parent_collection)
child_collections = Hyrax.custom_queries.find_child_collections(resource: parent_collection)
How to find parent-collections?
Children: have direct link to parent(s) in attribute: :member_of_collection_ids
The following will find parent-collections:
parent_collection_ids = Hyrax.custom_queries.find_parent_collection_ids(resource: child_collection)
parent_collections = Hyrax.custom_queries.find_parent_collections(resource: child_collection)
Works in Collections
How to check if a work is in a collection?
How to find all works in a collection? (child-works)
How to find all collections that a work is in? (parent-collections)
Works in a Work
How to check if a work is in a work?
How to find all works in a work? (child-works)
How to find a work that a work is in? (parent-work)
Files (and File Sets) in a Work
How to check if a file is in a work?
How to find all files in a work? (child-files)
How to find the work that a file is in? (parent-work)
Modifying Relationships
Works in an Admin Set
How to move a work to a different admin set?
When work is_a Hyrax::Work which is_a Valkyrie::Resource
work_resource.admin_set_id = admin_set_resource.id
work_resource = Hyrax.persister.save(resource: work_resource)
NOTE: The work will no longer be in the old Admin Set since it can belong to one and only one Admin Set.