<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: controller code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Tue, 13 May 2008 04:54:35 GMT</pubDate>
    <description>DZone Snippets: controller code</description>
    <item>
      <title>Format IPv4 address in octal binary format and vice versa (Ruby / Rails)</title>
      <link>http://snippets.dzone.com/posts/show/4931</link>
      <description>Format an IPv4 address like 192.168.1.1 in dotted binary format like 11000000.10101000.00000001.00000001&lt;br /&gt;You also need this class: http://snippets.dzone.com/posts/show/2472&lt;br /&gt;For Rails: Put this in your controller!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  # convert a dotted decimal IPv4 address to dotted binary format&lt;br /&gt;  def ipv4_to_binary(ipv4addr)&lt;br /&gt;    ia = ipv4addr.to_s.split('.')&lt;br /&gt;    if ia.size != 4&lt;br /&gt;      return "0.0.0.0"&lt;br /&gt;    end&lt;br /&gt;    output = ""&lt;br /&gt;    i = 1&lt;br /&gt;    for octett in ia&lt;br /&gt;      output = output + octett.to_i.to_s(2).using("########","0",true)&lt;br /&gt;      if i &lt; 4&lt;br /&gt;        output = output + "."&lt;br /&gt;      end&lt;br /&gt;      i += 1&lt;br /&gt;    end&lt;br /&gt;    return output&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  &lt;br /&gt;  # convert a IPv4 adress in binary dotted format to a dotted IPv4 address&lt;br /&gt;  def binary_to_ipv4(ipv4addr)&lt;br /&gt;    ia = ipv4addr.to_s.split('.')&lt;br /&gt;    if ia.size != 4&lt;br /&gt;      return "0.0.0.0"&lt;br /&gt;    end&lt;br /&gt;    output = ""&lt;br /&gt;    i = 1&lt;br /&gt;    for octett in ia&lt;br /&gt;      output = output + octett.to_s.to_i(2).to_s&lt;br /&gt;      if i &lt; 4&lt;br /&gt;        output = output + "."&lt;br /&gt;      end&lt;br /&gt;      i += 1&lt;br /&gt;    end&lt;br /&gt;    return output&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 28 Dec 2007 14:12:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4931</guid>
      <author>MichaelWhi (Michael Whittaker)</author>
    </item>
    <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>
    <item>
      <title>#post method in tests with a different controller</title>
      <link>http://snippets.dzone.com/posts/show/2880</link>
      <description>I wanted a #login method in test_helper that would allow me to easily login from any of my functional tests.  However, the #post method won't allow you to set a different controller than the one in the @controller instance variable that's defined in your test's #setup.  Well, by looking at how the &lt;a href="http://api.rubyonrails.org/classes/ActionController/TestProcess.html#M000046"&gt;#process method&lt;/a&gt; works, you can see that it just grabs the controller from @controller.  Redefine that, and you're good to go:&lt;br /&gt;&lt;code&gt;old_controller = @controller&lt;br /&gt;@controller = LoginController.new&lt;br /&gt;post(&lt;br /&gt;  :attempt_login,&lt;br /&gt;  {:user =&gt; {:name =&gt; 'joe', :password =&gt; 'password'}}&lt;br /&gt;)&lt;br /&gt;@controller = old_controller&lt;/code&gt;&lt;br /&gt;If you have several login methods, such as a #login_admin and #login_regular, you could make a wrapper to reduce duplication:&lt;br /&gt;&lt;code&gt;def wrap_with_controller( new_controller = LoginController )&lt;br /&gt;  old_controller = @controller&lt;br /&gt;  @controller = new_controller.new&lt;br /&gt;  yield&lt;br /&gt;  @controller = old_controller&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def login_admin&lt;br /&gt;  wrap_with_controller do&lt;br /&gt;    post(&lt;br /&gt;      :attempt_login,&lt;br /&gt;      {:user =&gt; {:name =&gt; 'root', :password =&gt; 'password'}}&lt;br /&gt;    )&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def login_regular&lt;br /&gt;  wrap_with_controller do&lt;br /&gt;    post(&lt;br /&gt;      :attempt_login,&lt;br /&gt;      {:user =&gt; {:name =&gt; 'joe', :password =&gt; 'password'}}&lt;br /&gt;    )&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 24 Oct 2006 18:54:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2880</guid>
      <author>moneypenny ()</author>
    </item>
    <item>
      <title>rails generator syntax</title>
      <link>http://snippets.dzone.com/posts/show/2800</link>
      <description>// generate application&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rails projectname&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// generate migration.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ruby script/generate migration migration_name&lt;br /&gt;ruby script/generate migration add_price&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// generate model&lt;br /&gt;// we can give a list of columns and types&lt;br /&gt;// :string, :text, :integer, :decimal, :float, :date, ...&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ruby script/generate model model_name&lt;br /&gt;ruby script/generate model user name:string hashed_password:string salt:string&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// generate controller&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ruby script/generate controller controller_name method_name(s)&lt;br /&gt;ruby script/generate controller store index&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// generate scaffold&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ruby script/generate scaffold model_name controller_name&lt;br /&gt;ruby script/generate scaffold product admin&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 08 Oct 2006 18:52:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2800</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
    <item>
      <title>how to escape from AJAX URL</title>
      <link>http://snippets.dzone.com/posts/show/1705</link>
      <description>From Dylan Stamat's post on the Ruby on Rails list 	&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Within my "login.rjs" template that get's invoked after my login process, it returns to the same page with an error message on error, or... if the login was successful, it needs to "redirect" to another controller... which is pretty much impossible to do otherwise.  so, my " login.rjs" looks like:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;if @logged_in_client&lt;br /&gt;  page.replace_html "message", :partial =&gt; 'shared/bad_login'&lt;br /&gt;else&lt;br /&gt;  page.redirect_to "whatever you want here"&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;------ End Message--------&lt;br /&gt;Here is my two cents:&lt;br /&gt;Technically, you can do this without a RJS template at all, because it is such a simple example. Use this in your controller&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;render :update do |page|&lt;br /&gt;  if @logged_in_client&lt;br /&gt;    page.replace_html "message", :partial =&gt; 'shared/bad_login'&lt;br /&gt;  else&lt;br /&gt;    page.redirect_to "whatever you want here"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 16 Mar 2006 08:42:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1705</guid>
      <author>heavysixer ()</author>
    </item>
    <item>
      <title>Firing a controller's action from the console</title>
      <link>http://snippets.dzone.com/posts/show/600</link>
      <description>It seems like a simple task, but here's how you can simulate the calling of a controller's action:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ruby script/console&lt;br /&gt;&lt;br /&gt;irb&gt; require 'action_controller/test_process'&lt;br /&gt;irb&gt; require 'application'&lt;br /&gt;irb&gt; require 'site_controller'&lt;br /&gt;irb&gt; request = ActionController::TestRequest.new&lt;br /&gt;irb&gt; response = ActionController::TestResponse.new&lt;br /&gt;irb&gt; request.env['REQUEST_METHOD'] = 'GET'&lt;br /&gt;irb&gt; request.action = "late_employee"&lt;br /&gt;irb&gt; InfoController.process(request,response)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Basically, it's like getting inside of a TestUnit method, but you have to do the dirty work yourself.</description>
      <pubDate>Fri, 26 Aug 2005 02:44:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/600</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
    <item>
      <title>Render a partial to an instance variable</title>
      <link>http://snippets.dzone.com/posts/show/396</link>
      <description>Normally, if you call "render_partial" within a controller, nothing but the partial will be rendered.&lt;br /&gt;&lt;br /&gt;Occasionally, it is useful to render a partial to an instance variable as a string so that the view can still be rendered as normal, and the string can be passed in to the view.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  add_variables_to_assigns&lt;br /&gt;  @content_for_navbar = @template.render_partial 'layouts/public_navbar'&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 19 Jun 2005 15:27:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/396</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
  </channel>
</rss>
