Class: Nochmal::Adapters::CarrierwaveMigration

Inherits:
Carrierwave show all
Defined in:
lib/nochmal/adapters/carrierwave_migration.rb

Overview

Wrap ActiveStorageHelper and use carrierwave as simulated from_storage_service

Direct Known Subclasses

CarrierwaveResume

Constant Summary

Constants inherited from Carrierwave

Nochmal::Adapters::Carrierwave::PREFIX

Instance Method Summary collapse

Methods inherited from Carrierwave

#blob, #models_with_attachments

Methods inherited from Base

#blob, #count, #empty_collection?, #from_storage_service, #general_notes, #initialize, #list, #model_completed, #models_with_attachments, #to_storage_service, #type_notes, #validate

Constructor Details

This class inherits a constructor from Nochmal::Adapters::Base

Instance Method Details

#attachment_types_for(model) ⇒ Object



7
8
9
10
11
12
# File 'lib/nochmal/adapters/carrierwave_migration.rb', line 7

def attachment_types_for(model)
  @types[model] ||= model.uploaders.map do |uploader, uploader_class|
    @uploaders[model] = { uploader => uploader_class }
    uploader
  end
end

#collection(model, uploader) ⇒ Object



29
30
31
32
33
# File 'lib/nochmal/adapters/carrierwave_migration.rb', line 29

def collection(model, uploader)
  super(model, uploader).tap do |scope|
    type_started(model, uploader, scope.count)
  end
end

#item_completed(record, type, status) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nochmal/adapters/carrierwave_migration.rb', line 54

def item_completed(record, type, status)
  return if @mode == :count
  return unless %i[ok missing].include? status

  MigrationData::Status.find_or_create_by(
    record_id: record.id,
    record_type: record.class.sti_name,
    uploader_type: type,
    filename: blob(record.send(type)).to_s
  ).update(status: status)
end

#reupload(record, type) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nochmal/adapters/carrierwave_migration.rb', line 14

def reupload(record, type)
  pathname = blob(record.send(type))

  if pathname.exist?
    StringIO.open(pathname.read) do |temp|
      record.send(not_prefixed(type)).attach(io: temp, filename: pathname.basename)
    end

    { status: :ok }
  else
    { status: :missing,
      message: MigrationData::Status.new(filename: pathname, record: record).missing_message }
  end
end

#setup(action) ⇒ Object

hooks



37
38
39
40
41
42
43
44
# File 'lib/nochmal/adapters/carrierwave_migration.rb', line 37

def setup(action)
  @mode = action

  return if @mode == :count
  raise MigrationData::StatusExists if MigrationData::Status.table_exists?

  MigrationData::CreateMigrationTables.new.up
end

#teardownObject



46
47
48
49
50
51
52
# File 'lib/nochmal/adapters/carrierwave_migration.rb', line 46

def teardown
  return if @mode == :count
  raise MigrationData::Incomplete unless completely_done?
  return if ENV["NOCHMAL_KEEP_METADATA"].present?

  MigrationData::CreateMigrationTables.new.down
end

#type_completed(model, type) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/nochmal/adapters/carrierwave_migration.rb', line 66

def type_completed(model, type)
  return if @mode == :count

  MigrationData::Meta
    .find_by(record_type: model.sti_name, uploader_type: type)
    .update(migrated: migrated(model, type))
end