Sha256: 1feddb32aaf49e7c5b1b64bda47783dfb868df84f1eb1d61b7649eb5a16cc74f

Contents?: true

Size: 1.87 KB

Versions: 90

Compression:

Stored size: 1.87 KB

Contents

module Spree
  module Core
    module Importer
      class Product
        attr_reader :product, :product_attrs, :variants_attrs, :options_attrs

        def initialize(product, product_params, options = {})
          @product = product || Spree::Product.new(product_params)

          @product_attrs = product_params
          @variants_attrs = options[:variants_attrs] || []
          @options_attrs = options[:options_attrs] || []
        end

        def create
          if product.save
            variants_attrs.each do |variant_attribute|
              # make sure the product is assigned before the options=
              product.variants.create({ product: product }.merge(variant_attribute))
            end

            set_up_options
          end

          product
        end

        def update
          if product.update_attributes(product_attrs)
            variants_attrs.each do |variant_attribute|
              # update the variant if the id is present in the payload
              if variant_attribute['id'].present?
                product.variants.find(variant_attribute['id'].to_i).update_attributes(variant_attribute)
              else
                # make sure the product is assigned before the options=
                product.variants.create({ product: product }.merge(variant_attribute))
              end
            end

            set_up_options
          end

          product
        end

        private
          def set_up_options
            options_attrs.each do |name|
              option_type = Spree::OptionType.where(name: name).first_or_initialize do |option_type|
                option_type.presentation = name
                option_type.save!
              end

              unless product.option_types.include?(option_type)
                product.option_types << option_type
              end
            end
          end
      end
    end
  end
end

Version data entries

90 entries across 90 versions & 2 rubygems

Version Path
spree_core-2.4.0 lib/spree/core/importer/product.rb
spree_core-2.3.5 lib/spree/core/importer/product.rb
spree_core-2.4.0.rc3 lib/spree/core/importer/product.rb
spree_core-2.4.0.rc2 lib/spree/core/importer/product.rb
spree_core-2.4.0.rc1 lib/spree/core/importer/product.rb
spree_core-2.3.4 lib/spree/core/importer/product.rb
spree_core-2.3.3 lib/spree/core/importer/product.rb
spree_core-2.3.2 lib/spree/core/importer/product.rb
spree_core-2.3.1 lib/spree/core/importer/product.rb
spree_core-2.3.0 lib/spree/core/importer/product.rb