Sha256: e05079eed4f5072e97e42d0685f56621d72ef7d45c298d957bf252d488df5a95

Contents?: true

Size: 1.97 KB

Versions: 26

Compression:

Stored size: 1.97 KB

Contents

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

        def initialize(product, product_params, options = {})
          @store = options[:store] || Spree::Store.default
          @product = product || Spree::Product.new(product_params)
          @product.stores << @store if @product.stores.exclude?(@store)

          @product_attrs = product_params.to_h
          @variants_attrs = (options[:variants_attrs] || []).map(&:to_h)
          @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(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']).update(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

26 entries across 26 versions & 1 rubygems

Version Path
spree_core-4.10.1 lib/spree/core/importer/product.rb
spree_core-4.10.0 lib/spree/core/importer/product.rb
spree_core-4.9.0 lib/spree/core/importer/product.rb
spree_core-4.8.3 lib/spree/core/importer/product.rb
spree_core-4.8.2 lib/spree/core/importer/product.rb
spree_core-4.7.3 lib/spree/core/importer/product.rb
spree_core-4.6.6 lib/spree/core/importer/product.rb
spree_core-4.5.5 lib/spree/core/importer/product.rb
spree_core-4.4.1 lib/spree/core/importer/product.rb
spree_core-4.5.4 lib/spree/core/importer/product.rb
spree_core-4.6.5 lib/spree/core/importer/product.rb
spree_core-4.7.2 lib/spree/core/importer/product.rb
spree_core-4.7.1 lib/spree/core/importer/product.rb
spree_core-4.6.4 lib/spree/core/importer/product.rb
spree_core-4.7.0 lib/spree/core/importer/product.rb
spree_core-4.6.3 lib/spree/core/importer/product.rb
spree_core-4.6.2 lib/spree/core/importer/product.rb
spree_core-4.6.1 lib/spree/core/importer/product.rb
spree_core-4.6.0 lib/spree/core/importer/product.rb
spree_core-4.5.3 lib/spree/core/importer/product.rb