Re: Re: getting all controllers for application

Jeremy Kemper
Tue, 10 May 2005 23:22:30 -0000

On May 10, 2005, at 4:00 PM, Florian Groß wrote:
> Craig Beck wrote:
>> The question is there any way of doing this automatically (kinda
>> like  AR extracts columns and db schema) from the application so
>> that I  could modify the application.rhtml once with something
>> like <%=  get_all_controllers_as_links %> and have it
>> automatically keep up  with newly added controllers etc?
>
> I''m not sure if there is a simpler way of doing it, but this ought
> to work:
>
> def all_controllers()
>   result = []
>
>   ObjectSpace.each_object(Class) do |cls|
>     if cls < ApplicationController then
>       result << cls
>     end
>   end
>
>   return result
> end

Controllers are loaded on demand via const_missing so this will miss
any controller that hasn''t handled a request.

Keeping a constant in environment.rb doesn''t seem bad.  However,
greater introspection into controllers and actions would be nice for
an easy-admin scaffolding.

jeremy