Class: Effective::AssetReplacer

Inherits:
Object
  • Object
show all
Includes:
ActiveStorage::Blob::Analyzable
Defined in:
app/models/effective/asset_replacer.rb

Constant Summary collapse

BATCH_SIZE =
500
TIMEOUT =
120

Instance Method Summary collapse

Instance Method Details

#replace!(skip_existing: false) ⇒ Object



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
51
52
53
54
55
56
57
58
# File 'app/models/effective/asset_replacer.rb', line 16

def replace!(skip_existing: false)
  verify!

  attachments = attachments_to_process().to_a

  while(true)
    wait_for_active_job!

    puts "\nEnqueuing #{attachments.length} attachments with ids #{attachments.first.id} to #{attachments.last.id}"

    attachments.each do |attachment|
      asset = attachment.asset
      attachable = attachment.attachable
      next if asset.blank? || attachable.blank?

      box = attachment.box.singularize
      boxes = attachment.box

      one = attachable.respond_to?(box) && attachable.send(box).kind_of?(ActiveStorage::Attached::One)
      many = attachable.respond_to?(boxes) && attachable.send(boxes).kind_of?(ActiveStorage::Attached::Many)
      box = (one ? box : boxes)

      if skip_existing
        existing = Array(attachable.send(box))

        if existing.any? { |obj| obj.respond_to?(:filename) && obj.filename.to_s == asset.file_name }
          puts("Skipping existing #{attachable.class.name} #{attachable.id} #{box} #{asset.file_name}.")
          next
        end
      end

      Effective::AssetReplacerJob.perform_later(attachment, box)
    end

    attachments = attachments_to_process().where.not(id: attachments.map(&:id))
    break if attachments.to_a.blank?

    GC.start
  end

  puts "\nAll Done. Have a great day."
  true
end

#replace_attachment!(attachment, box) ⇒ Object

This is called on the background by the AssetReplacerJob



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/effective/asset_replacer.rb', line 61

def replace_attachment!(attachment, box)
  raise('expected an Effective::Attachment') unless attachment.kind_of?(Effective::Attachment)
  puts("Processing attachment ##{attachment.id}")

  asset = attachment.asset
  attachable = attachment.attachable

  attachable.replacing_asset = true if attachable.respond_to?(:replacing_asset=)

  Timeout.timeout(TIMEOUT) do
    attachable.send(box).attach(
      io: URI.open(asset.url),
      filename: CGI.unescape(asset.file_name.to_s),
      content_type: asset.content_type.presence,
      identify: (asset.content_type.blank?)
    )

    attachment.update_column(:replaced, true)
  end

  true
end

#reset!Object



123
124
125
# File 'app/models/effective/asset_replacer.rb', line 123

def reset!
  Effective::Attachment.update_all(replaced: false)
end

#verify!Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/effective/asset_replacer.rb', line 84

def verify!
  raise('expected effective assets') unless defined?(Effective::Asset)
  raise('expected active storage') unless defined?(ActiveStorage)

  unless Effective::Attachment.new.respond_to?(:replaced?)
    raise('please add a replaced boolean to Effective::Attachment. add_column :attachments, :replaced, :boolean')
  end

  (Effective::Attachment.all.pluck(:attachable_type, :box).uniq).each do |name, boxes|
    next if name.blank? || boxes.blank?

    box = boxes.singularize

    klass = name.safe_constantize
    raise("invalid class #{klass}") unless klass.present?

    instance = klass.new

    if instance.respond_to?(:effective_assets)
      raise("please remove acts_as_asset_box() from #{klass.name}")
    end

    unless instance.respond_to?(box) || instance.respond_to?(boxes)
      raise("expected #{klass.name} to has_one_attached :#{box} or has_many_attached :#{boxes}")
    end

    one = instance.respond_to?(box) && instance.send(box).kind_of?(ActiveStorage::Attached::One)
    many = instance.respond_to?(boxes) && instance.send(boxes).kind_of?(ActiveStorage::Attached::Many)

    unless one.present? || many.present?
      raise("expected #{klass.name} to has_one_attached :#{box} or has_many_attached :#{boxes}")
    end
  end

  puts 'All attachment classes verified.'

  true
end