Module: Workarea::Factories::Performance::Catalog

Defined in:
lib/workarea/testing/factories/performance/catalog.rb

Instance Method Summary collapse

Instance Method Details

#create_complex_product(overrides = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/workarea/testing/factories/performance/catalog.rb', line 44

def create_complex_product(overrides = {})
  attributes = { variants: [], details: {} }.merge(overrides)

  product = create_product(attributes)
  product.variants = Array.new(100) { create_complex_variant } unless product.variants.any?

  colors = product.variants.flat_map { |v| v.fetch_detail('Color') }.uniq!
  sizes = product.variants.flat_map { |v| v.fetch_detail('Size') }.uniq!
  materials = product.variants.flat_map { |v| v.fetch_detail('Material') }.uniq!

  colors.each do |color|
    product.images.build(image: product_image_file, option: color)
  end

  product.filters = { 'Color' => colors, 'Size' => sizes, 'Material' => materials }
  product.save
  product
end

#create_complex_variantObject



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
# File 'lib/workarea/testing/factories/performance/catalog.rb', line 7

def create_complex_variant
  @sku_counter ||= 0

  details =
    if @sku_counter < variant_details.length
      variant_details[@sku_counter]
    else
      variant_details[@sku_counter % variant_details.length]
    end

  @sku_counter += 1

  attributes = {
    sku: "sku#{@sku_counter}",
    details: details
  }

  create_pricing_sku(
    id: attributes[:sku],
    msrp: (rand * 215.0).round(2),
    prices: [
      { regular: (rand * 101.1).round(2), sale: (rand * 98.6).round(2), min_quantity: 1 },
      { regular: (rand * 101.1).round(2), sale: (rand * 98.6).round(2), min_quantity: 2 },
      { regular: (rand * 101.1).round(2), sale: (rand * 98.6).round(2), min_quantity: 5 }
    ]
  )

  create_inventory(
    id: attributes[:sku],
    policy: 'standard',
    available: (rand * 100).round + 10,
    reserve: (rand * 5).round
  )

  Workarea::Catalog::Variant.new(attributes)
end