app/models/bulkrax/csv_entry.rb in bulkrax-2.2.4 vs app/models/bulkrax/csv_entry.rb in bulkrax-2.3.0

- old
+ new

@@ -1,11 +1,14 @@ # frozen_string_literal: true require 'csv' module Bulkrax - class CsvEntry < Entry + # TODO: We need to rework this class some to address the Metrics/ClassLength rubocop offense. + # We do too much in these entry classes. We need to extract the common logic from the various + # entry models into a module that can be shared between them. + class CsvEntry < Entry # rubocop:disable Metrics/ClassLength serialize :raw_metadata, JSON def self.fields_from_data(data) data.headers.flatten.compact.uniq end @@ -227,13 +230,22 @@ def possible_collection_ids ActiveSupport::Deprecation.warn( 'Creating Collections using the collection_field_mapping will no longer be supported as of Bulkrax version 3.0.' \ ' Please configure Bulkrax to use related_parents_field_mapping and related_children_field_mapping instead.' ) - @possible_collection_ids ||= record.inject([]) do |memo, (key, value)| - memo += value.split(/\s*[:;|]\s*/) if self.class.collection_field.to_s == key_without_numbers(key) && value.present? - memo - end || [] + return @possible_collection_ids if @possible_collection_ids.present? + + collection_field_mapping = self.class.collection_field + return [] unless collection_field_mapping.present? && record[collection_field_mapping].present? + + identifiers = [] + split_titles = record[collection_field_mapping].split(/\s*[;|]\s*/) + split_titles.each do |c_title| + matching_collection_entries = importerexporter.entries.select { |e| e.raw_metadata['title'] == c_title } + raise ::StandardError, 'Only expected to find one matching entry' if matching_collection_entries.count > 1 + identifiers << matching_collection_entries.first&.identifier + end + @possible_collection_ids = identifiers.compact.presence || [] end def collections_created? possible_collection_ids.length == self.collection_ids.length end