<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rails code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 00:44:45 GMT</pubDate>
    <description>DZone Snippets: rails code</description>
    <item>
      <title>Generate a list of Rails controllers and methods</title>
      <link>http://snippets.dzone.com/posts/show/4792</link>
      <description>This code goes over the App/controllers directory and extracts a list&lt;br /&gt;of all controllers and their methods. &lt;br /&gt;&lt;br /&gt;use this script from rails console. &lt;br /&gt;&lt;br /&gt;I have used this script to generate a migration file with default authorization roles &amp; rights.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;controllers = Dir.new("#{RAILS_ROOT}/app/controllers").entries&lt;br /&gt;controllers.each do |controller|&lt;br /&gt; if controller =~ /_controller/ &lt;br /&gt;  cont = controller.camelize.gsub(".rb","")&lt;br /&gt;  puts cont&lt;br /&gt;  (eval("#{cont}.new.methods") - &lt;br /&gt;    ApplicationController.methods - &lt;br /&gt;    Object.methods -  &lt;br /&gt;    ApplicationController.new.methods).sort.each {|met| &lt;br /&gt;       puts "\t#{met}"&lt;br /&gt;    }&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;to use it to create a Right object I used the following adjustments:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;controllers.each do |controller|&lt;br /&gt;    if controller =~ /_controller/ &lt;br /&gt;    name = controller.camelize.gsub(".rb","")&lt;br /&gt;     (eval("#{name}.new.methods") - &lt;br /&gt;       Object.methods -  &lt;br /&gt;       ApplicationController.new.methods).sort.each {|met| &lt;br /&gt;          name_short = name.gsub("Controller","").downcase&lt;br /&gt;          #it is possible to call the create directly from this script&lt;br /&gt;          #I wanted to review and revise the names. &lt;br /&gt;          puts "#{met}_#{name_short}=Right.create(:name=&gt;\"#{met} #{name_short}\",&lt;br /&gt;                                                  :controller=&gt;\"#{name_short}\",&lt;br /&gt;                                                  :action=&gt;\"#{met}\")"}&lt;br /&gt;  end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 17 Nov 2007 22:16:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4792</guid>
      <author>rubp (john)</author>
    </item>
  </channel>
</rss>
