app/jobs/bulkrax/create_relationships_job.rb in bulkrax-3.0.0.beta4 vs app/jobs/bulkrax/create_relationships_job.rb in bulkrax-3.0.0.beta5

- old
+ new

@@ -19,31 +19,31 @@ class CreateRelationshipsJob < ApplicationJob include DynamicRecordLookup queue_as :import - attr_accessor :child_records, :parent_record, :parent_entry, :importer_run_id + attr_accessor :child_records, :child_entry, :parent_record, :parent_entry, :importer_run_id # @param parent_identifier [String] Work/Collection ID or Bulkrax::Entry source_identifiers # @param importer_run [Bulkrax::ImporterRun] current importer run (needed to properly update counters) # # The entry_identifier is used to lookup the @base_entry for the job (a.k.a. the entry the job was called from). # The @base_entry defines the context of the relationship (e.g. "this entry (@base_entry) should have a parent"). # Whether the @base_entry is the parent or the child in the relationship is determined by the presence of a # parent_identifier or child_identifier param. For example, if a parent_identifier is passed, we know @base_entry # is the child in the relationship, and vice versa if a child_identifier is passed. - def perform(parent_identifier:, importer_run_id:) + def perform(parent_identifier:, importer_run_id:) # rubocop:disable Metrics/AbcSize pending_relationships = Bulkrax::PendingRelationship.find_each.select do |rel| rel.bulkrax_importer_run_id == importer_run_id && rel.parent_id == parent_identifier end.sort_by(&:order) @importer_run_id = importer_run_id @parent_entry, @parent_record = find_record(parent_identifier, importer_run_id) @child_records = { works: [], collections: [] } pending_relationships.each do |rel| raise ::StandardError, %("#{rel}" needs either a child or a parent to create a relationship) if rel.child_id.nil? || rel.parent_id.nil? - _, child_record = find_record(rel.child_id, importer_run_id) + @child_entry, child_record = find_record(rel.child_id, importer_run_id) child_record.is_a?(::Collection) ? @child_records[:collections] << child_record : @child_records[:works] << child_record end if (child_records[:collections].blank? && child_records[:works].blank?) || parent_record.blank? reschedule( @@ -57,11 +57,11 @@ importerexporter_id: ImporterRun.find(importer_run_id).importer_id, importerexporter_type: "Bulkrax::Importer").first create_relationships pending_relationships.each(&:destroy) rescue ::StandardError => e - parent_entry.status_info(e) + parent_entry ? parent_entry.status_info(e) : child_entry.status_info(e) Bulkrax::ImporterRun.find(importer_run_id).increment!(:failed_relationships) # rubocop:disable Rails/SkipsModelValidations end private @@ -85,12 +85,12 @@ child_records[:works].each do |child_record| attrs = { id: child_record.id, member_of_collections_attributes: { 0 => { id: parent_record.id } } } ObjectFactory.new( attributes: attrs, source_identifier_value: nil, # sending the :id in the attrs means the factory doesn't need a :source_identifier_value - work_identifier: parent_entry.parser.work_identifier, - related_parents_parsed_mapping: parent_entry.parser.related_parents_parsed_mapping, + work_identifier: parent_entry&.parser&.work_identifier, + related_parents_parsed_mapping: parent_entry&.parser&.related_parents_parsed_mapping, replace_files: false, user: user, klass: child_record.class, importer_run_id: importer_run_id ).run @@ -104,12 +104,12 @@ child_record = child_records[:collections].first attrs = { id: parent_record.id, child_collection_id: child_record.id } ObjectFactory.new( attributes: attrs, source_identifier_value: nil, # sending the :id in the attrs means the factory doesn't need a :source_identifier_value - work_identifier: parent_entry.parser.work_identifier, - related_parents_parsed_mapping: parent_entry.parser.related_parents_parsed_mapping, + work_identifier: parent_entry&.parser&.work_identifier, + related_parents_parsed_mapping: parent_entry&.parser&.related_parents_parsed_mapping, replace_files: false, user: user, klass: parent_record.class, importer_run_id: importer_run_id ).run @@ -128,11 +128,11 @@ work_members_attributes: records_hash } ObjectFactory.new( attributes: attrs, source_identifier_value: nil, # sending the :id in the attrs means the factory doesn't need a :source_identifier_value - work_identifier: parent_entry.parser.work_identifier, - related_parents_parsed_mapping: parent_entry.parser.related_parents_parsed_mapping, + work_identifier: parent_entry&.parser&.work_identifier, + related_parents_parsed_mapping: parent_entry&.parser&.related_parents_parsed_mapping, replace_files: false, user: user, klass: parent_record.class, importer_run_id: importer_run_id ).run