Class: Workarea::HavenDemoProductSeeds

Inherits:
Object
  • Object
show all
Defined in:
app/seeds/workarea/haven_demo_product_seeds.rb

Instance Method Summary collapse

Instance Method Details

#performObject



3
4
5
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
67
68
69
70
71
72
# File 'app/seeds/workarea/haven_demo_product_seeds.rb', line 3

def perform
  puts "Adding Haven Theme Products..."

  Sidekiq::Callbacks.disable do
    Catalog::ProductPlaceholderImage.cached

    Catalog::Category.all.each do |category|
      10.times do
        materials = ["Leather", "Concrete", "Brass", "Ceramic"]
        colors = ["red", "pink", "purple", "blue", "light_blue", "green", "light_green", "yellow", "orange", "grey", "blue_grey", "white", "black"]
        on_sale = rand(10) > 9

        product = Catalog::Product.new(
          name: Faker::Commerce.product_name,
          template: "swatches"
        )

        sku_count = if chance(5)
          1
        else
          3
        end

        sku_count.times do
          sku = Faker::Code.isbn
          sku_price = Faker::Commerce.price.to_m

          details = { "Material" => materials.sample, "Color" => colors.sample }

          product.variants.build(
            sku: sku,
            details: details
          )

          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: sku_price - rand(5).to_m }
            ]
          )
        end

        materials = product.variants.map { |v| v.details["Material"] }.compact
        colors = product.variants.map { |v| v.details["Color"] }.compact

        if materials.present? || colors.present?
          product.filters = { "Materials" => materials.uniq, "Color" => colors.uniq }
        end

        product.new_badge = chance(2)
        product.sale_badge = on_sale
        product.best_seller_badge = chance(9)

        product.description = Faker::Hipster.paragraph
        product.save!

        add_images(product)
      end
    end
  end
end