One of the basic elements for each medium sized web-sites and above, is the site map.
when using rails, mapping in not so easy because the object oriented routing which makes it hard to distinct between regular actions and some "maintenance" actions.
I decided to prepare a small library (for now, extensions will be welcomed) which contains 3 basic mapping functions.
The first 2 are pretty custom made for my current project and will be irrelevant for the most of you, but the third is brought to you straight from the oven.
def site_map(path = RAILS_ROOT + "/app/controllers/")
collector = Hash.new
tree = Dir.entries(path).delete_if {|entry| entry.slice(0,1) == "."}
tree = tree.delete_if {|entry| entry.include?('application_controller.rb')}
unless tree.empty?
for element in tree
element = element.gsub('_controller', '')
if element.include?('.rb')
element = element.gsub('.rb', '')
collector['File-' + element] = element.camelize
else
collector['Dir-' + element] = site_map(path + '/' + element)
end
end
end
return collector
end
as you probably noticed, the site_map method returns a Hash.
regular controllers are returned with a key in this format. 'File-
Sub folders will be hashed as well and the same, except that the directory element's key will be in the format of 'Dir-
Enjoy.
1 comments:
March 19, 2007 at 2:41 PM
Great idea!
Post a Comment