Class: Workarea::ProductsSeeds
- Inherits:
-
Object
- Object
- Workarea::ProductsSeeds
- Defined in:
- app/seeds/workarea/products_seeds.rb
Constant Summary collapse
- SAMPLE_IMAGES_BASE_URL =
"https://github.com/workarea-commerce/workarea/raw/master/core/data/product_images"
- SAMPLE_IMAGES =
(0..39).map { |i| "#{i}.jpg" }
Instance Method Summary collapse
- #download_sample_images_cache ⇒ Object
- #find_random_image ⇒ Object
- #perform ⇒ Object
- #sample_images ⇒ Object
- #sample_images_cache ⇒ Object
- #sample_images_cached? ⇒ Boolean
Instance Method Details
#download_sample_images_cache ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/seeds/workarea/products_seeds.rb', line 92 def download_sample_images_cache SAMPLE_IMAGES.each do |file| headers = { 'Accept' => 'application/vnd.github.v3.raw' } headers['Authorization'] = "token #{ENV['GITHUB_TOKEN']}" if ENV['GITHUB_TOKEN'].present? File.open(sample_images_cache.join(file), 'wb') do |result| url = "#{SAMPLE_IMAGES_BASE_URL}/#{file}" puts "Downloading #{url}..." open(url, headers) { |tmp| result.write(tmp.read) } end end end |
#find_random_image ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'app/seeds/workarea/products_seeds.rb', line 68 def find_random_image # Ensure each image gets used at least once @indexes ||= (0..sample_images.count - 1).to_a next_index = @indexes.shuffle!.pop file = sample_images[next_index || rand(sample_images.size)] File.new(file) end |
#perform ⇒ Object
6 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/seeds/workarea/products_seeds.rb', line 6 def perform puts 'Adding products...' Sidekiq::Callbacks.disable do Catalog::ProductPlaceholderImage.cached Catalog::Category.all.each do |category| 10.times do sizes = Workarea.config.search_facet_size_sort colors = Array.new(3) { Faker::Commerce.color.titleize } on_sale = rand(10) > 9 product = Catalog::Product.new( name: Faker::Commerce.product_name, template: 'option_selects', created_at: (0..3).to_a.sample.days.ago ) 3.times do sku = Faker::Code.isbn sku_price = Faker::Commerce.price.to_m sale_price = sku_price - rand(5).to_m product.variants.build( sku: sku, details: { 'Size' => sizes.sample, 'Color' => colors.sample } ) Inventory::Sku.create!( id: sku, policy: 'standard', available: rand(25) ) Pricing::Sku.create!( id: sku, msrp: sku_price + 10.to_m, tax_code: '001', on_sale: on_sale, prices: [ { regular: sku_price, sale: sale_price < 0 ? 1.to_m : sale_price } ] ) Fulfillment::Sku.create!(id: sku) end sizes = product.variants.map { |v| v.details['Size'] } colors = product.variants.map { |v| v.details['Color'] } product.filters = { 'Size' => sizes.uniq, 'Color' => colors.uniq } product.description = Faker::Hipster.paragraph product.save! if (sample_image = find_random_image).present? product.images.create!(image: sample_image) end end end end end |
#sample_images ⇒ Object
77 78 79 80 81 82 |
# File 'app/seeds/workarea/products_seeds.rb', line 77 def sample_images @sample_images ||= begin download_sample_images_cache unless sample_images_cached? Dir[sample_images_cache.join('*')] end end |
#sample_images_cache ⇒ Object
84 85 86 |
# File 'app/seeds/workarea/products_seeds.rb', line 84 def sample_images_cache Workarea::Core::Engine.root.join('data', 'product_images').tap(&:mkpath) end |
#sample_images_cached? ⇒ Boolean
88 89 90 |
# File 'app/seeds/workarea/products_seeds.rb', line 88 def sample_images_cached? Dir[sample_images_cache.join('*')].many? end |