Tweaking this to get a get a hash of namespaced models

so i have this piece of code here

Dir.glob(RAILS_ROOT + ‘/app/models//*.rb’).each do |file|
@models.push file.gsub(%r{ ^#{RAILS_ROOT + ‘/app/models/’}(.) }io,
‘\1’).chomp(’.rb’)
end

that gives me an array of all my models… its something i tweaked from
the current examples on getting recursive directory listings…
except, it doesnt seem very rubyish… and doesnt return what I really
want…

what i want is some kind of way to have all my models and their
associated namespaces in some kinda hash or array or something that i
can use to refer to them in an administrationy menu…

no, i don’t like hobo or autoadmin or streamlined and don’t need all
of their extended functionality…

and active scaffold doesn’t play well with rails 2.1.0…

thanks,

  • FJM

what i want is some kind of way to have all my models and their
associated namespaces in some kinda hash or array or something that i
can use to refer to them in an administrationy menu…

Don’t know about the namespaces bit, but this will get a list of all
your models:

http://blog.matt-darby.com/2008/05/16/iterating-over-all-models-in-rails/

yeah saw that but as far as i can tell, it only works if those models
are loaded…

ive tried it and didnt get much success… thanks tho.

class ListFiles

def initialize path

@dirs = []

@files = []

@platform = "/"

@platform = "\\" if PLATFORM =~ /win/

@root_path = os_path path



root = Dir.open(@root_path)

scan root

end

def list_files

recurse

@files

end

def recurse

while @dirs != []

  path = @dirs.pop

  dir = Dir.open(path)

  scan dir

end

end

def scan dir

local_path = dir.path

dir.each  do |item|

  path = os_join local_path, item

  (File.directory?(path) ? @dirs.push(path) :  @files.push(path))

unless item.find {|r| r=~/^./ }

end

end

def os_join *args

args.join(@platform)

end

def os_path path

if path.include?("\\")

  p = path.split("\\")

elsif path.include?("/")

  p = path.split("/")

end

path = os_join p

path

end

end

you use it like:

f = ListFiles.new(‘./app/models/’)
f.list_files

and will return an array of files in that directory with paths. it
will ignore .name files (svn git…)

It is one of our home scripts, not very beautifull, but works.

On Jul 1, 2:51 am, “[email protected][email protected]