This is very very very coooool!
thank you so much. awesome. oh. oh. oh. thanx@!#$
The following piece of code can be used in an .rhtml file to automatically generate a sitemap of all the \ActionControllers and their respective actions. Thanks to everyone on IRC’s #rubyonrails for helping with this.
<ul>
<%
@classes = Hash.new;
ObjectSpace.each_object(ActionController::Base) { |obj|
@classes["#{obj.class}"] = obj
}
@classes.each_value { |c|
@cname = c.controller_name
%>
<li><%= link_to "#{@cname}", { :action => "index", :controller => "#{@cname}" } %></li>
<ul style="font-size:1em">
<%
@methods = c.class.instance_methods(false)
@methods.sort.each { |m|
@mname = m.to_s
next if @mname == "index"
%>
<li><%=link_to "#{@mname}", { :action => "#{@mname}", :controller => "#{@cname}" } %></li>
<%
}
%>
</li></ul>
<%
}
%>
</ul>
Comments
This is rather handy but I think it needs to be tidied up somewhat. That’s far too much code to be sticking into an rhtml file in my opinion. I think this could be tidied away into a helper or something. – Goynang
LeeO: The above code apparently doesn’t work any more (having to do with how files are loaded in more recent versions of Rails). On IRC Ulysses suggested preceeding the ObjectSpace loop with some code to load all the controlles, so they can be found:
require 'find'
Find.find(File.join(RAILS_ROOT, 'app/controllers')) { |name|
require_dependency(name) if /_controller\.rb$/ =~ name
}
Phrogz: Related is DiscoveringControllersAndActions, detailing a way to create a list of all “controller/action” strings, for use in ACL security.
OliverOberlach: the following code is working for me (Rails 0.13):
<ul>
<%
@classes = Hash.new;
require 'find'
Find.find(File.join(RAILS_ROOT, 'app/controllers')) { |name|
require_dependency(name) if /_controller\.rb$/ =~ name
}
ObjectSpace.subclasses_of(ActionController::Base).each do |obj|
@classes["#{obj.controller_name}"] = obj
end
@classes.each_value { |c|
@cname = c.controller_name
%>
<li><%= link_to "#{@cname}", { :action => "index", :controller => "#{@cname}" } %></li>
<ul style="font-size:1em">
<%
@methods = c.public_instance_methods(false)
@methods.sort.each { |m|
@mname = m.to_s
next if @mname == "index"
%>
<li><%=link_to "#{@mname}", { :action => "#{@mname}", :controller => "#{@cname}" } %></li>
<%
}
%>
</li></ul>
<%
}
%>
</ul>
This is very very very coooool!
thank you so much. awesome. oh. oh. oh. thanx@!#$
The following piece of code can be used in an .rhtml file to automatically generate a sitemap of all the \ActionControllers and their respective actions. Thanks to everyone on IRC’s #rubyonrails for helping with this.
<ul>
<%
@classes = Hash.new;
ObjectSpace.each_object(ActionController::Base) { |obj|
@classes["#{obj.class}"] = obj
}
@classes.each_value { |c|
@cname = c.controller_name
%>
<li><%= link_to "#{@cname}", { :action => "index", :controller => "#{@cname}" } %></li>
<ul style="font-size:1em">
<%
@methods = c.class.instance_methods(false)
@methods.sort.each { |m|
@mname = m.to_s
next if @mname == "index"
%>
<li><%=link_to "#{@mname}", { :action => "#{@mname}", :controller => "#{@cname}" } %></li>
<%
}
%>
</li></ul>
<%
}
%>
</ul>
Comments
This is rather handy but I think it needs to be tidied up somewhat. That’s far too much code to be sticking into an rhtml file in my opinion. I think this could be tidied away into a helper or something. – Goynang
LeeO: The above code apparently doesn’t work any more (having to do with how files are loaded in more recent versions of Rails). On IRC Ulysses suggested preceeding the ObjectSpace loop with some code to load all the controlles, so they can be found:
require 'find'
Find.find(File.join(RAILS_ROOT, 'app/controllers')) { |name|
require_dependency(name) if /_controller\.rb$/ =~ name
}
Phrogz: Related is DiscoveringControllersAndActions, detailing a way to create a list of all “controller/action” strings, for use in ACL security.
OliverOberlach: the following code is working for me (Rails 0.13):
<ul>
<%
@classes = Hash.new;
require 'find'
Find.find(File.join(RAILS_ROOT, 'app/controllers')) { |name|
require_dependency(name) if /_controller\.rb$/ =~ name
}
ObjectSpace.subclasses_of(ActionController::Base).each do |obj|
@classes["#{obj.controller_name}"] = obj
end
@classes.each_value { |c|
@cname = c.controller_name
%>
<li><%= link_to "#{@cname}", { :action => "index", :controller => "#{@cname}" } %></li>
<ul style="font-size:1em">
<%
@methods = c.public_instance_methods(false)
@methods.sort.each { |m|
@mname = m.to_s
next if @mname == "index"
%>
<li><%=link_to "#{@mname}", { :action => "#{@mname}", :controller => "#{@cname}" } %></li>
<%
}
%>
</li></ul>
<%
}
%>
</ul>