<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rubyonrails code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 17:44:36 GMT</pubDate>
    <description>DZone Snippets: rubyonrails code</description>
    <item>
      <title>Helper for testing default routes generated by a resource in Ruby on Rails</title>
      <link>http://snippets.dzone.com/posts/show/4517</link>
      <description>These methods test that the routes for resources defined in routes.rb are working as expected. Call them from your functional (controller) tests.&lt;br /&gt;&lt;br /&gt;Add the following 3 methods to test/test_helper.rb (updated for Rails 1.2.5 which no longer uses semicolons as a separator for the edit action):&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Test for routes generated by map.resource (singular).&lt;br /&gt;def assert_routing_for_resource(controller, skip=[], nesting=[])&lt;br /&gt;  routes = [&lt;br /&gt;    ["new",'/new',{},:get], ["create",'',{},:post],&lt;br /&gt;    ["show",'',{},:get], ["edit",'/edit',{},:get],&lt;br /&gt;    ["update",'',{},:put], ["destroy",'',{},:delete]&lt;br /&gt;    ]&lt;br /&gt;  check_resource_routing(controller, routes, skip, nesting)&lt;br /&gt;end&lt;br /&gt;# Test for routes generated by map.resources (plural).&lt;br /&gt;def assert_routing_for_resources(controller, skip=[], nesting=[])&lt;br /&gt;  routes = [&lt;br /&gt;    ["index",'',{},:get], ["new",'/new',{},:get], ["create",'',{},:post],&lt;br /&gt;    ["show",'/1',{:id=&gt;'1'},:get], ["edit",'/1/edit',{:id=&gt;'1'},:get],&lt;br /&gt;    ["update",'/1',{:id=&gt;'1'},:put], ["destroy",'/1',{:id=&gt;'1'},:delete]&lt;br /&gt;    ]&lt;br /&gt;  check_resource_routing(controller, routes, skip, nesting)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Check that the expected paths will be generated by a resource, and that&lt;br /&gt;# the expected params will be generated by paths defined by a resource.&lt;br /&gt;# routes is array of [action, url string after controller, extra params].&lt;br /&gt;def check_resource_routing(controller, routes, skip=[], nesting=[])&lt;br /&gt;  # set a prefix for nested resources&lt;br /&gt;  prefix = nesting.join('s/1/')&lt;br /&gt;  unless prefix.blank?&lt;br /&gt;    prefix += "s/1/"&lt;br /&gt;  end&lt;br /&gt;  # Add params for nested resources.&lt;br /&gt;  # For each 'nest', include a ":nest_id=&gt;'1'" param.&lt;br /&gt;  params = {}&lt;br /&gt;  nesting.each do |param|&lt;br /&gt;    params["#{param}_id".to_sym] = '1'&lt;br /&gt;  end&lt;br /&gt;  # Test each of the standard resource routes.&lt;br /&gt;  routes.each do |pair|&lt;br /&gt;    unless skip.include? pair[0]&lt;br /&gt;      assert_generates("/#{prefix}#{controller}#{pair[1]}",&lt;br /&gt;        {:controller=&gt;controller,&lt;br /&gt;        :action=&gt;pair[0]}.merge(pair[2]).merge(params), {}, {},&lt;br /&gt;        "Failed generation of resource route for action #{pair[0]} /#{prefix}#{controller}#{pair[1]}")&lt;br /&gt;      assert_recognizes(&lt;br /&gt;        {:controller=&gt;controller,&lt;br /&gt;          :action=&gt;pair[0]}.merge(pair[2]).merge(params),&lt;br /&gt;        {:path=&gt;"/#{prefix}#{controller}#{pair[1]}", :method=&gt;pair[3]},&lt;br /&gt;        {}, "Failed to recognize resource route for path #{pair[3]}:/#{prefix}#{controller}#{pair[1]}")&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;EXAMPLES&lt;br /&gt;&lt;br /&gt;You can specify actions to 'skip' (if you have a special route for that action).&lt;br /&gt;If using nested resources, set the nesting array (use singular strings).&lt;br /&gt;&lt;br /&gt;So, if you have the following in routes.rb:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;map.make_thing '/make', :controller=&gt;'things', :action=&gt;'new'&lt;br /&gt;map.resources :nests do |nest|&lt;br /&gt;  nest.resources :things&lt;br /&gt;end&lt;br /&gt;map.resource :foo&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;then you can use the following in things_controller_test.rb:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def test_resource_routing&lt;br /&gt;  assert_routing_for_resources 'things', ['new'], ['nest']&lt;br /&gt;  assert_routing_for_resource 'foo'&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 10 Sep 2007 12:46:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4517</guid>
      <author>grantneufeld (Grant Neufeld)</author>
    </item>
    <item>
      <title>Modified Growl support for Ruby autotest</title>
      <link>http://snippets.dzone.com/posts/show/4514</link>
      <description># This code is a different approach to using Growl notifications (Mac OS X)&lt;br /&gt;# with autotest for Ruby.&lt;br /&gt;# The code can be added to ~/.autotest&lt;br /&gt;# Be sure to comment out "require 'autotest/growl'" if you have that line&lt;br /&gt;# in your .autotest file.&lt;br /&gt;# Based on ZenTest-3.6.1/lib/autotest/growl.rb&lt;br /&gt;# Requires the 2 rails png graphics found at:&lt;br /&gt;# http://blog.internautdesign.com/2006/11/12/autotest-growl-goodness&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module Autotest::Growl&lt;br /&gt;  # save the name of the working directory (e.g., the app name)&lt;br /&gt;  # to be used as a key to identify this particular run of autotest&lt;br /&gt;  # so the same growl notification will be reused for updates,&lt;br /&gt;  # instead of a new growl window for every posting.&lt;br /&gt;  @@current_directory_name = Dir.pwd.match(/([^\/]+)\z/).to_s&lt;br /&gt;  def self.growl title, msg, pri=0&lt;br /&gt;    title += " in #{@@current_directory_name}"&lt;br /&gt;    msg += " at #{Time.now}"&lt;br /&gt;    # ### change the path to the directory for the png graphics:&lt;br /&gt;    system "growlnotify -n autotest #{pri &gt; 0 ? '-s ' : ''}--image /Users/grant/Pictures/rails_#{pri &gt; 0 ? 'fail' : 'ok'}.png -p #{pri} -d '#{Dir.pwd}' -m #{msg.inspect} #{title}"&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  Autotest.add_hook :run do  |at|&lt;br /&gt;    growl "autotest running", "Started"&lt;br /&gt;  end&lt;br /&gt;  Autotest.add_hook :red do |at|&lt;br /&gt;    growl "Tests Failed", "#{at.files_to_test.size} tests failed", 2&lt;br /&gt;  end&lt;br /&gt;  Autotest.add_hook :green do |at|&lt;br /&gt;    growl "Tests Passed", "Tests passed", -2 if at.tainted&lt;br /&gt;  end&lt;br /&gt;  Autotest.add_hook :all_good do |at|&lt;br /&gt;    growl "Tests Passed", "All tests passed", -2 if at.tainted&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 09 Sep 2007 13:22:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4514</guid>
      <author>grantneufeld (Grant Neufeld)</author>
    </item>
  </channel>
</rss>
