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!(force: false) ⇒ Object
-
#replace_attachment!(attachment, box) ⇒ Object
This is called on the background by the AssetReplacerJob.
- #reset! ⇒ Object
- #verify! ⇒ Object
Instance Method Details
#replace!(force: 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 |
# File 'app/models/effective/asset_replacer.rb', line 15 def replace!(force: false) verify! ().find_in_batches(batch_size: BATCH_SIZE).each do || wait_for_active_job! puts "\nEnqueuing #{attachments.length} attachments with ids #{attachments.first.id} to #{attachments.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) unless force existing = Array(attachable.send(box)) if existing.any? { |obj| obj.respond_to?(:filename) && obj.filename.to_s == asset.file_name } puts("Skipping: #{attachable.class.name} #{attachable.id} #{box} #{asset.file_name}. Already exists."); next end end Effective::AssetReplacerJob.perform_later(, box) end end puts "\nAll Done. Have a great day." true end |
#replace_attachment!(attachment, box) ⇒ Object
This is called on the background by the AssetReplacerJob
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/models/effective/asset_replacer.rb', line 52 def (, box) raise('expected an Effective::Attachment') unless .kind_of?(Effective::Attachment) puts("Processing attachment ##{attachment.id}") asset = .asset attachable = .attachable attachable.upgrading = true if attachable.respond_to?(:upgrading=) 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
114 115 116 |
# File 'app/models/effective/asset_replacer.rb', line 114 def reset! Effective::Attachment.update_all(replaced: false) end |
#verify! ⇒ Object
75 76 77 78 79 80 81 82 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 |
# File 'app/models/effective/asset_replacer.rb', line 75 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 |