def generate_menu(name, path)
@menu = Menu.find(:first)
if @menu.nil?
@menu = Menu.new
@menu.path = path
@menu.name = name
end
delete_obsolete(@menu)
recurse_directory(path, @menu)
if @menu.save!
return @menu
else
flash['notice'] = "Menu Generation Failed"
end
end
def recurse_directory(cur_path, object)
for new_path in cur_path.children
if new_path.directory?
# Read in information about the directory
dir = Directory.new
dir.name = new_path.basename.to_s
dir.path = new_path.to_s
# Does this directory already exist in our database?
old_object = object.directories.find_by_path(dir.path)
if old_object.nil?
# If not, add it as a child object of its directory / menu
object.directories << dir
old_object = nil
end
# The directory has been read and is added, now we need to add its contents.
recurse_directory(new_path, dir)
else
# Read in information about the file
file = MenuObject.new
file.extension = File.extname(new_path)
file.name = File.basename(new_path, file.extension)
file.path = new_path.to_s
# Does this file already exist in our database?
old_object = object.menu_objects.find_by_path(file.path)
if old_object.nil?
# If not, add it as a child object of its directory / menu.
object.menu_objects << file
old_object = nil
end
end
end
end