Skip to content

Instantly share code, notes, and snippets.

@traels
Last active December 18, 2015 10:29
Show Gist options
  • Save traels/5768747 to your computer and use it in GitHub Desktop.
Save traels/5768747 to your computer and use it in GitHub Desktop.
namespace :import do
task :stuff => :environment do
require 'nokogiri'
taxonomy = Spree::Taxonomy.create!(:name => "Categories")
xref = Hash.new
category_xref = Nokogiri::HTML(open('public/u6Dn_vm_category_xref.xml'))
category_xref.encoding = 'utf-8'
category_xref.xpath('//database/table_data/row').each do |node|
xref[node.at('field[@name="category_child_id"]').text.to_i] = node.at('field[@name="category_parent_id"]').text.to_i
end
#puts xref.inspect
cats = Hash.new
category = Nokogiri::HTML(open('public/u6Dn_vm_category.xml'))
category.encoding = 'utf-8'
category.xpath('//database/table_data/row').each do |node|
cats[node.at('field[@name="category_id"]').text.to_i] = {
:name => node.at('field[@name="category_name"]').text,
:parent => xref[node.at('field[@name="category_id"]').text.to_i],
:taxonomy => taxonomy
}
#puts node.at('field[@name="category_name"]').text
end
taxons = Hash.new
while taxons.count < cats.count
cats.each do |id,cat|
if cat[:parent] == 0
taxons[id] = Spree::Taxon.create!({:name => cat[:name], :taxonomy => cat[:taxonomy], :position => 1, :parent => Spree::Taxon.where(:name => taxonomy.name).first}, :without_protection => true)
else
taxons[id] = Spree::Taxon.create!({:name => cat[:name], :taxonomy => cat[:taxonomy], :position => taxons[cat[:parent]].position + 1, :parent => taxons[cat[:parent]]}, :without_protection => true) unless taxons[cat[:parent]].nil?
end
end
end
puts taxons.inspect
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment