The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Listing all controllers and actions in Rails

I'm still getting the hang of both Ruby and Rails coming from PHP, but I'm writing a CMS at the moment and the permission system is based on the controllers, actions and IDs.

In the admin area of the app, the administrator needs to be able to add new permissions and rather than have the admin type them in (requiring knowledge of the application code), I'd like to auto-list the controllers and actions within those controllers with select boxes.

The time-consuming way is to keep a list of all the controllers and actions in the app, but it must be possible to list the controllers (by reading the files in the /controllers folder and getting the names from there) and to then retrieve the available methods in each controller class, but I don't know how I'd go about it in Ruby.

Can anyone explain how I could get the list of controllers and then actions within those controllers or point at a resource somewhere else that explains it please?

Luke, I don't think your code will work, because ObjectSpace.each_object(Class) will iterate over all *instances* of Class, that is, all classes. Then you ask: if c.is_a? ActionController::Base, which will never be true, because c is a Class, and not an ActionController::Base.

Just got round to trying this and it occurred that this doesn't quite do what I'm after as I need a list of all controllers in the application, not just those used in the current page (which ObjectSpace gives you).

I presume to do this I'll need to read the list of files in /app/controllers?

Aside from that though, there was only one error in the code - it doesn't like the 'ancestors.include? ActionController::Base' or '.is_a? ActionController::Base' calls, throwing this exception:

uninitialized constant Base

If I'm going to just read the classes in the /controllers dir though, I guess it gets around this problem anyway.

Thanks again for all your help and any further advice you can give on traversing the /controllers directory

Will I just need to include the files then do something along the lines of

After a quick test, using ControllerName:ublic_methods it gives a huge number of extra inherited methods that aren't actions, so the class has to be instantiated before it gives the 'correct' method list.

My problem now is how to create an instance with a variable class name. In PHP I'd just do 'new $var();' but I'm not sure how I'd do it in Ruby.

On the speed issue, it's not too big a problem - this is only to be run once on one page of the admin section when adding a new permission to a user or group so it's not going to affect 99% of the site.