Class: Bulkrax::RemoveRelationshipsForImporter

Inherits:
Object
  • Object
show all
Defined in:
app/services/bulkrax/remove_relationships_for_importer.rb

Overview

This module is rather destructive; it will break relationships between the works, file sets, and collections that were imported via an importer. You probably don’t want to run this on your data, except in the case where you have been testing a Bulkrax::Importer, the parsers and mappings. Then, you might have relationships that you want to remove.

tl;dr - Caution this will break things!

Defined Under Namespace

Modules: NullProgressBar

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries:, progress_bar:) ⇒ RemoveRelationshipsForImporter

Returns a new instance of RemoveRelationshipsForImporter.

Parameters:

  • entries (#each)
  • progress_bar (#increment)


46
47
48
49
# File 'app/services/bulkrax/remove_relationships_for_importer.rb', line 46

def initialize(entries:, progress_bar:)
  @progress_bar = progress_bar
  @entries = entries
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



51
52
53
# File 'app/services/bulkrax/remove_relationships_for_importer.rb', line 51

def entries
  @entries
end

#progress_barObject (readonly)

Returns the value of attribute progress_bar.



51
52
53
# File 'app/services/bulkrax/remove_relationships_for_importer.rb', line 51

def progress_bar
  @progress_bar
end

Class Method Details

.break_relationships_for!(importer:, with_progress_bar: false) ⇒ Object

Remove the relationships of the works and collections for all of the Bulkrax::Entry records associated with the given Bulkrax::Importer.

Parameters:



17
18
19
20
21
# File 'app/services/bulkrax/remove_relationships_for_importer.rb', line 17

def self.break_relationships_for!(importer:, with_progress_bar: false)
  entries = importer.entries.select(&:succeeded?)
  progress_bar = build_progress_bar_for(with_progress_bar: with_progress_bar, entries: entries)
  new(progress_bar: progress_bar, entries: entries).break_relationships!
end

.build_progress_bar_for(with_progress_bar:, entries:) ⇒ #increment

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (#increment)


33
34
35
36
37
38
39
40
41
42
# File 'app/services/bulkrax/remove_relationships_for_importer.rb', line 33

def self.build_progress_bar_for(with_progress_bar:, entries:)
  return NullProgressBar unless with_progress_bar

  begin
    require 'ruby-progressbar'
    ProgessBar.create(total: entries.count)
  rescue LoadError
    Rails.logger.info("Using NullProgressBar because ProgressBar is not available due to a LoadError.")
  end
end

Instance Method Details

#break_relationships!Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/services/bulkrax/remove_relationships_for_importer.rb', line 53

def break_relationships!
  entries.each do |entry|
    progress_bar.increment

    obj = entry.factory.find
    next if obj.is_a?(Bulkrax.file_model_class) # FileSets must be attached to a Work

    if obj.is_a?(Bulkrax.collection_model_class)
      remove_relationships_from_collection(obj)
    else
      remove_relationships_from_work(obj)
    end

    obj.try(:reindex_extent=, Hyrax::Adapters::NestingIndexAdapter::LIMITED_REINDEX) if
      defined?(Hyrax::Adapters::NestingIndexAdapter)
    obj.save!
  end
end

#remove_relationships_from_collection(collection) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/services/bulkrax/remove_relationships_for_importer.rb', line 72

def remove_relationships_from_collection(collection)
  # Remove child work relationships
  collection.member_works.each do |work|
    change = work.member_of_collections.delete(collection)
    work.save! if change.present?
  end

  return if defined?(Hyrax)

  # NOTE: This should not need to be migrated to the object factory.
  # Remove parent collection relationships
  collection.member_of_collections.each do |parent_col|
    Hyrax::Collections::NestedCollectionPersistenceService
      .remove_nested_relationship_for(parent: parent_col, child: collection)
  end

  # NOTE: This should not need to be migrated to the object factory.
  # Remove child collection relationships
  collection.member_collections.each do |child_col|
    Hyrax::Collections::NestedCollectionPersistenceService
      .remove_nested_relationship_for(parent: collection, child: child_col)
  end
end

#remove_relationships_from_work(work) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/services/bulkrax/remove_relationships_for_importer.rb', line 96

def remove_relationships_from_work(work)
  # Remove parent collection relationships
  work.member_of_collections = []

  # Remove parent work relationships
  work.member_of_works.each do |parent_work|
    parent_work.members.delete(work)
    parent_work.save!
  end

  # Remove child work relationships
  work.member_works.each do |child_work|
    work.member_works.delete(child_work)
  end
end