Class: ArchiveStorage::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/archive_storage/registry.rb

Instance Method Summary collapse

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
# File 'lib/archive_storage/registry.rb', line 8

def available?
  defined?(::ActiveRecord::Base) &&
    ::ActiveRecord::Base.connected? &&
    record_class.table_exists?
rescue StandardError
  false
end

#claim_candidate(candidate) ⇒ Object Also known as: ensure_for_candidate



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/archive_storage/registry.rb', line 58

def claim_candidate(candidate)
  raise RegistryUnavailableError, "archive_storage_files table is not available" unless available?

  record = record_class.find_or_initialize_by(
    record_type: candidate.record.class.name,
    record_id: candidate.record.id,
    mounted_as: candidate.mounted_as.to_s,
    identifier: candidate.identifier.to_s,
    storage_key: candidate.storage_key.to_s
  )
  return nil unless claimable?(record)

  record.uploader = candidate.uploader.class.name
  record.current_storage = candidate.current_storage.to_s
  record.source_storage = candidate.current_storage.to_s
  record.target_storage = candidate.target_storage.to_s
  record.source_storage_key = candidate.source_storage_key.to_s if record.respond_to?(:source_storage_key=)
  record.target_storage_key = candidate.target_storage_key.to_s if record.respond_to?(:target_storage_key=)
  record.enqueued_at = Time.now if record.respond_to?(:enqueued_at=)
  record.source_delete_pending = false if record.respond_to?(:source_delete_pending=) && record.new_record?
  record.byte_size ||= candidate.byte_size
  record.content_type ||= candidate.content_type
  record.save!
  record
end

#current_storage_for(uploader, identifier:, storage_key:, default:) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/archive_storage/registry.rb', line 29

def current_storage_for(uploader, identifier:, storage_key:, default:)
  find_for_uploader(
    uploader,
    identifier: identifier,
    storage_key: storage_key
  )&.current_storage&.to_sym || default
end

#find_for_uploader(uploader, identifier:, storage_key:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/archive_storage/registry.rb', line 16

def find_for_uploader(uploader, identifier:, storage_key:)
  return nil unless available?
  return nil unless uploader_identity_available?(uploader)

  record_class.find_by(
    record_type: uploader.model.class.name,
    record_id: uploader.model.id,
    mounted_as: uploader.mounted_as.to_s,
    identifier: identifier.to_s,
    storage_key: storage_key.to_s
  )
end

#upsert_for_uploader(uploader, identifier:, storage_key:, current_storage:, metadata: {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/archive_storage/registry.rb', line 37

def upsert_for_uploader(uploader, identifier:, storage_key:, current_storage:, metadata: {})
  return nil unless available?
  return nil unless uploader_identity_available?(uploader)

  record = record_class.find_or_initialize_by(
    record_type: uploader.model.class.name,
    record_id: uploader.model.id,
    mounted_as: uploader.mounted_as.to_s,
    identifier: identifier.to_s,
    storage_key: storage_key.to_s
  )

  record.uploader = uploader.class.name
  record.current_storage = current_storage.to_s
  record.byte_size ||= [:byte_size]
  record.content_type ||= [:content_type]
  record.checksum ||= [:checksum]
  record.save!
  record
end