Skip to content

Instantly share code, notes, and snippets.

@HoyaBoya
Created February 1, 2014 21:57
Show Gist options
  • Save HoyaBoya/8759537 to your computer and use it in GitHub Desktop.
Save HoyaBoya/8759537 to your computer and use it in GitHub Desktop.
require 'date'
module Bonobos
COLORS = ["brown", "khaki", "olive", "almond", "arizona wash", "ayr", "biscuit", "black", "black ink", "blueberry", "bubble gum", "charcoal", "collage", "cosmic rocks wash", "cosmos", "dove wings", "fade", "geranium", "grey", "heather grey", "ice wash", "jac's jean", "jac's jean with destroy", "la brea tar pits", "little fishes", "look stripe", "marine", "melon", "navy", "nightshade", "poppy", "snow", "stripe", "teacup", "unicorn", "water ice", "wave wash", "white"]
WAISTS = ["24", "25", "26", "27", "28", "30", "31", "29", "32", "34", "36", "38", "40"]
LENGTHS = ["32", "34", "25", "27", "28", "29", "30"]
NUMBER_OF_VARIANTS = 131
class Importer
def load!
# Create the product
product_params = {
name: 'Awesome Pants',
price: default_price,
tax_category: default_tax_category,
shipping_category: default_shipping_category,
sku: 'PANTS',
option_types: [color_option_type, waist_option_type, length_option_type]
}
product = Spree::Product.create!(product_params)
# Create 131 variants
NUMBER_OF_VARIANTS.times do |i|
option_values = []
option_values << find_or_create_option_value(waist_option_type, WAISTS.sample)
option_values << find_or_create_option_value(length_option_type, LENGTHS.sample)
option_values << find_or_create_option_value(color_option_type, COLORS.sample)
variant = Spree::Variant.new
variant.attributes = {
price: default_price,
sku: "PANTS_#{i}",
option_values: option_values,
track_inventory: false
}
product.variants << variant
end
end
def default_tax_category
category = Spree::TaxCategory.find_by_name("Clothing")
if category.nil?
category = Spree::TaxCategory.create!(name: 'Clothing')
end
category
end
def default_shipping_category
category = Spree::ShippingCategory.find_by_name("Default")
if category.nil?
category = Spree::ShippingCategory.create!(name: 'Default')
end
category
end
##
# Find or create the option type.
#
def find_or_create_option_type(name)
option_type = Spree::OptionType.find_by_name(name)
if option_type.nil?
option_type = Spree::OptionType.create!({
name: name.downcase,
presentation: name.titleize
})
end
option_type
end
def fit_option_type
find_or_create_option_type 'fit'
end
def waist_option_type
find_or_create_option_type 'waist'
end
def length_option_type
find_or_create_option_type 'length'
end
def size_option_type
find_or_create_option_type 'size'
end
def color_option_type
find_or_create_option_type 'color'
end
def find_or_create_option_value(option_type, value)
option_value = Spree::OptionValue.where(option_type: option_type, name: value.downcase).first
if option_value.nil?
option_value = create_option_value(option_type, value)
end
option_value
end
def find_option_value(option_type, value)
Spree::OptionValue.where(option_type: option_type, name: value.downcase).first
end
def create_option_value(option_type, value)
option_value = Spree::OptionValue.new(
name: value.downcase,
presentation: value.titleize,
option_type: option_type,
)
option_value.save!
option_value
end
def default_price
99.99
end
end
end
@HoyaBoya
Copy link
Author

HoyaBoya commented Feb 1, 2014

Run in console as Bonobos::Importer.new.load!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment