From 6776e96c40c9f586af7aa7c71688610bd7608ba2 Mon Sep 17 00:00:00 2001 From: Rob Kaufman Date: Wed, 13 Sep 2023 14:45:55 -0700 Subject: [PATCH] maybe fix valkyrieobject load issue --- app/matchers/concerns/has_ams_matchers.rb | 68 +++++++++++++++++++++++ app/models/bulkrax/csv_entry.rb | 2 + app/models/bulkrax/pbcore_xml_entry.rb | 1 + config/initializers/bulkrax.rb | 68 ----------------------- 4 files changed, 71 insertions(+), 68 deletions(-) create mode 100644 app/matchers/concerns/has_ams_matchers.rb diff --git a/app/matchers/concerns/has_ams_matchers.rb b/app/matchers/concerns/has_ams_matchers.rb new file mode 100644 index 00000000..fc94ccfe --- /dev/null +++ b/app/matchers/concerns/has_ams_matchers.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true +# rubocop:disable Metrics/ModuleLength +module HasAmsMatchers + extend ActiveSupport::Concern + class_methods do + ## + # Field of the model that can be supported + def field_supported?(field) + field = field.gsub("_attributes", "") + + return false if excluded?(field) + return true if supported_bulkrax_fields.include?(field) + # title is not defined in M3 + return true if field == "title" + + property_defined = factory_class.singleton_methods.include?(:properties) && factory_class.properties[field].present? + + factory_class.method_defined?(field) && (Bulkrax::ValkyrieObjectFactory.schema_properties(factory_class).include?(field) || property_defined) + end + + ## + # Determine a multiple properties field + def multiple?(field) + @multiple_bulkrax_fields ||= + %W[ + file + remote_files + rights_statement + #{related_parents_parsed_mapping} + #{related_children_parsed_mapping} + ] + + return true if @multiple_bulkrax_fields.include?(field) + return false if field == "model" + # title is not defined in M3 + return true if field == "title" + + field_supported?(field) && (multiple_field?(field) || factory_class.singleton_methods.include?(:properties) && factory_class&.properties&.[](field)&.[]("multiple")) + end + + def multiple_field?(field) + form_definition = schema_form_definitions[field.to_sym] + form_definition.nil? ? false : form_definition[:multiple] + end + + # override: we want to directly infer from a property being multiple that we should split when it's a String + # def multiple_metadata(content) + # return unless content + + # case content + # when Nokogiri::XML::NodeSet + # content&.content + # when Array + # content + # when Hash + # Array.wrap(content) + # when String + # String(content).strip.split(Bulkrax.multi_value_element_split_on) + # else + # Array.wrap(content) + # end + # end + + def schema_form_definitions + @schema_form_definitions ||= Hyrax::SimpleSchemaLoader.new.form_definitions_for(schema: factory_class.name.underscore.to_sym) + end + end +end diff --git a/app/models/bulkrax/csv_entry.rb b/app/models/bulkrax/csv_entry.rb index cd3ddf7d..c0527cef 100644 --- a/app/models/bulkrax/csv_entry.rb +++ b/app/models/bulkrax/csv_entry.rb @@ -4,6 +4,8 @@ require_dependency Bulkrax::Engine.root.join('app', 'models', 'bulkrax', 'csv_entry') Bulkrax::CsvEntry.class_eval do + include HasAmsMatchers + def self.read_data(path) raise StandardError, 'CSV path empty' if path.blank? diff --git a/app/models/bulkrax/pbcore_xml_entry.rb b/app/models/bulkrax/pbcore_xml_entry.rb index 9c456cb3..b84a6179 100644 --- a/app/models/bulkrax/pbcore_xml_entry.rb +++ b/app/models/bulkrax/pbcore_xml_entry.rb @@ -4,6 +4,7 @@ module Bulkrax class PbcoreXmlEntry < XmlEntry + include HasAmsMatchers def self.read_data(path) if MIME::Types.type_for(path).include?('text/csv') CSV.read(path, diff --git a/config/initializers/bulkrax.rb b/config/initializers/bulkrax.rb index 82b921d1..c8a58698 100644 --- a/config/initializers/bulkrax.rb +++ b/config/initializers/bulkrax.rb @@ -146,74 +146,6 @@ def headers(term = nil) 'media_type' => { from: headers('media_type'), split: true, join: true } } - module HasMappingExt - ## - # Field of the model that can be supported - def field_supported?(field) - field = field.gsub("_attributes", "") - - return false if excluded?(field) - return true if supported_bulkrax_fields.include?(field) - # title is not defined in M3 - return true if field == "title" - - property_defined = factory_class.singleton_methods.include?(:properties) && factory_class.properties[field].present? - - factory_class.method_defined?(field) && (Bulkrax::ValkyrieObjectFactory.schema_properties(factory_class).include?(field) || property_defined) - end - - ## - # Determine a multiple properties field - def multiple?(field) - @multiple_bulkrax_fields ||= - %W[ - file - remote_files - rights_statement - #{related_parents_parsed_mapping} - #{related_children_parsed_mapping} - ] - - return true if @multiple_bulkrax_fields.include?(field) - return false if field == "model" - # title is not defined in M3 - return true if field == "title" - - field_supported?(field) && (multiple_field?(field) || factory_class.singleton_methods.include?(:properties) && factory_class&.properties&.[](field)&.[]("multiple")) - end - - def multiple_field?(field) - form_definition = schema_form_definitions[field.to_sym] - form_definition.nil? ? false : form_definition[:multiple] - end - - # override: we want to directly infer from a property being multiple that we should split when it's a String - # def multiple_metadata(content) - # return unless content - - # case content - # when Nokogiri::XML::NodeSet - # content&.content - # when Array - # content - # when Hash - # Array.wrap(content) - # when String - # String(content).strip.split(Bulkrax.multi_value_element_split_on) - # else - # Array.wrap(content) - # end - # end - - def schema_form_definitions - @schema_form_definitions ||= Hyrax::SimpleSchemaLoader.new.form_definitions_for(schema: factory_class.name.underscore.to_sym) - end - end - - [Bulkrax::HasMatchers, Bulkrax::HasMatchers.singleton_class].each do |mod| - mod.prepend HasMappingExt - end - # WorkType to use as the default if none is specified in the import # Default is the first returned by Hyrax.config.curation_concerns # config.default_work_type = MyWork