 |
Rubyonrails model and controller lists: |
February 6, 2007
In your Rubyonrails app you may at some point need a list of all your controllers, here's how I do it:
def get_controllers
controllers = []
glob = RAILS_ROOT + '/app/controllers/*_controller.rb'
Dir.glob(glob).each do |f|
file = File.basename(f).gsub( /^(.+)_controller.rb/, '\1')
controllers << file
end
controllers
end
Getting a list of all your models is similar:
def get_models
models = []
glob = RAILS_ROOT + '/app/models/*'
Dir.glob(glob).each do |f|
file = File.basename( f ).gsub( /^(.+).rb/, '\1')
models << file
end
models
end
|