Class: SolidusImportProducts::SaveProduct

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'app/services/solidus_import_products/save_product.rb', line 3

def logger
  @logger
end

#productObject

Returns the value of attribute product.



3
4
5
# File 'app/services/solidus_import_products/save_product.rb', line 3

def product
  @product
end

#product_informationObject

Returns the value of attribute product_information.



3
4
5
# File 'app/services/solidus_import_products/save_product.rb', line 3

def product_information
  @product_information
end

Class Method Details

.call(options = {}) ⇒ Object



7
8
9
# File 'app/services/solidus_import_products/save_product.rb', line 7

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

Instance Method Details

#call(args = { product: nil, product_information: nil }) ⇒ 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
# File 'app/services/solidus_import_products/save_product.rb', line 11

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

  logger.log("SAVE PRODUCT: #{product.inspect}", :debug)

  unless product.valid?
    msg = "A product could not be imported - here is the information we have:\n" \
          "#{product_information}, #{product.inspect} #{product.errors.full_messages.join(', ')}"
    logger.log(msg, :error)
    raise SolidusImportProducts::Exception::ProductError, msg
  end

  product.save

  # Associate our new product with any taxonomies that we need to worry about
  if product_information[:attributes].key?(:taxonomies) && product_information[:attributes][:taxonomies]
    associate_product_with_taxon(product, product_information[:attributes][:taxonomies], true)
  end

  # Finally, attach any images that have been specified
  product_information[:images].each do |filename|
    find_and_attach_image_to(product, filename)
  end

  logger.log("#{product.name} successfully imported.\n")
  true
end