Skip to content

Commit

Permalink
collection_field_mapping removal and replacement - remove deprecation…
Browse files Browse the repository at this point in the history
… warnings (#438)

* initial commit for collection_field_mapping removal

* tweaks and spec fixes

* fix minor lint issues

* fix more specs.

* fix remaining specs

* removed unneeded TODOs and removed unneeded methods/refine transform_attributes

* removed temp methods
  • Loading branch information
sephirothkod authored Mar 23, 2022
1 parent 4949647 commit 0fcdd87
Show file tree
Hide file tree
Showing 29 changed files with 155 additions and 399 deletions.
53 changes: 8 additions & 45 deletions app/factories/bulkrax/object_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 3 additions & 16 deletions app/jobs/bulkrax/create_relationships_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 }
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions app/jobs/bulkrax/importer_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 4 additions & 18 deletions app/models/bulkrax/csv_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -47,7 +44,6 @@ def build_metadata
add_visibility
add_metadata_for_model
add_rights_statement
add_collections
add_local

self.parsed_metadata
Expand All @@ -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
Expand Down Expand Up @@ -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 = []
Expand Down
10 changes: 3 additions & 7 deletions app/models/bulkrax/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
23 changes: 15 additions & 8 deletions app/models/bulkrax/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions app/models/bulkrax/oai_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
12 changes: 2 additions & 10 deletions app/models/bulkrax/rdf_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@ 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 = []
children = []
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
Expand Down Expand Up @@ -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?

Expand Down
6 changes: 1 addition & 5 deletions app/models/bulkrax/xml_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}']"
Expand All @@ -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 = {}
Expand Down
Loading

0 comments on commit 0fcdd87

Please sign in to comment.