# Tweaking this to get a get a hash of namespaced models

**URL:** <https://www.ruby-forum.com/t/tweaking-this-to-get-a-get-a-hash-of-namespaced-models/141854>\
**Category:** Rails\
**Created:** [July 1, 2008, 3:42am UTC](https://www.ruby-forum.com/t/tweaking-this-to-get-a-get-a-hash-of-namespaced-models/141854 "2008-07-01T03:42:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 1, 2008, 3:42am UTC](https://www.ruby-forum.com/t/tweaking-this-to-get-a-get-a-hash-of-namespaced-models/141854/1 "2008-07-01T03:42:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 1, 2008, 3:45am UTC](https://www.ruby-forum.com/t/tweaking-this-to-get-a-get-a-hash-of-namespaced-models/141854/2 "2008-07-01T03:45:09Z")

</div>

> 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/](http://blog.matt-darby.com/2008/05/16/iterating-over-all-models-in-rails/)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 1, 2008, 3:52am UTC](https://www.ruby-forum.com/t/tweaking-this-to-get-a-get-a-hash-of-namespaced-models/141854/3 "2008-07-01T03:52:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 1, 2008, 2:16pm UTC](https://www.ruby-forum.com/t/tweaking-this-to-get-a-get-a-hash-of-namespaced-models/141854/4 "2008-07-01T14:16:19Z")

</div>

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, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
