Our Rails 3.2 application has admin panel. Admin panel is divided into modules. For instance there is Admin::Searching
. I want to refactor some repetitive and hardcoded partials. For instance:
<ul class='searching_nav'>
<% %w(tests fuzzy stats terms hits).each do |tab| %>
<li class="<%= css_nav(tab) %>"><%= link_to tab.humanize, [:admin, :searching, tab] %></li>
<% end %>
</ul>
It is searching nav section, after adding another controller you have to change it manually.
What I want is, getting list of all controllers in Admin::Searching
(controllers in this namespace are subclasses of Admin::AdminController
). I believe it is possible to do it in elegant way from Rails api. Then it can be generalized for all modules tabs.
I have tried checking Admin::AdminController.subclasses, but classes in that array are lazy loaded so after hitting Terms
for example, after server restart, there is only one element [Admin::Searching::TermsController]
, and after visiting other modules there are also other controllers. I could iterate over them and base on controller_path
results, filter only admin/search
controllers. Maybe it is possible from the routes end? I mean, could I get all controllers in given namespace, from Rails.application.routes.routes?
I do not want to get to filesystem. I know that I could scan admin/searching directory with Dir
but it is inefficient.