Class: EasySeeds::Images

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_seeds/images.rb

Class Method Summary collapse

Class Method Details

.attach_images(class_image_names) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/easy_seeds/images.rb', line 7

def self.attach_images(class_image_names)
  begin
    Dir.chdir('../seed_image_files')
  rescue
    Dir.chdir(Dir.pwd + '/db/seed_image_files')
  end

  Dir.glob("*").each_with_index do |seed_file, i|
    headers, data = CSVLoader.unpack_csvs(seed_file)
    class_image_name = class_image_names[i]
    puts "Attaching to #{class_image_name}..."

    data.each_with_index do |row|
      object_id, url, filename = row
      class_instance = class_image_name.find_by_id(object_id)

      begin
        class_instance.image.attach(io: URI.open(url), filename: filename)
        puts "Attached to #{filename}"
      rescue OpenURI::HTTPError
        puts('Waiting 30 seconds before seeding the next row of data, please be patient')
        sleep(30.second)
        class_instance.image.attach(io: URI.open(url), filename: filename)
        puts "Attached to #{filename}"
      end
    end
  end
end