Class: Effective::AssetReplacer
- Inherits:
-
Object
- Object
- Effective::AssetReplacer
- Includes:
- ActiveStorage::Blob::Analyzable
- Defined in:
- app/models/effective/asset_replacer.rb
Constant Summary collapse
- BATCH_SIZE =
500
- TIMEOUT =
120
Instance Method Summary collapse
- #replace!(skip_existing: false) ⇒ Object
-
#replace_attachment!(attachment, box) ⇒ Object
This is called on the background by the AssetReplacerJob.
- #reset! ⇒ Object
- #verify! ⇒ Object
Instance Method Details
#replace!(skip_existing: false) ⇒ Object
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 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/models/effective/asset_replacer.rb', line 15 def replace!(skip_existing: false) verify! = ().to_a while(true) wait_for_active_job! puts "\nEnqueuing #{.length} attachments with ids #{.first.id} to #{.last.id}" .each do || asset = .asset attachable = .attachable next if asset.blank? || attachable.blank? box = .box.singularize boxes = .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(, box) end = ().where.not(id: .map(&:id)) break if .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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/models/effective/asset_replacer.rb', line 60 def (, box) raise('expected an Effective::Attachment') unless .kind_of?(Effective::Attachment) puts("Processing attachment ##{.id}") asset = .asset attachable = .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: asset.file_name, content_type: asset.content_type.presence, identify: (asset.content_type.blank?) ) .update_column(:replaced, true) end true end |
#reset! ⇒ Object
122 123 124 |
# File 'app/models/effective/asset_replacer.rb', line 122 def reset! Effective::Attachment.update_all(replaced: false) end |
#verify! ⇒ Object
83 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 |
# File 'app/models/effective/asset_replacer.rb', line 83 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 |