Files
openfoodnetwork/app/services/image_importer.rb
2021-10-25 21:28:28 -05:00

25 lines
443 B
Ruby

# frozen_string_literal: true
class ImageImporter
def import(url, product)
attach(download(url), product)
end
private
def download(url)
local_file = Tempfile.new
remote_file = open(url)
IO.copy_stream(remote_file, local_file)
local_file
end
def attach(file, product)
Spree::Image.create(
attachment: file,
viewable_id: product.master.id,
viewable_type: Spree::Variant,
)
end
end