Skip to content

Commit

Permalink
🧹 Make CreateRelationshipJob work for Valkyrie (#908)
Browse files Browse the repository at this point in the history
* 🧹 Make the relationships job work for Valkyrie

This will add a relationships path for Valkyrie objects.  It also will
add a transactions call so set child flag will fire off in IIIF Print.

ref:
  - scientist-softserv/hykuup_knapsack#141

* 💄 rubocop fix

Co-Authored-By: Kirk Wang <[email protected]>

* ♻️ Adjust rescue logic to move closer to error

This also adds some consideration for refactoring the queries to instead
use the persistence layer.

* Adding notes about transactions

---------

Co-authored-by: Shana Moore <[email protected]>
Co-authored-by: Jeremy Friesen <[email protected]>
  • Loading branch information
3 people authored Feb 2, 2024
1 parent afcfc3d commit 4844893
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
9 changes: 6 additions & 3 deletions app/factories/bulkrax/object_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,18 @@ def update
def find
found = find_by_id if attributes[:id].present?
return found if found.present?
rescue Valkyrie::Persistence::ObjectNotFoundError
return search_by_identifier if attributes[work_identifier].present?
false
ensure
search_by_identifier if attributes[work_identifier].present?
end

def find_by_id
# TODO: Push logic into Bulkrax.persistence_adapter; maybe
# Rails / Ruby upgrade, we moved from :exists? to :exist? However we want to continue (for a
# bit) to support older versions.
method_name = klass.respond_to?(:exist?) ? :exist? : :exists?
klass.find(attributes[:id]) if klass.send(method_name, attributes[:id])
rescue Valkyrie::Persistence::ObjectNotFoundError
false
end

def find_or_create
Expand All @@ -110,11 +111,13 @@ def find_or_create
end

def search_by_identifier
# TODO: Push logic into Bulkrax.persistence_adapter; maybe
query = { work_identifier_search_field =>
source_identifier_value }
# Query can return partial matches (something6 matches both something6 and something68)
# so we need to weed out any that are not the correct full match. But other items might be
# in the multivalued field, so we have to go through them one at a time.
#
match = klass.where(query).detect { |m| m.send(work_identifier).include?(source_identifier_value) }
return match if match
end
Expand Down
34 changes: 31 additions & 3 deletions app/jobs/bulkrax/create_relationships_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,15 @@ def perform(parent_identifier:, importer_run_id:) # rubocop:disable Metrics/AbcS
# save record if members were added
if @parent_record_members_added
parent_record.save!
# TODO: Push logic into Bulkrax.persistence_adapter
# Ensure that the new relationship gets indexed onto the children
@child_members_added.each(&:update_index)
if parent_record.is_a?(Valkyrie::Resource)
@child_members_added.each do |child|
Hyrax.index_adapter.save(resource: child)
end
else
@child_members_added.each(&:update_index)
end
end
end
else
Expand Down Expand Up @@ -165,13 +172,34 @@ def add_to_collection(child_record, parent_record)
end

def add_to_work(child_record, parent_record)
return true if parent_record.ordered_members.to_a.include?(child_record)
parent_record.is_a?(Valkyrie::Resource) ? add_to_valkyrie_work(child_record, parent_record) : add_to_af_work(child_record, parent_record)

parent_record.ordered_members << child_record
@parent_record_members_added = true
@child_members_added << child_record
end

def add_to_valkyrie_work(child_record, parent_record)
return true if parent_record.member_ids.include?(child_record.id)

parent_record.member_ids << child_record.id

# TODO: Hyrax is in the process of extracting an "Action" object that we could call. It does
# provide validation that we may want to consider.
#
# NOTE: We may need to look at the step args we're passing, see
# `Hyrax::WorksControllerBehavior#update_valkyrie_work`
# Hyrax's `./app/controllers/concerns/hyrax/works_controller_behavior.rb`
#
change_set = Hyrax::ChangeSet.for(parent_record)
Hyrax::Transactions::Container['change_set.update_work'].call(change_set)
end

def add_to_af_work(child_record, parent_record)
return true if parent_record.ordered_members.to_a.include?(child_record)

parent_record.ordered_members << child_record
end

def reschedule(parent_identifier:, importer_run_id:)
CreateRelationshipsJob.set(wait: 10.minutes).perform_later(
parent_identifier: parent_identifier,
Expand Down
2 changes: 1 addition & 1 deletion lib/bulkrax/persistence_layer/active_fedora_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ActiveFedoraAdapter < AbstractAdapter
def self.find(id)
ActiveFedora::Base.find(id)
rescue ActiveFedora::ObjectNotFoundError => e
raise PersistenceLayer::RecordNotFound, e.message
raise PersistenceLayer::ObjectNotFoundError, e.message
end

def self.query(q, **kwargs)
Expand Down

0 comments on commit 4844893

Please sign in to comment.