Module: ASModel::RepositoryTransfers

Defined in:
backend/app/model/ASModel_transfers.rb

Overview

Code for moving records between repositories

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'backend/app/model/ASModel_transfers.rb', line 5

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#transfer_to_repository(target_repository, transfer_group = []) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'backend/app/model/ASModel_transfers.rb', line 10

def transfer_to_repository(target_repository, transfer_group = [])
  if self.class.columns.include?(:repo_id)

    source_repository = Repository[self.repo_id]

    # Do the update in the cheapest way possible (bypassing save hooks, etc.)
    self.class.filter(id: self.id).update(repo_id: target_repository.id,
                                             system_mtime: Time.now)

    # Mark the old URI as deleted (if we can locate the source repo)
    if source_repository
      RequestContext.open(repo_id: source_repository.id) do
        Tombstone.create(uri: self.uri)
        DB.after_commit do
          RealtimeIndexing.record_delete(self.uri)
        end
      end
    end

    RequestContext.open(repo_id: target_repository.id) do
      # if this record has been in the target_repository before, it might have
      # an old tombstone lying around.
      Tombstone.filter(uri: self.uri).delete
      # Create an event if this is the top-level record being transferred.
      if transfer_group.empty?
        Event.for_repository_transfer(source_repository, target_repository, self)
      end
    end
  end

  # Tell any nested records to transfer themselves too
  self.class.nested_records.each do |nested_record_defn|
    association = nested_record_defn[:association][:name]
    association_dataset = self.send("#{association}_dataset".intern)
    nested_model = Kernel.const_get(nested_record_defn[:association][:class_name])

    association_dataset.select(Sequel.qualify(nested_model.table_name, :id)).all.each do |nested_record|
      nested_record.transfer_to_repository(target_repository, transfer_group + [self])
    end
  end
end