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
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
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::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.
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
Jonathan V. wrote:
ActiveRecord::Bases.subclasses is protected.
ActiveRecord::Base.send(:subclasses)
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs