-
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 values into into Spree::OptionValue
- Loading branch information
Flavio Auciello
committed
Apr 3, 2020
1 parent
5f80d56
commit 8aa7b12
Showing
4 changed files
with
88 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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusImporter | ||
module Processors | ||
class OptionValues < Base | ||
def call(context) | ||
@data = context.fetch(:data) | ||
return unless option_value_names.any? | ||
|
||
variant = context.fetch(:variant) | ||
process_option_values(variant) | ||
end | ||
|
||
private | ||
|
||
def process_option_values(variant) | ||
option_value_names.each_with_index do |name, i| | ||
option_type = option_type(variant.product, i) | ||
|
||
v = Spree::OptionValue.find_or_initialize_by( | ||
option_type: option_type, | ||
name: name | ||
) | ||
v.presentation = name | ||
variant.option_values << v | ||
end | ||
end | ||
|
||
def option_type(product, index) | ||
product.option_types.find_by(position: index + 1) | ||
end | ||
|
||
def option_value_names | ||
@option_value_names ||= @data.values_at( | ||
'Option1 Value', | ||
'Option2 Value', | ||
'Option3 Value' | ||
).compact.reject { |t| t == 'Default 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
43 changes: 43 additions & 0 deletions
43
spec/lib/solidus_importer/processors/option_values_spec.rb
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,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe SolidusImporter::Processors::OptionValues do | ||
describe '#call' do | ||
subject(:described_method) { described_class.call(context) } | ||
|
||
let(:context) { { data: data, variant: variant } } | ||
let(:variant) { create :base_variant } | ||
let(:product) { variant.product } | ||
let(:color) { create :option_type, presentation: 'Color' } | ||
let(:size) { create :option_type, presentation: 'Size' } | ||
let(:data) do | ||
{ | ||
'Option1 Name' => 'Size', | ||
'Option1 Value' => 'L', | ||
'Option2 Name' => 'Color', | ||
'Option2 Value' => 'Black' | ||
} | ||
end | ||
|
||
before { product.option_types << size << color } | ||
|
||
context 'when "Option(1,2,3) Value" are present' do | ||
it 'create option values for variant in row' do | ||
expect { described_method }.to change(variant.option_values, :count).from(0).to(2) | ||
expect(variant.option_values.first.presentation).to eq 'L' | ||
expect(variant.option_values.first.name).to eq 'L' | ||
expect(variant.option_values.last.presentation).to eq 'Black' | ||
expect(variant.option_values.last.name).to eq 'Black' | ||
end | ||
|
||
it 'creates "Black" option value for related "Color" option type' do | ||
described_method | ||
black = variant.option_values.find_by(presentation: 'Black') | ||
l = variant.option_values.find_by(presentation: 'L') | ||
expect(black.option_type).to eq color | ||
expect(l.option_type).to eq size | ||
end | ||
end | ||
end | ||
end |