Skip to content

Commit

Permalink
Add OptionTypes processor
Browse files Browse the repository at this point in the history
Shopify allows a maximum of three key,value options to define product
attributes (I don't know if premium shop permits more options)

We map these keys into into Spree::OptionTypes
  • Loading branch information
Flavio Auciello committed Apr 3, 2020
1 parent 76981e1 commit 5f80d56
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/solidus_importer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions lib/solidus_importer/processors/option_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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)
end

private

def process_option_types(product)
option_type_names.each_with_index do |name, i|
t = product.option_types.find_or_initialize_by(
name: name
)
t.presentation = name
t.name = name.downcase
t.position = i + 1
t.save
end
end

def option_type_names
@option_type_names ||= @data.values_at(
'Option1 Name',
'Option2 Name',
'Option3 Name'
).compact.reject { |t| t == 'Title' }
end
end
end
end
2 changes: 2 additions & 0 deletions spec/features/solidus_importer/import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions spec/lib/solidus_importer/processors/option_types_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5f80d56

Please sign in to comment.