Sha256: a2109657ba21f306b41924ad3e124b6fb632e18a67f51ac5772d07e700ac9fe9

Contents?: true

Size: 1.61 KB

Versions: 9

Compression:

Stored size: 1.61 KB

Contents

require 'csv'

Spree::Sample.load_sample('taxonomies')

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

SPECIAL_TAXONS = { 'New Collection': "Summer #{Date.today.year}", '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.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.taxonomy = categories
  taxon.save!
end

taxon = categories_taxon.children.where(name: 'New').first_or_create!
taxon.taxonomy = categories
taxon.save!

ADDITIONAL_TAXONS.each do |taxon_name|
  taxon = categories_taxon.children.where(name: taxon_name).first_or_create!
  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.taxonomy = categories
  parent.save!

  taxon = parent.children.where(name: taxon_name).first_or_create!
  taxon.taxonomy = categories
  taxon.save!
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
spree_sample-4.5.5 db/samples/taxons.rb
spree_sample-4.4.1 db/samples/taxons.rb
spree_sample-4.5.4 db/samples/taxons.rb
spree_sample-4.5.3 db/samples/taxons.rb
spree_sample-4.5.2 db/samples/taxons.rb
spree_sample-4.5.1 db/samples/taxons.rb
spree_sample-4.5.0 db/samples/taxons.rb
spree_sample-4.4.0 db/samples/taxons.rb
spree_sample-4.4.0.rc2 db/samples/taxons.rb