diff --git a/app/factories/bulkrax/object_factory.rb b/app/factories/bulkrax/object_factory.rb index 03b3884b..e296101d 100644 --- a/app/factories/bulkrax/object_factory.rb +++ b/app/factories/bulkrax/object_factory.rb @@ -7,20 +7,15 @@ class ObjectFactory include DynamicRecordLookup define_model_callbacks :save, :create - attr_reader :attributes, :object, :source_identifier_value, :klass, :replace_files, :update_files, :work_identifier, :collection_field_mapping, :related_parents_parsed_mapping + attr_reader :attributes, :object, :source_identifier_value, :klass, :replace_files, :update_files, :work_identifier, :related_parents_parsed_mapping # rubocop:disable Metrics/ParameterLists - def initialize(attributes:, source_identifier_value:, work_identifier:, collection_field_mapping:, related_parents_parsed_mapping: nil, replace_files: false, user: nil, klass: nil, update_files: false) - 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.' - ) + def initialize(attributes:, source_identifier_value:, work_identifier:, related_parents_parsed_mapping: nil, replace_files: false, user: nil, klass: nil, update_files: false) @attributes = ActiveSupport::HashWithIndifferentAccess.new(attributes) @replace_files = replace_files @update_files = update_files @user = user || User.batch_user @work_identifier = work_identifier - @collection_field_mapping = collection_field_mapping @related_parents_parsed_mapping = related_parents_parsed_mapping @source_identifier_value = source_identifier_value @klass = klass || Bulkrax.default_work_type.constantize @@ -55,7 +50,7 @@ def run! def update raise "Object doesn't exist" unless object destroy_existing_files if @replace_files && ![Collection, FileSet].include?(klass) - attrs = attribute_update + attrs = transform_attributes(update: true) run_callbacks :save do if klass == Collection update_collection(attrs) @@ -97,7 +92,7 @@ def search_by_identifier # https://github.com/projecthydra/active_fedora/issues/874 # 2+ years later, still open! def create - attrs = create_attributes + attrs = transform_attributes @object = klass.new object.reindex_extent = Hyrax::Adapters::NestingIndexAdapter::LIMITED_REINDEX if object.respond_to?(:reindex_extent) run_callbacks :save do @@ -142,25 +137,15 @@ def work_actor end def create_collection(attrs) - 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.' - ) attrs = collection_type(attrs) - persist_collection_memberships(parent: object, child: find_collection(attributes[:child_collection_id])) if attributes[:child_collection_id].present? - persist_collection_memberships(parent: find_collection(attributes[collection_field_mapping]), child: object) if attributes[collection_field_mapping].present? + persist_collection_memberships(parent: find_collection(attributes[related_parents_parsed_mapping]), child: object) if attributes[related_parents_parsed_mapping].present? object.attributes = attrs object.apply_depositor_metadata(@user) object.save! end def update_collection(attrs) - 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.' - ) - persist_collection_memberships(parent: object, child: find_collection(attributes[:child_collection_id])) if attributes[:child_collection_id].present? - persist_collection_memberships(parent: find_collection(attributes[collection_field_mapping]), child: object) if attributes[collection_field_mapping].present? + persist_collection_memberships(parent: find_collection(attributes[related_parents_parsed_mapping]), child: object) if attributes[related_parents_parsed_mapping].present? object.attributes = attrs object.save! end @@ -219,34 +204,12 @@ def collection_type(attrs) attrs end - # Strip out the :collection key, and add the member_of_collection_ids, - # which is used by Hyrax::Actors::AddAsMemberOfCollectionsActor - def create_attributes - return transform_attributes if klass == Collection - 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.' - ) - transform_attributes.except(:collections, :collection, collection_field_mapping) - end - - # Strip out the :collection key, and add the member_of_collection_ids, - # which is used by Hyrax::Actors::AddAsMemberOfCollectionsActor - def attribute_update - return transform_attributes.except(:id) if klass == Collection - 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.' - ) - transform_attributes.except(:id, :collections, :collection, collection_field_mapping) - end - # Override if we need to map the attributes from the parser in # a way that is compatible with how the factory needs them. - def transform_attributes + def transform_attributes(update: false) @transform_attributes = attributes.slice(*permitted_attributes) @transform_attributes.merge!(file_attributes(update_files)) if with_files - @transform_attributes + update ? @transform_attributes.except(:id) : @transform_attributes end # Regardless of what the Parser gives us, these are the properties we are prepared to accept. diff --git a/app/jobs/bulkrax/create_relationships_job.rb b/app/jobs/bulkrax/create_relationships_job.rb index c6e16312..b5b3b361 100644 --- a/app/jobs/bulkrax/create_relationships_job.rb +++ b/app/jobs/bulkrax/create_relationships_job.rb @@ -82,17 +82,13 @@ def user # Work-Collection membership is added to the child as member_of_collection_ids # This is adding the reverse relationship, from the child to the parent def collection_parent_work_child - 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.' - ) 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, - collection_field_mapping: parent_entry.parser.collection_field_mapping, + related_parents_parsed_mapping: parent_entry.parser.related_parents_parsed_mapping, replace_files: false, user: user, klass: child_record.class @@ -104,17 +100,13 @@ def collection_parent_work_child # Collection-Collection membership is added to the as member_ids def collection_parent_collection_child - 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.' - ) 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, - collection_field_mapping: parent_entry.parser.collection_field_mapping, + related_parents_parsed_mapping: parent_entry.parser.related_parents_parsed_mapping, replace_files: false, user: user, klass: parent_record.class @@ -125,11 +117,6 @@ def collection_parent_collection_child # Work-Work membership is added to the parent as member_ids def work_parent_work_child - 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.' - ) - records_hash = {} child_records[:works].each_with_index do |child_record, i| records_hash[i] = { id: child_record.id } @@ -142,7 +129,7 @@ def work_parent_work_child 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, - collection_field_mapping: parent_entry.parser.collection_field_mapping, + related_parents_parsed_mapping: parent_entry.parser.related_parents_parsed_mapping, replace_files: false, user: user, klass: parent_record.class diff --git a/app/jobs/bulkrax/importer_job.rb b/app/jobs/bulkrax/importer_job.rb index c7a2fba5..0af2cda5 100644 --- a/app/jobs/bulkrax/importer_job.rb +++ b/app/jobs/bulkrax/importer_job.rb @@ -18,10 +18,7 @@ def import(importer, only_updates_since_last_import) importer.only_updates = only_updates_since_last_import || false return unless importer.valid_import? - importer.import_collections - importer.import_works - importer.import_relationships - importer.import_file_sets + importer.import_objects end def unzip_imported_file(parser) diff --git a/app/models/bulkrax/csv_entry.rb b/app/models/bulkrax/csv_entry.rb index 7a8522af..d09f3cf6 100644 --- a/app/models/bulkrax/csv_entry.rb +++ b/app/models/bulkrax/csv_entry.rb @@ -22,18 +22,15 @@ def self.read_data(path) encoding: 'utf-8') end - def self.data_for_entry(data, _source_id) - 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.' - ) + def self.data_for_entry(data, _source_id, parser) # If a multi-line CSV data is passed, grab the first row data = data.first if data.is_a?(CSV::Table) # model has to be separated so that it doesn't get mistranslated by to_h raw_data = data.to_h raw_data[:model] = data[:model] if data[:model].present? # If the collection field mapping is not 'collection', add 'collection' - the parser needs it - raw_data[:collection] = raw_data[collection_field.to_sym] if raw_data.keys.include?(collection_field.to_sym) && collection_field != 'collection' + # TODO: change to :parents + raw_data[:parents] = raw_data[parent_field(parser).to_sym] if raw_data.keys.include?(parent_field(parser).to_sym) && parent_field(parser) != 'parents' return raw_data end @@ -47,7 +44,6 @@ def build_metadata add_visibility add_metadata_for_model add_rights_statement - add_collections add_local self.parsed_metadata @@ -70,15 +66,9 @@ def add_metadata_for_model end def add_ingested_metadata - 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.' - ) # we do not want to sort the values in the record before adding the metadata. # if we do, the factory_class will be set to the default_work_type for all values that come before "model" or "work type" record.each do |key, value| - next if self.parser.collection_field_mapping.to_s == key_without_numbers(key) - index = key[/\d+/].to_i - 1 if key[/\d+/].to_i != 0 add_metadata(key_without_numbers(key), value, index) end @@ -249,13 +239,9 @@ def self.matcher_class end 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.' - ) return @possible_collection_ids if @possible_collection_ids.present? - collection_field_mapping = self.class.collection_field + collection_field_mapping = self.class.parent_field(parser) return [] unless collection_field_mapping.present? && record[collection_field_mapping].present? identifiers = [] diff --git a/app/models/bulkrax/entry.rb b/app/models/bulkrax/entry.rb index f748da4c..91c65b1e 100644 --- a/app/models/bulkrax/entry.rb +++ b/app/models/bulkrax/entry.rb @@ -58,7 +58,7 @@ def self.read_data(_path) # @param data - the data from the metadata file # @param path - the path to the metadata file (used by some entries to get the file_paths for import) # @return Hash containing the data (the entry build_metadata method will know what to expect in the hash) - def self.data_for_entry(_data, _source_id) + def self.data_for_entry(_data, _source_id, _parser) raise StandardError, 'Not Implemented' end @@ -70,12 +70,8 @@ def work_identifier parser&.work_identifier&.to_s || 'source' end - def self.collection_field - 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.' - ) - Bulkrax.collection_field_mapping[self.to_s] + def self.parent_field(parser) + parser.related_parents_parsed_mapping end def build diff --git a/app/models/bulkrax/importer.rb b/app/models/bulkrax/importer.rb index fc5f3d9d..05916cb7 100644 --- a/app/models/bulkrax/importer.rb +++ b/app/models/bulkrax/importer.rb @@ -125,25 +125,32 @@ def update_files end def import_works - self.only_updates ||= false - import_objects('works') + import_objects(['work']) end def import_collections - import_objects('collections') + import_objects(['collection']) end def import_file_sets - import_objects('file_sets') + import_objects(['file_set']) end def import_relationships - import_objects('relationships') + import_objects(['relationship']) end - def import_objects(object_type) - self.save if self.new_record? # Object needs to be saved for statuses - parser.send("create_#{object_type}") + def import_objects(types_array = nil) + self.only_updates ||= false + types = types_array || %w[work collection file_set relationship] + if parser.class == Bulkrax::CsvParser + parser.create_objects(types) + else + types.each do |object_type| + self.save if self.new_record? # Object needs to be saved for statuses + parser.send("create_#{object_type.pluralize}") + end + end rescue StandardError => e status_info(e) end diff --git a/app/models/bulkrax/oai_entry.rb b/app/models/bulkrax/oai_entry.rb index 33f6d7e6..51e23309 100644 --- a/app/models/bulkrax/oai_entry.rb +++ b/app/models/bulkrax/oai_entry.rb @@ -26,10 +26,6 @@ def thumbnail_url end def build_metadata - 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.' - ) self.parsed_metadata = {} self.parsed_metadata[work_identifier] = [record.header.identifier] diff --git a/app/models/bulkrax/rdf_entry.rb b/app/models/bulkrax/rdf_entry.rb index de3467ab..7e5e269c 100644 --- a/app/models/bulkrax/rdf_entry.rb +++ b/app/models/bulkrax/rdf_entry.rb @@ -13,11 +13,7 @@ def self.fields_from_data(data) data.predicates.map(&:to_s) end - def self.data_for_entry(data, source_id) - 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.' - ) + def self.data_for_entry(data, source_id, parser) reader = data format = reader.class.format.to_sym collections = [] @@ -25,7 +21,7 @@ def self.data_for_entry(data, source_id) delete = nil data = RDF::Writer.for(format).buffer do |writer| reader.each_statement do |statement| - collections << statement.object.to_s if collection_field.present? && collection_field == statement.predicate.to_s + collections << statement.object.to_s if parent_field(parser).present? && parent_field(parser) == statement.predicate.to_s children << statement.object.to_s if related_children_parsed_mapping.present? && related_children_parsed_mapping == statement.predicate.to_s delete = statement.object.to_s if /deleted/.match?(statement.predicate.to_s) writer << statement @@ -55,10 +51,6 @@ def record end def build_metadata - 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.' - ) raise StandardError, 'Record not found' if record.nil? raise StandardError, "Missing source identifier (#{source_identifier})" if self.raw_metadata[source_identifier].blank? diff --git a/app/models/bulkrax/xml_entry.rb b/app/models/bulkrax/xml_entry.rb index 88154dac..d01d25a3 100644 --- a/app/models/bulkrax/xml_entry.rb +++ b/app/models/bulkrax/xml_entry.rb @@ -14,7 +14,7 @@ def self.read_data(path) Nokogiri::XML(open(path)).remove_namespaces! end - def self.data_for_entry(data, source_id) + def self.data_for_entry(data, source_id, _parser) collections = [] children = [] xpath_for_source_id = ".//*[name()='#{source_id}']" @@ -39,10 +39,6 @@ def record end def build_metadata - 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.' - ) raise StandardError, 'Record not found' if record.nil? raise StandardError, "Missing source identifier (#{source_identifier})" if self.raw_metadata[source_identifier].blank? self.parsed_metadata = {} diff --git a/app/models/concerns/bulkrax/has_matchers.rb b/app/models/concerns/bulkrax/has_matchers.rb index 0b1dd5ad..2d8f2416 100644 --- a/app/models/concerns/bulkrax/has_matchers.rb +++ b/app/models/concerns/bulkrax/has_matchers.rb @@ -129,10 +129,6 @@ def field_supported?(field) end def supported_bulkrax_fields - 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.' - ) @supported_bulkrax_fields ||= %W[ id @@ -141,22 +137,16 @@ def supported_bulkrax_fields model visibility delete - #{parser.collection_field_mapping} #{related_parents_parsed_mapping} #{related_children_parsed_mapping} ] end def multiple?(field) - 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.' - ) @multiple_bulkrax_fields ||= %W[ file remote_files - #{parser.collection_field_mapping} #{related_parents_parsed_mapping} #{related_children_parsed_mapping} ] diff --git a/app/models/concerns/bulkrax/import_behavior.rb b/app/models/concerns/bulkrax/import_behavior.rb index 849a808d..17e8ad6a 100644 --- a/app/models/concerns/bulkrax/import_behavior.rb +++ b/app/models/concerns/bulkrax/import_behavior.rb @@ -85,10 +85,6 @@ def add_admin_set_id def add_collections return if find_collection_ids.blank? - 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.' - ) self.parsed_metadata['member_of_collections_attributes'] = {} find_collection_ids.each_with_index do |c, i| self.parsed_metadata['member_of_collections_attributes'][i.to_s] = { id: c } @@ -96,15 +92,10 @@ def add_collections end def factory - 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.' - ) @factory ||= Bulkrax::ObjectFactory.new(attributes: self.parsed_metadata, source_identifier_value: identifier, work_identifier: parser.work_identifier, - collection_field_mapping: parser.collection_field_mapping, - related_parents_parsed_mapping: related_parents_parsed_mapping, + related_parents_parsed_mapping: parser.related_parents_parsed_mapping, replace_files: replace_files, user: user, klass: factory_class, diff --git a/app/models/concerns/bulkrax/importer_exporter_behavior.rb b/app/models/concerns/bulkrax/importer_exporter_behavior.rb index ec72d3ed..738dd3ad 100644 --- a/app/models/concerns/bulkrax/importer_exporter_behavior.rb +++ b/app/models/concerns/bulkrax/importer_exporter_behavior.rb @@ -20,14 +20,14 @@ def next_import_at (last_imported_at || Time.current) + frequency.to_seconds if schedulable? && last_imported_at.present? end - def increment_counters(index, collection: false, file_set: false) + def increment_counters(index, collection: false, file_set: false, work: false) # Only set the totals if they were not set on initialization importer_run = ImporterRun.find(current_run.id) # make sure fresh if collection importer_run.total_collection_entries = index + 1 unless parser.collections_total.positive? elsif file_set importer_run.total_file_set_entries = index + 1 unless parser.file_sets_total.positive? - else + elsif work # TODO: differentiate between work and collection counts for exporters importer_run.total_work_entries = index + 1 unless limit.to_i.positive? || parser.total.positive? end diff --git a/app/parsers/bulkrax/application_parser.rb b/app/parsers/bulkrax/application_parser.rb index 022aae5a..dafee2a5 100644 --- a/app/parsers/bulkrax/application_parser.rb +++ b/app/parsers/bulkrax/application_parser.rb @@ -56,7 +56,7 @@ def related_parents_raw_mapping end def related_parents_parsed_mapping - @related_parents_parsed_mapping ||= get_field_mapping_hash_for('related_parents_field_mapping')&.keys&.first + @related_parents_parsed_mapping ||= (get_field_mapping_hash_for('related_parents_field_mapping')&.keys&.first || 'parents') end def related_children_raw_mapping @@ -64,29 +64,22 @@ def related_children_raw_mapping end def related_children_parsed_mapping - @related_children_parsed_mapping ||= get_field_mapping_hash_for('related_children_field_mapping')&.keys&.first + @related_children_parsed_mapping ||= (get_field_mapping_hash_for('related_children_field_mapping')&.keys&.first || 'children') end def get_field_mapping_hash_for(key) return instance_variable_get("@#{key}_hash") if instance_variable_get("@#{key}_hash").present? + mapping = importerexporter.field_mapping == [{}] ? {} : importerexporter.field_mapping instance_variable_set( "@#{key}_hash", - importerexporter.mapping.with_indifferent_access.select { |_, h| h.key?(key) } + mapping&.with_indifferent_access&.select { |_, h| h.key?(key) } ) raise StandardError, "more than one #{key} declared: #{instance_variable_get("@#{key}_hash").keys.join(', ')}" if instance_variable_get("@#{key}_hash").length > 1 instance_variable_get("@#{key}_hash") end - def collection_field_mapping - 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.' - ) - Bulkrax.collection_field_mapping[self.entry_class.to_s]&.to_sym || :collection - end - def model_field_mappings model_mappings = Bulkrax.field_mappings[self.class.to_s]&.dig('model', :from) || [] model_mappings |= ['model'] diff --git a/app/parsers/bulkrax/bagit_parser.rb b/app/parsers/bulkrax/bagit_parser.rb index bfb7d874..9391e8e1 100644 --- a/app/parsers/bulkrax/bagit_parser.rb +++ b/app/parsers/bulkrax/bagit_parser.rb @@ -39,7 +39,7 @@ def records(_opts = {}) path = metadata_path(bag) raise StandardError, 'No metadata files were found' if path.blank? data = entry_class.read_data(path) - data = entry_class.data_for_entry(data, source_identifier) + data = entry_class.data_for_entry(data, source_identifier, self) data[:file] = bag.bag_files.join('|') unless importerexporter.metadata_only? data end @@ -75,7 +75,7 @@ def create_works else ImportWorkJob.send(perform_method, new_entry.id, current_run.id) end - increment_counters(index) + increment_counters(index, work: true) end importer.record_status rescue StandardError => e @@ -83,11 +83,7 @@ def create_works end def collections - 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.' - ) - records.map { |r| r[collection_field_mapping].split(/\s*[;|]\s*/) if r[collection_field_mapping].present? }.flatten.compact.uniq + records.map { |r| r[related_parents_parsed_mapping].split(/\s*[;|]\s*/) if r[related_parents_parsed_mapping].present? }.flatten.compact.uniq end def collections_total diff --git a/app/parsers/bulkrax/csv_parser.rb b/app/parsers/bulkrax/csv_parser.rb index adf62ea6..ec02696b 100644 --- a/app/parsers/bulkrax/csv_parser.rb +++ b/app/parsers/bulkrax/csv_parser.rb @@ -14,18 +14,13 @@ def records(_opts = {}) csv_data = entry_class.read_data(file_for_import) importer.parser_fields['total'] = csv_data.count importer.save - @records ||= csv_data.map { |record_data| entry_class.data_for_entry(record_data, nil) } + @records ||= csv_data.map { |record_data| entry_class.data_for_entry(record_data, nil, self) } end def collections - 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.' - ) # retrieve a list of unique collections records.map do |r| collections = [] - r[collection_field_mapping].split(/\s*[;|]\s*/).each { |title| collections << { title: title, from_collection_field_mapping: true } } if r[collection_field_mapping].present? model_field_mappings.each do |model_mapping| collections << r if r[model_mapping.to_sym]&.downcase == 'collection' end @@ -85,93 +80,52 @@ def valid_import? end def create_collections - collections.each_with_index do |collection, index| - next if collection.blank? - break if records.find_index(collection).present? && limit_reached?(limit, records.find_index(collection)) - - ## BEGIN - # Add required metadata to collections being imported using the collection_field_mapping, which only have a :title - # TODO: Remove once collection_field_mapping is removed - metadata = add_required_collection_metadata(collection) - collection_hash = metadata.presence || collection - ## END - - new_entry = find_or_create_entry(collection_entry_class, collection_hash[source_identifier], 'Bulkrax::Importer', collection_hash) - increment_counters(index, collection: true) - # TODO: add support for :delete option - if collection.key?(:from_collection_field_mapping) - 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.' - ) - # When importing collections using the deprecated collection_field_mapping, the collection MUST be created - # before the work, so we use #perform_now to make sure that happens. The downside is, if a collection fails - # to import, it will stop the rest of the collections from importing successfully. - # TODO: Remove once collection_field_mapping is removed - ImportCollectionJob.perform_now(new_entry.id, current_run.id) - else - ImportCollectionJob.perform_later(new_entry.id, current_run.id) - end - end - importer.record_status - rescue StandardError => e - status_info(e) + create_objects(['collection']) end def create_works - works.each_with_index do |work, index| - next unless record_has_source_identifier(work, records.find_index(work)) - break if limit_reached?(limit, records.find_index(work)) - - seen[work[source_identifier]] = true - new_entry = find_or_create_entry(entry_class, work[source_identifier], 'Bulkrax::Importer', work.to_h) - if work[:delete].present? - DeleteWorkJob.send(perform_method, new_entry, current_run) - else - ImportWorkJob.send(perform_method, new_entry.id, current_run.id) - end - increment_counters(index) - end - importer.record_status - rescue StandardError => e - status_info(e) + create_objects(['work']) end def create_file_sets - file_sets.each_with_index do |file_set, index| - next unless record_has_source_identifier(file_set, records.find_index(file_set)) - break if limit_reached?(limit, records.find_index(file_set)) - - new_entry = find_or_create_entry(file_set_entry_class, file_set[source_identifier], 'Bulkrax::Importer', file_set.to_h) - ImportFileSetJob.perform_later(new_entry.id, current_run.id) - increment_counters(index, file_set: true) - end - importer.record_status - rescue StandardError => e - status_info(e) + create_objects(['file_set']) end def create_relationships - ScheduleRelationshipsJob.set(wait: 5.minutes).perform_later(importer_id: importerexporter.id) + create_objects(['relationship']) end - # Add required metadata to collections being imported using the collection_field_mapping, which only have a :title - # TODO: Remove once collection_field_mapping is removed - def add_required_collection_metadata(raw_collection_data) - return unless raw_collection_data.key?(:from_collection_field_mapping) - 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.' - ) + def create_objects(types_array = nil) + (types_array || %w[work collection file_set relationship]).each do |type| + if type.eql?('relationship') + ScheduleRelationshipsJob.set(wait: 5.minutes).perform_later(importer_id: importerexporter.id) + next + end + send(type.pluralize).each_with_index do |current_record, index| + next unless record_has_source_identifier(current_record, records.find_index(current_record)) + break if limit_reached?(limit, records.find_index(current_record)) - uci = unique_collection_identifier(raw_collection_data) - { - title: raw_collection_data[:title], - work_identifier => uci, - source_identifier => uci, - visibility: 'open', - collection_type_gid: ::Hyrax::CollectionType.find_or_create_default_collection_type.gid - } + seen[current_record[source_identifier]] = true + create_entry_and_job(current_record, type) + increment_counters(index, "#{type}": true) + end + importer.record_status + end + rescue StandardError => e + status_info(e) + end + + def create_entry_and_job(current_record, type) + new_entry = find_or_create_entry(send("#{type}_entry_class"), + current_record[source_identifier], + 'Bulkrax::Importer', + current_record.to_h) + if current_record[:delete].present? + # TODO: create a "Delete" job for file_sets and collections + "Bulkrax::Delete#{type.camelize}Job".constantize.send(perform_method, new_entry, current_run) + else + "Bulkrax::Import#{type.camelize}Job".constantize.send(perform_method, new_entry.id, current_run.id) + end end def write_partial_import_file(file) @@ -278,6 +232,7 @@ def create_new_entries def entry_class CsvEntry end + alias work_entry_class entry_class def collection_entry_class CsvCollectionEntry diff --git a/app/parsers/bulkrax/oai_dc_parser.rb b/app/parsers/bulkrax/oai_dc_parser.rb index 80eb8443..8d3ff329 100644 --- a/app/parsers/bulkrax/oai_dc_parser.rb +++ b/app/parsers/bulkrax/oai_dc_parser.rb @@ -102,7 +102,7 @@ def create_works else ImportWorkJob.send(perform_method, new_entry.id, importerexporter.current_run.id) end - increment_counters(index) + increment_counters(index, work: true) end importer.record_status end diff --git a/app/parsers/bulkrax/xml_parser.rb b/app/parsers/bulkrax/xml_parser.rb index 4aed0812..c4fbd544 100644 --- a/app/parsers/bulkrax/xml_parser.rb +++ b/app/parsers/bulkrax/xml_parser.rb @@ -40,14 +40,14 @@ def records(_opts = {}) metadata_paths.map do |md| # Retrieve all records elements = entry_class.read_data(md).xpath("//#{record_element}") - r += elements.map { |el| entry_class.data_for_entry(el, source_identifier) } + r += elements.map { |el| entry_class.data_for_entry(el, source_identifier, self) } end # Flatten because we may have multiple records per array r.compact.flatten elsif parser_fields['import_type'] == 'single' metadata_paths.map do |md| data = entry_class.read_data(md).xpath("//#{record_element}").first # Take only the first record - entry_class.data_for_entry(data, source_identifier) + entry_class.data_for_entry(data, source_identifier, self) end.compact # No need to flatten because we take only the first record end end @@ -94,7 +94,7 @@ def create_works else ImportWorkJob.send(perform_method, new_entry.id, current_run.id) end - increment_counters(index) + increment_counters(index, work: true) end importer.record_status rescue StandardError => e diff --git a/lib/bulkrax.rb b/lib/bulkrax.rb index f7abcbbb..c02aa4e4 100644 --- a/lib/bulkrax.rb +++ b/lib/bulkrax.rb @@ -5,11 +5,9 @@ module Bulkrax class << self - # TODO: remove collection_field_mapping when releasing v2 mattr_accessor :parsers, :default_work_type, :default_field_mapping, - :collection_field_mapping, :fill_in_blank_source_identifiers, :related_children_field_mapping, :related_parents_field_mapping, @@ -35,17 +33,6 @@ class << self self.removed_image_path = Bulkrax::Engine.root.join('spec', 'fixtures', 'removed.png').to_s self.server_name = 'bulkrax@example.com' - # NOTE: 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. - # TODO: remove collection_field_mapping when releasing v2 - # Field_mapping for establishing a collection relationship (FROM work TO collection) - # This value IS NOT used for OAI, so setting the OAI Entries here will have no effect - # The mapping is supplied per Entry, provide the full class name as a string, eg. 'Bulkrax::CsvEntry' - # The default value for CSV is collection - self.collection_field_mapping = { - 'Bulkrax::CsvEntry' => 'collection' - } - # Hash of Generic field_mappings for use in the view # There must be one field_mappings hash per view parial # Based on Hyrax CoreMetadata && BasicMetadata diff --git a/spec/factories/bulkrax_importers.rb b/spec/factories/bulkrax_importers.rb index d7776a6a..cfce4015 100644 --- a/spec/factories/bulkrax_importers.rb +++ b/spec/factories/bulkrax_importers.rb @@ -62,7 +62,12 @@ parser_klass { 'Bulkrax::CsvParser' } limit { 10 } parser_fields { { 'import_file_path' => 'spec/fixtures/csv/complex.csv' } } - field_mapping { {} } + field_mapping do + { + 'parents' => { 'from' => ['parents_column'], split: /\s*[|]\s*/, related_parents_field_mapping: true }, + 'children' => { 'from' => ['children_column'], split: /\s*[|]\s*/, related_children_field_mapping: true } + } + end end factory :bulkrax_importer_bagit, class: 'Bulkrax::Importer' do diff --git a/spec/factories/bulkrax_object_factories.rb b/spec/factories/bulkrax_object_factories.rb index ef619b23..0e49a0e4 100644 --- a/spec/factories/bulkrax_object_factories.rb +++ b/spec/factories/bulkrax_object_factories.rb @@ -6,8 +6,7 @@ new( attributes: {}, source_identifier_value: :source_identifier, - work_identifier: :source, - collection_field_mapping: :collection + work_identifier: :source ) end end diff --git a/spec/fixtures/csv/good.csv b/spec/fixtures/csv/good.csv index 023171f1..1892da17 100644 --- a/spec/fixtures/csv/good.csv +++ b/spec/fixtures/csv/good.csv @@ -1,3 +1,5 @@ -source_identifier,title,collection -1,Lovely Title,A Collection;Another Collection, -2,Another Title,A Collection, \ No newline at end of file +model,source_identifier,title,parents_column +GenericWork,1,Lovely Title,3;4, +GenericWork,2,Another Title,3, +Collection,3,A Collection,, +Collection,4,Another Collection,, \ No newline at end of file diff --git a/spec/jobs/bulkrax/create_relationships_job_spec.rb b/spec/jobs/bulkrax/create_relationships_job_spec.rb index 8fc01f2e..51952ed8 100644 --- a/spec/jobs/bulkrax/create_relationships_job_spec.rb +++ b/spec/jobs/bulkrax/create_relationships_job_spec.rb @@ -19,7 +19,7 @@ module Bulkrax { source_identifier_value: nil, work_identifier: :source, - collection_field_mapping: :collection, + related_parents_parsed_mapping: parent_entry.parser.related_parents_parsed_mapping, replace_files: false, user: importer.user } diff --git a/spec/jobs/bulkrax/importer_job_spec.rb b/spec/jobs/bulkrax/importer_job_spec.rb index 03e0560d..78591540 100644 --- a/spec/jobs/bulkrax/importer_job_spec.rb +++ b/spec/jobs/bulkrax/importer_job_spec.rb @@ -13,17 +13,17 @@ module Bulkrax describe 'successful job' do it 'calls import_works with false' do - expect(importer).to receive(:import_works) + expect(importer).to receive(:import_objects) importer_job.perform(1) end it 'calls import_works with true if only_updates_since_last_import=true' do - expect(importer).to receive(:import_works) + expect(importer).to receive(:import_objects) importer_job.perform(1, true) end it 'updates the current run counters' do - expect(importer).to receive(:import_works) + expect(importer).to receive(:import_objects) importer_job.perform(1) expect(importer.current_run.total_work_entries).to eq(10) @@ -35,8 +35,7 @@ module Bulkrax let(:importer) { FactoryBot.create(:bulkrax_importer_csv_bad) } it 'returns for an invalid import' do - expect(importer).not_to receive(:import_collections) - expect(importer).not_to receive(:import_works) + expect(importer).not_to receive(:import_objects) end end @@ -47,7 +46,7 @@ module Bulkrax end it 'schedules import_works when schedulable?' do - expect(importer).to receive(:import_works) + expect(importer).to receive(:import_objects) expect(described_class).to receive(:set).with(wait_until: 1).and_return(described_class) importer_job.perform(1) end diff --git a/spec/lib/bulkrax_spec.rb b/spec/lib/bulkrax_spec.rb index 56bc58b0..581e6b8f 100644 --- a/spec/lib/bulkrax_spec.rb +++ b/spec/lib/bulkrax_spec.rb @@ -16,18 +16,6 @@ end end - context 'collection_field_mapping' do - it 'responds to collection_field_mapping' do - expect(described_class).to respond_to(:collection_field_mapping) - end - it 'collection_field_mapping is settable' do - expect(described_class).to respond_to(:collection_field_mapping=) - end - it 'has a default value for CsvEntry' do - expect(described_class.collection_field_mapping).to eq({ 'Bulkrax::CsvEntry' => 'collection' }) - end - end - context 'import_path' do it 'responds to import_path' do expect(described_class).to respond_to(:import_path) diff --git a/spec/models/bulkrax/importer_spec.rb b/spec/models/bulkrax/importer_spec.rb index c3aa7e39..f653e39f 100644 --- a/spec/models/bulkrax/importer_spec.rb +++ b/spec/models/bulkrax/importer_spec.rb @@ -77,7 +77,8 @@ module Bulkrax it 'creates a default mapping from the column headers' do expect(importer.mapping).to eq( - "collection" => { "excluded" => false, "from" => ["collection"], "if" => nil, "parsed" => false, "split" => false }, + "model" => { "excluded" => false, "from" => ["model"], "if" => nil, "parsed" => true, "split" => false }, + "parents_column" => { "excluded" => false, "from" => ["parents_column"], "if" => nil, "parsed" => false, "split" => false }, "source_identifier" => { "excluded" => false, "from" => ["source_identifier"], "if" => nil, "parsed" => false, "split" => false }, "title" => { "excluded" => false, "from" => ["title"], "if" => nil, "parsed" => false, "split" => false } ) diff --git a/spec/models/bulkrax/rdf_entry_spec.rb b/spec/models/bulkrax/rdf_entry_spec.rb index 9b99ac22..15faee23 100644 --- a/spec/models/bulkrax/rdf_entry_spec.rb +++ b/spec/models/bulkrax/rdf_entry_spec.rb @@ -4,8 +4,23 @@ module Bulkrax RSpec.describe RdfEntry, type: :model do + subject { described_class.new(importerexporter: importer) } let(:path) { './spec/fixtures/bags/bag/descMetadata.nt' } let(:data) { described_class.read_data(path) } + let(:importer) do + i = FactoryBot.create(:bulkrax_importer_bagit, + parser_fields: { + 'import_file_path' => './spec/fixtures/bags/bag', + 'metadata_file_name' => 'descMetadata.nt', + 'metadata_format' => 'Bulkrax::RdfEntry' + }, + field_mapping: { + 'identifier' => { from: ['http://purl.org/dc/terms/identifier'] }, + 'title' => { from: ['http://purl.org/dc/terms/title'] } + }) + i.current_run + i + end describe 'class methods' do it 'reads the data' do @@ -17,7 +32,7 @@ module Bulkrax end it 'retrieves the data and constructs a hash' do - expect(described_class.data_for_entry(data, :source_identifier)).to eq( + expect(described_class.data_for_entry(data, :source_identifier, subject.parser)).to eq( data: " \"12345\" .\n \"Test Bag\" .\n", delete: nil, children: [], @@ -29,22 +44,7 @@ module Bulkrax end describe 'builds entry' do - subject { described_class.new(importerexporter: importer) } - let(:raw_metadata) { described_class.data_for_entry(data, :source_identifier) } - let(:importer) do - i = FactoryBot.create(:bulkrax_importer_bagit, - parser_fields: { - 'import_file_path' => './spec/fixtures/bags/bag', - 'metadata_file_name' => 'descMetadata.nt', - 'metadata_format' => 'Bulkrax::RdfEntry' - }, - field_mapping: { - 'identifier' => { from: ['http://purl.org/dc/terms/identifier'] }, - 'title' => { from: ['http://purl.org/dc/terms/title'] } - }) - i.current_run - i - end + let(:raw_metadata) { described_class.data_for_entry(data, :source_identifier, subject.parser) } context 'deleted' do let(:path) { './spec/fixtures/bags/deleted_bag/descMetadata.nt' } diff --git a/spec/models/bulkrax/xml_entry_spec.rb b/spec/models/bulkrax/xml_entry_spec.rb index de8705d7..6552c10e 100644 --- a/spec/models/bulkrax/xml_entry_spec.rb +++ b/spec/models/bulkrax/xml_entry_spec.rb @@ -17,7 +17,7 @@ module Bulkrax context '#data_for_entry' do it 'retrieves the data and constructs a hash' do - expect(described_class.data_for_entry(data, source_identifier)).to eq( + expect(described_class.data_for_entry(data, source_identifier, nil)).to eq( DrisUnique: '3456012', delete: nil, data: " Single XML Entry Lorem ipsum dolor sit amet. 3456012 ", @@ -31,7 +31,7 @@ module Bulkrax describe 'deleted' do subject(:xml_entry) { described_class.new(importerexporter: importer) } let(:path) { './spec/fixtures/xml/deleted.xml' } - let(:raw_metadata) { described_class.data_for_entry(data, source_identifier) } + let(:raw_metadata) { described_class.data_for_entry(data, source_identifier, nil) } let(:importer) do i = FactoryBot.create(:bulkrax_importer_xml) i.current_run @@ -56,7 +56,7 @@ module Bulkrax describe '#build' do subject(:xml_entry) { described_class.new(importerexporter: importer) } - let(:raw_metadata) { described_class.data_for_entry(data, source_identifier) } + let(:raw_metadata) { described_class.data_for_entry(data, source_identifier, nil) } let(:importer) do i = FactoryBot.create(:bulkrax_importer_xml) i.field_mapping['source'] = { 'from' => ['DrisUnique'], 'source_identifier' => true } diff --git a/spec/parsers/bulkrax/csv_parser_spec.rb b/spec/parsers/bulkrax/csv_parser_spec.rb index 408d9be9..789501bd 100644 --- a/spec/parsers/bulkrax/csv_parser_spec.rb +++ b/spec/parsers/bulkrax/csv_parser_spec.rb @@ -7,6 +7,9 @@ module Bulkrax subject { described_class.new(importer) } let(:importer) { FactoryBot.create(:bulkrax_importer_csv) } + let(:relationship_importer) { FactoryBot.create(:bulkrax_importer_csv, :with_relationships_mappings) } + let(:relationship_subject) { described_class.new(relationship_importer) } + describe '#collections' do let(:all_collection_titles) { subject.collections.collect { |c| c[:title] } } @@ -14,10 +17,6 @@ module Bulkrax importer.parser_fields = { import_file_path: './spec/fixtures/csv/mixed_works_and_collections.csv' } end - it 'includes collection titles listed in the :collection column' do - expect(all_collection_titles).to include("Second Work's Collection") - end - it 'includes rows whose :model is set to Collection' do expect(all_collection_titles).to include('Collection 1 Title', 'Collection 2 Title') end @@ -28,43 +27,6 @@ module Bulkrax expect(subject.collections).to include({ model: 'cOlEcTiOn' }) end - context 'when Bulkrax.collection_field_mapping' do - before do - allow(subject) - .to receive(:records) - .and_return( - [ - { collection: 'collection mapping', title: 'W1', model: 'work' }, - { parent: 'parent mapping', title: 'W2', model: 'work' } - ] - ) - end - - context 'is set' do - before do - allow(subject).to receive(:collection_field_mapping).and_return(:parent) - end - - it 'the collection field mapping is used' do - expect(all_collection_titles).to include('parent mapping') - expect(all_collection_titles).not_to include('collection mapping') - end - end - - context 'is not set' do - it 'the mapping falls back on :collection' do - expect(all_collection_titles).not_to include('parent mapping') - expect(all_collection_titles).to include('collection mapping') - end - end - - context 'is used to import a collection' do - it 'a :from_collection_field_mapping key-value pair is added to its data' do - expect(subject.collections).to include({ title: 'collection mapping', from_collection_field_mapping: true }) - end - end - end - describe ':model field mappings' do before do allow(subject) @@ -133,7 +95,7 @@ module Bulkrax end it 'runs an ImportCollectionJob in memory for each entry' do - expect(ImportCollectionJob).to receive(:perform_now).twice + expect(ImportCollectionJob).to receive(:perform_later).twice subject.create_collections end @@ -176,6 +138,7 @@ module Bulkrax let(:record_hash) { { source_identifier: 'csid' } } it "uses the record's source_identifier as the entry's identifier" do + allow(subject.records).to receive(:find_index).and_return(0) subject.create_collections expect(importer.entries.last.identifier).to eq('csid') @@ -183,12 +146,13 @@ module Bulkrax end context 'when collection record does not have a source_identifier' do - let(:record_hash) { { title: 'no source id | alt title', from_collection_field_mapping: true } } + let(:record_hash) { { title: 'no source id | alt title', model: 'Collection' } } it "uses the record's first title as the entry's identifier" do + allow(subject.records).to receive(:find_index).and_return(0) subject.create_collections - expect(importer.entries.last.identifier).to eq('no source id') + expect(ImporterRun.find(subject.current_run.id).failed_records).to eq(1) end context 'when Bulkrax is set to fill in blank source_identifiers' do @@ -198,6 +162,7 @@ module Bulkrax end it "uses the generated identifier as the entry's identifier" do + allow(subject.records).to receive(:find_index).and_return(0) subject.create_collections expect(importer.entries.last.identifier).to eq("#{importer.id}-99") @@ -285,7 +250,7 @@ module Bulkrax it 'counts the correct number of works and collections' do subject.records - expect(subject.total).to eq(2) + expect(subject.total).to eq(4) expect(subject.collections_total).to eq(2) end end @@ -527,14 +492,18 @@ module Bulkrax end end - describe '#collection_field_mapping' do + describe '#related_parents_field_mapping' do context 'when the mapping is set' do before do - allow(Bulkrax).to receive(:collection_field_mapping).and_return({ 'Bulkrax::CsvEntry' => 'parent' }) + importer.field_mapping = { + 'parents' => { 'from' => ['parents_column'], related_parents_field_mapping: true }, + 'children' => { 'from' => ['children_column'], related_children_field_mapping: true }, + 'unrelated' => { 'from' => ['unrelated_column'] } + } end it 'returns the mapping' do - expect(subject.collection_field_mapping).to eq(:parent) + expect(subject.related_parents_parsed_mapping).to eq('parents') end end end @@ -692,7 +661,7 @@ module Bulkrax end describe '#related_parents_parsed_mapping' do - it { expect(subject.related_parents_parsed_mapping).to be_nil } + it { expect(subject.related_parents_parsed_mapping).to eq('parents') } end describe '#related_children_raw_mapping' do @@ -700,7 +669,7 @@ module Bulkrax end describe '#related_children_parsed_mapping' do - it { expect(subject.related_children_parsed_mapping).to be_nil } + it { expect(subject.related_children_parsed_mapping).to eq('children') } end end @@ -731,43 +700,5 @@ module Bulkrax end end end - - describe '#add_required_collection_metadata' do - context 'when importing collections using the collection_field_mapping' do - let(:collection_data) do - { - title: 'Collection from collection_field_mapping', - from_collection_field_mapping: true - } - end - - it 'adds metadata required for collections' do - expect(subject.add_required_collection_metadata(collection_data)).to eq( - { - title: collection_data[:title], - source: collection_data[:title], - source_identifier: collection_data[:title], - visibility: 'open', - collection_type_gid: ::Hyrax::CollectionType.find_or_create_default_collection_type.gid - } - ) - end - end - - context 'when importing collections without using the collection_field_mapping' do - let(:collection_data) do - { - source_identifier: 'cwm1', - title: 'Collection with Metadata', - model: 'Collection', - description: 'Lorem ipsum' - } - end - - it 'does not alter the data' do - expect(subject.add_required_collection_metadata(collection_data)).to be_nil - end - end - end end end diff --git a/spec/parsers/bulkrax/xml_parser_spec.rb b/spec/parsers/bulkrax/xml_parser_spec.rb index 559b8237..75bd70db 100644 --- a/spec/parsers/bulkrax/xml_parser_spec.rb +++ b/spec/parsers/bulkrax/xml_parser_spec.rb @@ -62,27 +62,26 @@ module Bulkrax end end - describe '#collection_field_mapping' do + describe '#parent_field_mapping' do subject(:xml_parser) { described_class.new(importer) } let(:importer) { FactoryBot.create(:bulkrax_importer_xml) } context 'when the mapping is set' do before do - allow(Bulkrax).to receive(:collection_field_mapping).and_return({ 'Bulkrax::XmlEntry' => 'parent' }) + importer.field_mapping = { + 'parents_test' => { 'from' => ['parents_column'], related_parents_field_mapping: true }, + 'children_test' => { 'from' => ['children_column'], related_children_field_mapping: true }, + 'unrelated' => { 'from' => ['unrelated_column'] } + } end - it 'returns the mapping' do - expect(xml_parser.collection_field_mapping).to eq(:parent) + expect(xml_parser.related_parents_parsed_mapping).to eq('parents_test') end end context 'when the mapping is not set' do - before do - allow(Bulkrax).to receive(:collection_field_mapping).and_return({}) - end - - it 'returns :collection' do - expect(xml_parser.collection_field_mapping).to eq(:collection) + it 'returns "parents" by default' do + expect(xml_parser.related_parents_parsed_mapping).to eq('parents') end end end