Skip to content

Instantly share code, notes, and snippets.

@cmaujean
Created November 25, 2010 23:17
Show Gist options
  • Save cmaujean/716060 to your computer and use it in GitHub Desktop.
Save cmaujean/716060 to your computer and use it in GitHub Desktop.
changing the ordering of taxons based on yaml
---
-
Wine & Spirits:
-
Beer: []
-
Wine:
- Red
- White
- Blush
- Champagne
- Sparkling
-
Spirits:
- Whisky
- Rum
- Vodka
- Tequila/Gin
- Liqueur
- Cognac/Brandy
- Other Spirits
-
Non-Alcoholic: []
desc "set up the ordering of taxons using config/categories.yml as a blueprint"
task :position_taxons => :environment do
TaxonPositioneer.new.go
end
class TaxonPositioneer
attr_reader :taxon_tree # this is for debugging purposes
def initialize
@taxon_tree = YAML::load File.open(RAILS_ROOT + "/config/categories.yml")
end
def go
pos = 1
@taxon_tree.each do |taxonomy|
# now we have a hash with one key
taxonomy.each do |t,v|
puts " * In #{t} assiging position #{pos}:"
parent = Taxon.find_by_name(t)
parent.position = pos
parent.save
pos = pos + 1
"#{v.length} second level taxons"
v.each do |kk|
kk.each do |kkey, vv|
puts " ** Processing the #{kkey} second level assigning position #{pos}"
second = Taxon.find(:first, :conditions => ["name = ?", kkey])
next if second.nil?
second.position = pos
second.save
pos = pos + 1
puts "#{vv.length} third level taxons"
#third level
vv.each do |vvv|
puts "*** doing #{vvv} third level assigning position #{pos}"
third = Taxon.find_by_name(vvv)
next if third.nil?
puts third.class
third.position = pos
third.save
pos = pos + 1
end
end
end
end
end
end
end
@cmaujean
Copy link
Author

the reason the categories-sample is full of arrays, is that arrays allow easy enforcement of ordering.

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