Get collection of models inside your application.
1. Get table names inside database
ActiveRecord::Base.connection.tables.map do |model|
model.capitalize.singularize.camelize
end
=>["Blog", "User", "UserDetail"]
2. Load models dir
@models = Dir['app/models/*.rb'].map {|f| File.basename(f, '.*').camelize.constantize.name }
3. ActiveRecord subclasses
It will only get base class models not inherited models.
# make sure that relevant models are loaded otherwise
# require them prior
# Dir.glob('#{RAILS_ROOT}/app/models/*.rb').each { |file| require file }
class A < ActiveRecord::Base
end
class B < A
end
class C < B
end
ActiveRecord::Base.subclasses.each{|x| x.name }
=>["A"]
4. Inherited model
It will get both base class models and inherited models using descendants.
# make sure that relevant models are loaded otherwise
# require them prior
# Dir.glob('#{RAILS_ROOT}/app/models/*.rb').each { |file| require file }
class A < ActiveRecord::Base
end
class B < A
end
class C < B
end
ActiveRecord::Base.descendants.each{|x| x.name }
=>["A", "B", "C"]
Get collection of controllers inside your application.
1. ApplicationController subclasses
It will get all controller classes.
ApplicationController.subclasses
=>["AccountsController", "BlogsController", "UsersController"]
2. Inherited Controller
It is get all controller classes along with inherited controller classes.
class AccountsController < ApplicationController
end
class ArticlesController < ::AccountsController
 end
ApplicationController.descendants
=>["AccountsController", "BlogsController", "UsersController", "AccountsController", "ArticlesController"]
Note:
require all files in "#{RAILS_ROOT}/app/controllers" to populate your list in development mode.
It'll get you started, but keep in mind that in development mode you won't see much, because it will only show you what's actually been loaded. Fire it up in production mode, and you should see a list of all of your controllers.
To get all the actions in a controller, use action_methods
PostsController.action_methods
Thanks!!!
ReplyDeleteWorks. Congratulations. Thank so much!!!
ReplyDeleteGood Work! Thanks for help me
ReplyDeletewelcome.
DeleteThis is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.
ReplyDeleteI like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
Java training in Chennai
Java training in Bangalore
Java online training
Java training in Pune
Fantastic work! This is the type of information that should follow collective approximately the web. Embarrassment captivating position Google for not positioning this transmit higher! Enlarge taking place greater than and visit my web situate
ReplyDeletepython interview questions and answers
python tutorials
python course institute in electronic city
Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up.
ReplyDeleteAWS Training in Bangalore
This comment has been removed by the author.
ReplyDeletewe provide affordable and result-oriented SEO services, please give a chance to serve you.keep on share!!!
ReplyDeleteandroid training in chennai
android online training in chennai
android training in bangalore
android training in hyderabad
android Training in coimbatore
android training
android online training
The website loading pace is incredible.webcare360.com It seems t
ReplyDeletehat you’re doing any distinctive trick. Also,
mmorpg oyunlar
ReplyDeleteinstagram takipçi satın al
Tiktok jeton hilesi
Tiktok jeton hilesi
Sac ekim antalya
INSTAGRAM TAKİPÇİ SATİN AL
İnstagram takipçi satın al
Metin pvp
takipçi satın al