Friday 18 May 2012

Get models and controllers list inside rails application


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

11 comments:

  1. Works. Congratulations. Thank so much!!!

    ReplyDelete
  2. Good Work! Thanks for help me

    ReplyDelete
  3. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.
    I 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






    ReplyDelete
  4. 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
    python interview questions and answers
    python tutorials
    python course institute in electronic city

    ReplyDelete
  5. 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. 
    AWS Training in Bangalore

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. The website loading pace is incredible.webcare360.com It seems t
    hat you’re doing any distinctive trick. Also,

    ReplyDelete