Created
November 25, 2010 23:17
-
-
Save cmaujean/716060 to your computer and use it in GitHub Desktop.
changing the ordering of taxons based on yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- | |
Wine & Spirits: | |
- | |
Beer: [] | |
- | |
Wine: | |
- Red | |
- White | |
- Blush | |
- Champagne | |
- Sparkling | |
- | |
Spirits: | |
- Whisky | |
- Rum | |
- Vodka | |
- Tequila/Gin | |
- Liqueur | |
- Cognac/Brandy | |
- Other Spirits | |
- | |
Non-Alcoholic: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desc "set up the ordering of taxons using config/categories.yml as a blueprint" | |
task :position_taxons => :environment do | |
TaxonPositioneer.new.go | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the reason the categories-sample is full of arrays, is that arrays allow easy enforcement of ordering.