Class: SolidusImportProducts::CreateVariant

Inherits:
Object
  • Object
show all
Includes:
ImportHelper
Defined in:
app/services/solidus_import_products/create_variant.rb

Overview

CreateVariant This method assumes that some form of checking has already been done to make sure that we do actually want to create a variant. It performs a similar task to a product, but it also must pick up on size/color options

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'app/services/solidus_import_products/create_variant.rb', line 8

def logger
  @logger
end

#productObject

Returns the value of attribute product.



8
9
10
# File 'app/services/solidus_import_products/create_variant.rb', line 8

def product
  @product
end

#product_informationObject

Returns the value of attribute product_information.



8
9
10
# File 'app/services/solidus_import_products/create_variant.rb', line 8

def product_information
  @product_information
end

#variantObject

Returns the value of attribute variant.



8
9
10
# File 'app/services/solidus_import_products/create_variant.rb', line 8

def variant
  @variant
end

Class Method Details

.call(options = {}) ⇒ Object



12
13
14
# File 'app/services/solidus_import_products/create_variant.rb', line 12

def self.call(options = {})
  new.call(options)
end

Instance Method Details

#call(args = { product: nil, product_information: nil }) ⇒ Object



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
# File 'app/services/solidus_import_products/create_variant.rb', line 16

def call(args = { product: nil, product_information: nil })
  self.logger = SolidusImportProducts::Logger.instance
  self.product_information = args[:product_information]
  self.product = args[:product]
  return if product_information.nil?

  load_or_initialize_variant

  product_information.each do |field, value|
    if field == :variant_options
      value.each { |variant_field, variant_value| options(variant_field, variant_value) }
    elsif field == :attributes
      value.each { |attr_field, attr_value| variant.send("#{attr_field}=", attr_value) if variant.respond_to?("#{attr_field}=") }
    end
  end

  begin
    variant.save

    product_information[:variant_images].each do |filename|
      find_and_attach_image_to(variant, filename)
    end

    stock_items
    logger.log("Variant of SKU #{variant.sku} successfully imported.\n", :debug)
  rescue StandardError => e
    message = "A variant could not be imported - here is the information we have:\n"
    message += "#{product_information}, #{variant.errors.full_messages.join(', ')}\n"
    message += e.message.to_s
    logger.log(message, :error)
    raise SolidusImportProducts::Exception::VariantError, message
  end
  variant
end