Sha256: 3bd000ec4c4868ea58f50cc5544d42830e4f688692ad585b9b3694be7518e7ed

Contents?: true

Size: 1.97 KB

Versions: 10

Compression:

Stored size: 1.97 KB

Contents

require 'csv'

Spree::Sample.load_sample('taxonomies')

ADDITIONAL_TAXONS = ['Bestsellers', 'Trending', 'Streetstyle', 'Summer Sale'].freeze

SPECIAL_TAXONS = { 'New Collection': 'Summer 2021', 'Special Offers': '30% Off' }.freeze

CHILDREN_TAXON_NAMES = CSV.read(File.join(__dir__, 'variants.csv')).map do |(parent_name, taxon_name, _product_name, _color_name)|
  [parent_name, taxon_name]
end.uniq

TAXON_NAMES = CHILDREN_TAXON_NAMES.map { |(parent_name, taxon_name)| parent_name }

categories = Spree::Taxonomy.find_by!(name: I18n.t('spree.taxonomy_categories_name'))
categories_taxon = Spree::Taxon.where(name: I18n.t('spree.taxonomy_categories_name')).first_or_create!

TAXON_NAMES.each do |taxon_name|
  taxon = categories_taxon.children.where(name: taxon_name).first_or_create!
  taxon.permalink = taxon.permalink.gsub('categories/', '')
  taxon.taxonomy = categories
  taxon.save!
end

CHILDREN_TAXON_NAMES.each do |(parent_name, taxon_name)|
  parent = Spree::Taxon.where(name: parent_name).first
  taxon = parent.children.where(name: taxon_name).first_or_create!
  taxon.permalink = taxon.permalink.gsub('categories/', '')
  taxon.taxonomy = categories
  taxon.save!
end

taxon = categories_taxon.children.where(name: 'New', permalink: 'newest').first_or_create!
taxon.permalink = taxon.permalink.gsub('categories/', '')
taxon.taxonomy = categories
taxon.save!

ADDITIONAL_TAXONS.each do |taxon_name|
  taxon = categories_taxon.children.where(name: taxon_name).first_or_create!
  taxon.permalink = taxon.permalink.gsub('categories/', '')
  taxon.taxonomy = categories
  taxon.save!
end

SPECIAL_TAXONS.each do |parent_name, taxon_name|
  parent = categories_taxon.children.where(name: parent_name.to_s).first_or_create!
  parent.permalink = parent.permalink.gsub('categories/', '')
  parent.taxonomy = categories
  parent.save!

  taxon = parent.children.where(name: taxon_name).first_or_create!
  taxon.permalink = taxon.permalink.gsub('categories/', '')
  taxon.taxonomy = categories
  taxon.save!
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
spree_sample-4.2.7 db/samples/taxons.rb
spree_sample-4.2.6 db/samples/taxons.rb
spree_sample-4.2.5 db/samples/taxons.rb
spree_sample-4.2.4 db/samples/taxons.rb
spree_sample-4.2.3.1 db/samples/taxons.rb
spree_sample-4.2.3 db/samples/taxons.rb
spree_sample-4.2.2 db/samples/taxons.rb
spree_sample-4.2.1 db/samples/taxons.rb
spree_sample-4.2.0 db/samples/taxons.rb
spree_sample-4.2.0.rc5 db/samples/taxons.rb