Here's a useful way to get a list of controllers that are in your Rails app. Unlike other approaches that I found on Google, this one does not require a trip to the file system to parse the app/controllers/ directory (although the possible_controllers method in Routing.rb that we're calling actually does just that, but behind the scenes):

_controllers = ActionController::Routing.possible_controllers
# => ["rails/info", "rails_info", "catalogs", "photos", "products", "sessions", "users"]

You may want to filter this to exclude the two 'special' controllers (rails/info and rails_info):

_controllers = ActionController::Routing.possible_controllers - %w{rails/info "rails_info }
# => ["catalogs", "photos", "products", "sessions", "users"]
optic_projector.png

Put aside that one-mirror heliostat before someone gets hurt— there's an easier way to handle introspection for your Rails controllers.


To use one of these names to get a handle on an actual controller:

_c = eval(_controllers.first.camelize + 'Controller') # => CatalogsController

So what is all this good for? Well, consider the possibilities... You can go all kindsa crazy with introspection:

_c.instance_methods.include?('index') # => true

Or find those controllers that you've forgotten to secure behind an authentication_required method:

_unsecured = []
_controllers.each do |c|
  _this = eval(c.camelize + "Controller")
  _unsecured << c unless _this.before_filter.include?(:login_required)
end
puts "The following controllers are wide open to the public: #{_unsecured * ', '}"

Comments

Leave a Comment


IdolHands.com Spam-o-MeterTM
Bot
Spammer
Moron
Human






* Required fields.