Hey there,
I am programming a file tree listing for my rails project. I found some
code that seemed to do a nice job that would be perfect after some
styling.
This works smoothly in pure ruby when run on the console. However it
does not produce the results I want when integrated with Rails.
This is the model that is producing the result.
require ‘pathname’
class Filetree < ActiveRecord::Base
$result = []
def self.return_filetree_for_project(id)
$ArmMap = Hash.new("| ")
$ArmMap[""] = ""
$ArmMap["`"] = " "
ARGV << "." if ARGV.empty?
ARGV.map{ |path| visit(Pathname.new("/"), "", "", "",
Pathname.new(path) ) }
return $result
end
private
def self.visit(path, leader, tie, arm, node)
$result << "#{leader}#{arm}#{tie}#{node}<br/>"
visit_children(path + node, leader + $ArmMap[arm])
end
def self.visit_children(path, leader)
return unless FileTest.directory? path
return unless FileTest.readable? path
files = path.children(false).sort #false = return name, not
full path
return if files.empty?
arms = Array.new(files.length - 1, "|") << "`"
pairs = files.zip(arms)
pairs.each { |e| visit(path, leader, "-- ", e[1], e[0]) }
end
end
And the output:
webrick
–port
3000
I have tried using various mixtures of the path, but I can only manage
to produce either this or an empty result. Do you have any ideas?
Thank you in advance,
Christoffer