Skip to content

Commit a683eec

Browse files
Fixes re-running of samples multiple times
Fixes Spree Globalize compatibility errors
1 parent 9ec0aa2 commit a683eec

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

sample/db/samples/products.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@
2020
PRODUCTS.each do |(parent_name, taxon_name, product_name)|
2121
parent = Spree::Taxon.find_by!(name: parent_name)
2222
taxon = parent.children.find_by!(name: taxon_name)
23-
taxon.products.where(name: product_name.titleize).first_or_create! do |product|
23+
Spree::Product.where(name: product_name.titleize).first_or_create! do |product|
2424
product.price = rand(10...100) + 0.99
2525
product.description = FFaker::Lorem.paragraph
2626
product.available_on = Time.zone.now
2727
product.option_types = [color, size]
2828
product.shipping_category = default_shipping_category
2929
product.tax_category = clothing_tax_category
3030
product.sku = [taxon_name.delete(' '), product_name.delete(' '), product.price].join('_')
31-
parent.products << product
31+
product.taxons << parent unless product.taxons.include?(parent)
32+
product.taxons << taxon unless product.taxons.include?(taxon)
3233
end
3334
end
3435

35-
['Bestsellers', 'New', 'Trending', 'Streetstyle', 'Summer Sale'].each do |taxon_name|
36-
Spree::Taxon.find_by!(name: taxon_name).products << Spree::Product.all.sample(30)
36+
Spree::Taxon.where(name: ['Bestsellers', 'New', 'Trending', 'Streetstyle', 'Summer Sale']).each do |taxon|
37+
next if taxon.products.any?
38+
39+
taxon.products << Spree::Product.all.sample(30)
3740
end

sample/db/samples/variants.rb

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,28 @@
1414
VARIANTS.each do |(parent_name, taxon_name, product_name, color_name)|
1515
parent = Spree::Taxon.find_by!(name: parent_name)
1616
taxon = parent.children.find_by!(name: taxon_name)
17-
product = taxon.products.find_by!(name: product_name.titleize)
17+
product = Spree::Product.find_by!(name: product_name.titleize)
1818
color = color_option_values.find_by!(name: color_name)
1919

2020
size_option_values.each do |size|
2121
if parent_name == 'Women' and %w[Dresses Skirts].include?(taxon_name)
2222
length_option_values.each do |length|
2323
option_values = [color, length, size]
24-
product.variants.new do |variant|
24+
product.variants.first_or_create! do |variant|
2525
variant.cost_price = product.price
2626
variant.option_values = option_values
2727
variant.sku = product.sku + '_' + option_values.map(&:name).join('_')
2828
variant.tax_category = clothing_tax_category
29-
variant.save!
3029
end
3130
end
3231
else
3332
option_values = [color, size]
34-
product.variants.new do |variant|
33+
product.variants.first_or_create! do |variant|
3534
variant.cost_price = product.price
3635
variant.option_values = option_values
3736
variant.sku = product.sku + '_' + option_values.map(&:name).join('_')
3837
variant.tax_category = clothing_tax_category
39-
variant.save!
38+
4039
end
4140
end
4241
end

0 commit comments

Comments
 (0)