Class: Nochmal::Adapters::ActiveStorage
- Inherits:
-
Base
- Object
- Base
- Nochmal::Adapters::ActiveStorage
show all
- Defined in:
- lib/nochmal/adapters/active_storage.rb
Overview
Handles active storage specifics for the Reupload Task
Instance Method Summary
collapse
Methods inherited from Base
#count, #empty_collection?, #general_notes, #initialize, #item_completed, #list, #model_completed, #setup, #teardown, #type_completed, #type_notes
Instance Method Details
#attachment_types_for(model) ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/nochmal/adapters/active_storage.rb', line 35
def attachment_types_for(model)
@types[model] ||=
model
.methods
.map { |method| method.to_s.match(/^with_attached_(\w+)$/)&.captures&.first }
.compact
end
|
#blob(attachment) ⇒ Object
49
50
51
|
# File 'lib/nochmal/adapters/active_storage.rb', line 49
def blob(attachment)
attachment.blob
end
|
#collection(model, type) ⇒ Object
43
44
45
46
47
|
# File 'lib/nochmal/adapters/active_storage.rb', line 43
def collection(model, type)
::ActiveStorage::Attachment
.where(name: type)
.where(record_type: model.base_class.sti_name)
end
|
#from_storage_service(service = @from) ⇒ Object
18
19
20
|
# File 'lib/nochmal/adapters/active_storage.rb', line 18
def from_storage_service(service = @from)
storage_service(service)
end
|
#models_with_attachments ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/nochmal/adapters/active_storage.rb', line 22
def models_with_attachments
@models_with_attachments ||= begin
Rails.application.eager_load!
::ActiveStorage::Attachment
.select(:record_type).distinct.pluck(:record_type)
.compact.map(&:constantize)
.map do |model|
([model] + model.descendants).find { |child_or_self| attachment?(child_or_self) }
end
end
end
|
#reupload(attachment, _type) ⇒ Object
55
56
57
58
59
60
61
62
63
|
# File 'lib/nochmal/adapters/active_storage.rb', line 55
def reupload(attachment, _type)
blob = blob(attachment)
StringIO.open(from_storage_service.download(blob.key)) do |temp|
to_storage_service.upload(blob.key, temp)
end
{ status: :ok }
end
|
#to_storage_service(service = @to) ⇒ Object
14
15
16
|
# File 'lib/nochmal/adapters/active_storage.rb', line 14
def to_storage_service(service = @to)
storage_service(service)
end
|
#validate ⇒ Object
7
8
9
10
11
12
|
# File 'lib/nochmal/adapters/active_storage.rb', line 7
def validate
services_differ = (@to != @from)
message = "It does not make sense to migrate from one service to the same one. from and to should differ."
raise message unless services_differ
end
|