diff --git a/lib/solidus_importer/configuration.rb b/lib/solidus_importer/configuration.rb index bc6b53ea..93822d4b 100644 --- a/lib/solidus_importer/configuration.rb +++ b/lib/solidus_importer/configuration.rb @@ -22,6 +22,7 @@ class Configuration < Spree::Preferences::Configuration processors: [ SolidusImporter::Processors::Product, SolidusImporter::Processors::Variant, + SolidusImporter::Processors::OptionTypes, SolidusImporter::Processors::ProductImages, SolidusImporter::Processors::VariantImages, SolidusImporter::Processors::Log diff --git a/lib/solidus_importer/processors/option_types.rb b/lib/solidus_importer/processors/option_types.rb new file mode 100644 index 00000000..3b431312 --- /dev/null +++ b/lib/solidus_importer/processors/option_types.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +module SolidusImporter + module Processors + class OptionTypes < Base + def call(context) + @data = context.fetch(:data) + return unless option_type_names.any? + + product = context.fetch(:product) + process_option_types(product) if option_types? + end + + private + + def process_option_types(product) + option_type_names.each_with_index do |name, i| + option_type = product.option_types.find_or_initialize_by( + name: name + ) + option_type.presentation = name + option_type.name = name.downcase + option_type.position = i + 1 + option_type.save + end + end + + def option_type_names + @option_type_names ||= @data.values_at( + 'Option1 Name', + 'Option2 Name', + 'Option3 Name' + ).compact + end + + def option_types? + @data['Option1 Name'] != 'Title' + end + end + end +end diff --git a/spec/features/solidus_importer/import_spec.rb b/spec/features/solidus_importer/import_spec.rb index 555f4f1e..ba45dfd9 100644 --- a/spec/features/solidus_importer/import_spec.rb +++ b/spec/features/solidus_importer/import_spec.rb @@ -45,6 +45,8 @@ expect(product.variants.count).to eq(3) expect(product.slug).to eq(product_slug) expect(import.state).to eq('completed') + expect(product.images).not_to be_empty + expect(product.option_types.count).to eq 2 expect(Spree::Product.last.images).not_to be_empty expect(Spree::Variant.last.images).not_to be_empty expect(Spree::LogEntry).to have_received(:create!).exactly(csv_file_rows).times diff --git a/spec/lib/solidus_importer/processors/option_types_spec.rb b/spec/lib/solidus_importer/processors/option_types_spec.rb new file mode 100644 index 00000000..313a6d95 --- /dev/null +++ b/spec/lib/solidus_importer/processors/option_types_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe SolidusImporter::Processors::OptionTypes do + describe '#call' do + subject(:described_method) { described_class.call(context) } + + let(:context) { { data: data, product: product } } + let(:product) { create :base_product } + let(:color) { create :option_type, presentation: 'The color', name: 'Color' } + let(:size) { create :option_type, presentation: 'The size', name: 'Size' } + let(:data) do + { + 'Option1 Name' => 'Size', + 'Option2 Name' => 'Color' + } + end + + context 'when "Option(1,2,3) Name" are present' do + context 'when product already has option types' do + before { product.option_types << color << size } + + it 'do not create other option types' do + expect { described_method }.not_to change(product.option_types, :count) + end + end + + it 'create option types for product in row' do + expect { described_method }.to change(product.option_types, :count).from(0).to(2) + expect(product.option_types.first.presentation).to eq 'Size' + expect(product.option_types.first.name).to eq 'size' + expect(product.option_types.last.presentation).to eq 'Color' + expect(product.option_types.last.name).to eq 'color' + end + end + end +end