Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maybe fix valkyrieobject load issue #802

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions app/matchers/concerns/has_ams_matchers.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/models/bulkrax/csv_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
1 change: 1 addition & 0 deletions app/models/bulkrax/pbcore_xml_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
68 changes: 0 additions & 68 deletions config/initializers/bulkrax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down