Module: Workarea::Factories::Catalog

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.product_image_fileObject



61
62
63
# File 'lib/workarea/testing/factories/catalog.rb', line 61

def self.product_image_file
  IO.read(product_image_file_path)
end

.product_image_file_pathObject



57
58
59
# File 'lib/workarea/testing/factories/catalog.rb', line 57

def self.product_image_file_path
  Testing::Engine.root.join('lib', 'workarea', 'testing', 'product_image.jpg')
end

Instance Method Details

#create_category(overrides = {}) ⇒ Object



6
7
8
9
# File 'lib/workarea/testing/factories/catalog.rb', line 6

def create_category(overrides = {})
  attributes = factory_defaults(:category).merge(overrides)
  Workarea::Catalog::Category.create!(attributes)
end

#create_product(overrides = {}) ⇒ Object



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

def create_product(overrides = {})
  attributes = factory_defaults(:product).merge(overrides)

  Workarea::Catalog::Product.new(attributes.except(:variants)).tap do |product|
    product.id = attributes[:id] if attributes[:id].present?

    if attributes[:variants].present?
      attributes[:variants].each do |attrs|
        pricing_attrs = [
          :regular, :sale, :msrp, :on_sale,
          :tax_code, :discountable
        ]

        sku = Workarea::Pricing::Sku.find_or_create_by(id: attrs[:sku])
        sku.attributes = attrs.slice(
          :on_sale,
          :tax_code,
          :discountable
        )
        sku.prices.build(attrs.slice(:regular, :sale, :msrp))
        sku.save!

        variant_attrs = attrs.except(*pricing_attrs)
        product.variants.build(variant_attrs)
      end
    end

    product.save!
  end
end

#create_product_placeholder_image(overrides = {}) ⇒ Object



42
43
44
45
46
47
# File 'lib/workarea/testing/factories/catalog.rb', line 42

def create_product_placeholder_image(overrides = {})
  attributes = factory_defaults(:product_placeholder_image)
  Workarea::Catalog::ProductPlaceholderImage.create!(
    attributes.merge(overrides)
  )
end

#product_image_fileObject



53
54
55
# File 'lib/workarea/testing/factories/catalog.rb', line 53

def product_image_file
  Factories::Catalog.product_image_file
end

#product_image_file_pathObject



49
50
51
# File 'lib/workarea/testing/factories/catalog.rb', line 49

def product_image_file_path
  Factories::Catalog.product_image_file_path
end