-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 10, 2020
1 parent
76981e1
commit ad739c3
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |