-
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.
Ref. #14 We map the 'Tags' field (which is a comma-separated list of tag) onto a `Spree::Taxon` belonging to a `Tags` taxonomy. We map the `Type` field onto a `Spree::Taxon` beloinging to a `Type` taxonomy.
- Loading branch information
Flavio Auciello
committed
Apr 17, 2020
1 parent
ede9d32
commit 44e85f5
Showing
3 changed files
with
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusImporter | ||
module Processors | ||
class Taxon < Base | ||
attr_accessor :product, :taxonomy | ||
|
||
def call(context) | ||
@data = context.fetch(:data) | ||
|
||
self.product = context.fetch(:product) | ||
|
||
process_taxons_type | ||
process_taxons_tags | ||
end | ||
|
||
private | ||
|
||
def options | ||
@options ||= { | ||
type_taxonomy: Spree::Taxonomy.find_or_create_by(name: 'Type'), | ||
tags_taxonomy: Spree::Taxonomy.find_or_create_by(name: 'Tags') | ||
} | ||
end | ||
|
||
def process_taxons_type | ||
product.taxons << prepare_taxon(type, options[:type_taxonomy]) | ||
end | ||
|
||
def process_taxons_tags | ||
tags.map do |tag| | ||
product.taxons << prepare_taxon(tag, options[:tags_taxonomy]) | ||
end | ||
end | ||
|
||
def prepare_taxon(name, taxonomy) | ||
Spree::Taxon.find_or_initialize_by( | ||
name: name, | ||
taxonomy_id: taxonomy.id | ||
) | ||
end | ||
|
||
def tags | ||
@data['Tags'].split(',').map(&:strip) | ||
end | ||
|
||
def type | ||
@data['Type'] | ||
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe SolidusImporter::Processors::Taxon do | ||
describe '#call' do | ||
subject(:described_method) { described_class.call(context) } | ||
|
||
let!(:shipping_category) { create :shipping_category } | ||
let(:context) { { data: data, product: product } } | ||
let(:product) { create :product } | ||
let(:data) { build(:solidus_importer_row_product, :with_import, :with_type, :with_tags).data } | ||
let(:tags_taxonomy) { Spree::Taxonomy.find_by(name: 'Tags') } | ||
let(:type_taxonomy) { Spree::Taxonomy.find_by(name: 'Type') } | ||
|
||
it 'create a taxon for each tag, related to the given taxonomy' do | ||
expect { described_method }.to change(product.taxons, :count).by(4) | ||
|
||
%w[Type1 Tag1 Tag2 Tag3].each do |taxon_name| | ||
expect(product.taxons.map(&:name)).to include(taxon_name) | ||
end | ||
|
||
expect(Spree::Taxon.find_by(name: 'Tag1').taxonomy).to eq tags_taxonomy | ||
expect(Spree::Taxon.find_by(name: 'Type1').taxonomy).to eq type_taxonomy | ||
end | ||
|
||
it 'creates "Type" and "Tags" taxonomies' do | ||
described_method | ||
expect(type_taxonomy).to be_present | ||
expect(tags_taxonomy).to be_present | ||
end | ||
end | ||
end |