Skip to content

Instantly share code, notes, and snippets.

@Chris-Galten
Created December 28, 2011 18:09
Show Gist options
  • Save Chris-Galten/1528941 to your computer and use it in GitHub Desktop.
Save Chris-Galten/1528941 to your computer and use it in GitHub Desktop.
Making categories
step 'the following category records' do |table|
table.hashes.each do |hash|
a = AttributeSet.find_or_create_by(slug: 'myattritubetest', label: 'myattributetest', description: 'myattributeset')
if hash['parent'].empty?
Category.find_or_create_by(title: hash['category'], attribute_set_id: a.id)
else
path = []
# TODO: Rewrite this block
hash['parents'].split(', ').each do |path_element|
tmp = Category.find_or_create_by(title: path_element.strip, attribute_set_id: a.id)
begin
path << tmp.id
rescue NoMethodError
end
end
parent = Category.find_by_slug(hash['parent'].strip.downcase)
parent = Category.find_or_create_by(title: hash['parent'].strip, attribute_set_id: a.id)
begin
parent['_id']
rescue NoMethodError
parent = {'_id' => nil}
end
Category.find_or_create_by(title: hash['category'], attribute_set_id: a.id, parent_id: parent['_id'], path: path )
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment