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
# File 'backend/app/model/ASModel_transfers.rb', line 10

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

    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 (now changed) URI as deleted
    if old_uri
      Tombstone.create(:uri => old_uri)
      DB.after_commit do
        RealtimeIndexing.record_delete(old_uri)
      end

      # Create an event if this is the top-level record being transferred.
      if transfer_group.empty?
        RequestContext.open(:repo_id => target_repository.id) do
          Event.for_repository_transfer(source_repository, target_repository, self)
        end
      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