Edoardo "Dado" Marcora
2006-May-23 20:20 UTC
[Rails] How to list all models of an application?!?
How can I get a list of all model classes in the domain of a Rails application (all models, both in "app/models" and in components/somedir/model.rb)? Thanx in advance for your precious help! Edoardo "Dado" Marcora
Here is a god awful hack of some of the code from UserEngine to get controllers. All uglyness is mine and should not reflect poorly on UserEngine. UserEngine is an innocent bystander in this case. Changing the path and the split stuff might make it work for you. # Load all the controller files controller_files = Dir[RAILS_ROOT + "/app/controllers/**/ *_controller.rb"] # should we check to see if this is defined? I.E. will this code ever run # outside of the framework environment...? controller_files += Dir[Engines.config(:root) + "/**/app/ controllers/**/*_controller.rb"] # we need to get all the controller names @controllers = Hash.new controller_files.each do |file_name| file_name = file_name.split("\/").last.chomp("_controller.rb") @controllers[file_name.pluralize] = file_name.titleize #require file_name #if /_controller.rb$/ =~ file_name end This gets me a hash that I use to build a menu for an app. I also use the following to clear certain types out off the hash. def clear_admin(controllers) controllers.delete_if {|key,value| key == "users"} controllers.delete_if {|key,value| key == "logins"} end def clear_lookups(controllers) controllers.delete_if {|key,value| key == "method_of_estimations"} controllers.delete_if {|key,value| key == "method_of_observations"} controllers.delete_if {|key,value| key == "nests_types"} controllers.delete_if {|key,value| key == "decay_classes"} controllers.delete_if {|key,value| key == "food_types"} controllers.delete_if {|key,value| key == "feed_methods"} controllers.delete_if {|key,value| key == "species"} end On May 23, 2006, at 1:20 PM, Edoardo Dado Marcora wrote:> How can I get a list of all model classes in the domain of a Rails > application (all models, both in "app/models" and in > components/somedir/model.rb)? > > Thanx in advance for your precious help! > > Edoardo "Dado" Marcora > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Edoardo "Dado" Marcora
2006-May-24 01:18 UTC
[Rails] Re: How to list all models of an application?!?
Thanx Erik... that helps a lot and it''s probably the way I will go by!!! However, I was wondering whether there is a method that could retrieve the model list somewhere in the "glue" layer that ties together ActionPack and ActiveRecord in Rails. Thanx again, Dado Erik B. Ordway wrote:> Here is a god awful hack of some of the code from UserEngine to get > controllers. All uglyness is mine and should not reflect poorly on > UserEngine. UserEngine is an innocent bystander in this case. > Changing the path and the split stuff might make it work for you. > > > # Load all the controller files > controller_files = Dir[RAILS_ROOT + "/app/controllers/**/ > *_controller.rb"] > > # should we check to see if this is defined? I.E. will this code > ever run > # outside of the framework environment...? > controller_files += Dir[Engines.config(:root) + "/**/app/ > controllers/**/*_controller.rb"] > > # we need to get all the controller names > @controllers = Hash.new > controller_files.each do |file_name| > file_name = file_name.split("\/").last.chomp("_controller.rb") > @controllers[file_name.pluralize] = file_name.titleize > #require file_name #if /_controller.rb$/ =~ file_name > end > > This gets me a hash that I use to build a menu for an app. I also > use the following to clear certain types out off the hash. > def clear_admin(controllers) > controllers.delete_if {|key,value| key == "users"} > controllers.delete_if {|key,value| key == "logins"} > end > > def clear_lookups(controllers) > controllers.delete_if {|key,value| key == "method_of_estimations"} > controllers.delete_if {|key,value| key == "method_of_observations"} > controllers.delete_if {|key,value| key == "nests_types"} > controllers.delete_if {|key,value| key == "decay_classes"} > controllers.delete_if {|key,value| key == "food_types"} > controllers.delete_if {|key,value| key == "feed_methods"} > controllers.delete_if {|key,value| key == "species"} > > end > > > > > > On May 23, 2006, at 1:20 PM, Edoardo Dado Marcora wrote: > >> How can I get a list of all model classes in the domain of a Rails >> application (all models, both in "app/models" and in >> components/somedir/model.rb)? >> >> Thanx in advance for your precious help! >> >> Edoardo "Dado" Marcora >> >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails
Edoardo Marcora wrote:> Thanx Erik... that helps a lot and it''s probably the way I will go by!!! > > However, I was wondering whether there is a method that could retrieve > the > model list somewhere in the "glue" layer that ties together ActionPack > and > ActiveRecord in Rails. > > Thanx again, > > DadoActiveRecord::Base.subclasses j`ey http://www.eachmapinject.com -- Posted via http://www.ruby-forum.com/.
Jonathan Viney
2006-May-24 06:46 UTC
[Rails] Re: How to list all models of an application?!?
ActiveRecord::Bases.subclasses is protected. Object.subclasses_of(ActiveRecord::Base) should do what you want, but you will need to ensure that all the model files have been loaded. This should do it: Dir.glob(RAILS_ROOT + ''/app/models/**/*.rb'').each { |file| require file } -Jonathan. On 5/24/06, j`ey <joey@eachmapinject.com> wrote:> Edoardo Marcora wrote: > > Thanx Erik... that helps a lot and it''s probably the way I will go by!!! > > > > However, I was wondering whether there is a method that could retrieve > > the > > model list somewhere in the "glue" layer that ties together ActionPack > > and > > ActiveRecord in Rails. > > > > Thanx again, > > > > Dado > ActiveRecord::Base.subclasses > > j`ey > http://www.eachmapinject.com > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Jonathan Viney wrote:> ActiveRecord::Bases.subclasses is protected.ActiveRecord::Base.send(:subclasses) j`ey http://www.eachmapinject.com -- Posted via http://www.ruby-forum.com/.
Apparently Analagous Threads
- UserEngine - rake bootstrap aborted => undefined method `synchronize_with_controllers'' for Permission:Class
- UserEngine -- Permission.synchronize_with_controllers -- trouble with my own controller modules
- Role Based Authorization recipe implementation?
- Self-referential join model does not work
- UserEngine - rake bootstrap aborted