<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Internaut Design - Home</title>
  <id>tag:blog.internautdesign.com,2008:mephisto/</id>
  <generator version="0.8.0" uri="http://mephistoblog.com">Mephisto Drax</generator>
  <link href="http://blog.internautdesign.com/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://blog.internautdesign.com/" rel="alternate" type="text/html"/>
  <updated>2008-06-14T22:47:33Z</updated>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>David Lowenfels</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-06-14:236</id>
    <published>2008-06-14T22:34:00Z</published>
    <updated>2008-06-14T22:47:33Z</updated>
    <category term="ActiveRecord"/>
    <category term="rails"/>
    <category term="tricks"/>
    <link href="http://blog.internautdesign.com/2008/6/14/array-of-all-activerecord-models-in-a-rails-app" rel="alternate" type="text/html"/>
    <title>array of all ActiveRecord models in a rails app</title>
<content type="html">
            &lt;p&gt;I recently needed to iterate over all the ActiveRecord models in the &lt;a href=&quot;http://github.com/dfl/factories-and-workers/tree/master/lib/tasks/generate_factories.rake&quot;&gt;rake factory:generate task for factories-and-workers&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;I originally tried to iterate over Object.constants (like &lt;a href=&quot;http://rails.savvica.com/2008/1/4/rake-models-find_invalid&quot;&gt;John Philip Green&#8217;s find_invalid rake task&lt;/a&gt;) but that seemed a bit clunky to me, and didn&#8217;t really work for my purposes.&lt;/p&gt;


Here&#8217;s how I did it:
&lt;pre&gt;
&lt;code class=&quot;ruby&quot;&gt;
  all_models = Dir.glob( File.join( RAILS_ROOT, 'app', 'models', '*.rb') ).map{|path| path[/.+\/(.+).rb/,1] }
  AR_models = all_model_names.select{|m| m.classify.constantize &amp;lt; ActiveRecord::Base}
&lt;/code&gt;
&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>David Lowenfels</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-06-14:234</id>
    <published>2008-06-14T18:57:00Z</published>
    <updated>2008-06-14T19:00:59Z</updated>
    <category term="factories-and-workers"/>
    <category term="plugin"/>
    <category term="rails"/>
    <category term="testing"/>
    <link href="http://blog.internautdesign.com/2008/6/14/rake-task-to-generate-factory-templates" rel="alternate" type="text/html"/>
    <title>rake task to generate factory templates</title>
<content type="html">
            &lt;p&gt;I&#8217;ve been working more on &lt;a href=&quot;http://blog.internautdesign.com/2008/6/4/factories-and-workers-plugin&quot;&gt;factories-and-workers&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Yesterday I came up with the idea to make a rake task to ease the initial entry into factory-land.
It grabs info from the schema, via the model.columns hash, and prints out a factory template, to be pasted and edited in your factories.rb file.&lt;/p&gt;


	&lt;p&gt;Check it out:&lt;/p&gt;


&lt;pre&gt;
&lt;code class=&quot;ruby&quot;&gt;

dfl% rake factory:generate MODEL=task_estimate

factory :task_estimate, {
  :task =&amp;gt; :belongs_to_model,
  :date =&amp;gt; lambda{ Time.zone.today - 7 },
  :hours =&amp;gt; 1.23,
}
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;If called without the &lt;span class=&quot;caps&quot;&gt;MODEL&lt;/span&gt; argument, it will print factory templates for all models.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>Rodney Carvalho</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-06-12:231</id>
    <published>2008-06-12T23:50:00Z</published>
    <updated>2008-06-13T00:04:20Z</updated>
    <category term="rails gotcha"/>
    <link href="http://blog.internautdesign.com/2008/6/12/reverse_merge-on-params-issue" rel="alternate" type="text/html"/>
    <title>reverse_merge! on params issue</title>
<content type="html">
            &lt;p&gt;This was a gotcha for me.  reverse_params! is generally used to set default values on a hash.  In the case of the params hash, that is accessible in the controller from an http action, it uses stringed keys and not symbolic keys.  The following wasn&#8217;t working correctly because my resulting hash had two page keys set; one as a stringed key and one as a symbolic key.&lt;/p&gt;


	&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;BROKEN SCENARIO&lt;/span&gt;:
original params hash is the following: {&#8216;page&#8217;=&amp;gt;&#8216;2&#8217;}
params.reverse_merge!( :page =&amp;gt; 1, :sort_by =&amp;gt; &#8216;new&#8217;, :view_by =&amp;gt; &#8216;all&#8217; )
result: {:page =&amp;gt; 1, :sort_by =&amp;gt; &#8216;new&#8217;, :view_by =&amp;gt; &#8216;all&#8217; , &#8216;page&#8217;=&amp;gt;&#8216;2&#8217;}&lt;/p&gt;


	&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;CORRECT SCENARIO&lt;/span&gt;:
original params hash is the following: {&#8216;page&#8217;=&amp;gt;&#8216;2&#8217;}
params.reverse_merge!( &#8216;page&#8217; =&amp;gt; 1, &#8216;sort_by&#8217; =&amp;gt; &#8216;new&#8217;, &#8216;view_by&#8217; =&amp;gt; &#8216;all&#8217; )
result: {&#8216;page&#8217; =&amp;gt; 1, &#8216;sort_by&#8217; =&amp;gt; &#8216;new&#8217;, &#8216;view_by&#8217; =&amp;gt; &#8216;all&#8217;}&lt;/p&gt;


	&lt;p&gt;Trying to access the :page in the first scenario gives you a value of 1, set by the default values, where you really want 2, passed in to the controller.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>David Lowenfels</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-06-11:229</id>
    <published>2008-06-11T06:52:00Z</published>
    <updated>2008-06-11T06:54:03Z</updated>
    <category term="git"/>
    <category term="plugin"/>
    <category term="rails"/>
    <link href="http://blog.internautdesign.com/2008/6/11/friendly_fixtures-plugin-on-github" rel="alternate" type="text/html"/>
    <title>friendly_fixtures plugin on github</title>
<content type="html">
            &lt;p&gt;I previously posted about my &lt;a href=&quot;http://blog.internautdesign.com/2007/12/10/friendly_fixtures-plugin-for-rails&quot;&gt;friendly_fixtures plugin&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;It&#8217;s now available at github: &lt;a href=&quot;http://github.com/dfl/friendly_fixtures/tree/master&quot;&gt;http://github.com/dfl/friendly_fixtures/tree/master&lt;/a&gt;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>David Lowenfels</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-06-04:226</id>
    <published>2008-06-04T18:20:00Z</published>
    <updated>2008-06-04T18:21:15Z</updated>
    <category term="git"/>
    <category term="railsconf"/>
    <link href="http://blog.internautdesign.com/2008/6/4/finally-getting-git" rel="alternate" type="text/html"/>
    <title>Finally getting git</title>
<content type="html">
            &lt;p&gt;Okay, so I&#8217;m finally starting to understand the concept and rewards of git.
&lt;a href=&quot;https://peepcode.com/products/git-internals-pdf&quot;&gt;Scott Chacon&#8217;s peepcode book on git&lt;/a&gt; was really helpful, and also the &lt;a href=&quot;http://jointheconversation.org/2008/06/02/railsconf-git-talk/&quot;&gt;slides from his railsconf talk&lt;/a&gt;.&lt;/p&gt;


I found contemplation of this statement to be quite enlightening:
&lt;h3&gt;git != svn + magic&lt;/h3&gt;
          </content>  </entry>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>David Lowenfels</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-06-04:225</id>
    <published>2008-06-04T18:04:00Z</published>
    <updated>2008-06-13T08:43:31Z</updated>
    <category term="factories-and-workers"/>
    <category term="fixtures"/>
    <category term="git"/>
    <category term="plugin"/>
    <category term="rails"/>
    <category term="testing"/>
    <link href="http://blog.internautdesign.com/2008/6/4/factories-and-workers-plugin" rel="alternate" type="text/html"/>
    <title>factories-and-workers plugin on github</title>
<content type="html">
            &lt;p&gt;&lt;a href=&quot;http://github.com/dfl/factories-and-workers/&quot;&gt;factories-and-workers at github&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Factories and Workers is a Rails plugin originally written by &lt;a href=&quot;http://myobie.com&quot;&gt;Nathan Herald @ myobie.com&lt;/a&gt; after being inspired by &lt;a href=&quot;http://www.dcmanges.com/blog/38&quot;&gt;Dan Manges&#8217; blog post on factories&lt;/a&gt;.
It uses some slick metaprogramming to generate factory methods for your ActiveRecord models.
Over the past few months I&#8217;ve refactored the code, added a bunch of new features, and most importantly wrote some tests!&lt;/p&gt;


See it in action:
&lt;pre&gt;
&lt;code class=&quot;ruby&quot;&gt;
&amp;gt;&amp;gt; factory :monkey, { :name =&amp;gt; &quot;George&quot; }
=&amp;gt; #&amp;lt;FactoryBuilder:0x1a4f2a8&amp;gt;

&amp;gt;&amp;gt; valid_monkey_attributes
=&amp;gt; {:name=&amp;gt;&quot;George&quot;}

&amp;gt;&amp;gt; build_monkey
=&amp;gt; #&amp;lt;Monkey id: nil, name: &quot;George&quot;&amp;gt;

&amp;gt;&amp;gt; build_monkey( :name =&amp;gt; &quot;Bob&quot; )
=&amp;gt; #&amp;lt;Monkey id: nil, name: &quot;Bob&quot;&amp;gt;

&amp;gt;&amp;gt; Monkey.count
=&amp;gt; 0

&amp;gt;&amp;gt; create_monkey
=&amp;gt; #&amp;lt;Monkey id: 1, name: &quot;George&quot;&amp;gt;

&amp;gt;&amp;gt; Monkey.count
=&amp;gt; 1
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;There&#8217;s much more info in the &lt;a href=&quot;http://github.com/dfl/factories-and-workers/tree/master&quot;&gt;&lt;span class=&quot;caps&quot;&gt;README&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>David Lowenfels</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-04-22:216</id>
    <published>2008-04-22T16:56:00Z</published>
    <updated>2008-06-04T21:25:39Z</updated>
    <category term="git"/>
    <category term="rake"/>
    <category term="subversion"/>
    <link href="http://blog.internautdesign.com/2008/4/22/rake-task-to-svn-add-files" rel="alternate" type="text/html"/>
    <title>rake task to svn add files to project with confirmation</title>
<content type="html">
            &lt;p&gt;Cooked this one up this morning&#8230;
It will iterate through `svn st | grep \?` and ask for confirmation before adding to your subversion project.
Of course, all the cool kids are using git these days anyways ;) Shouldn&#8217;t be hard to modify for that.&lt;/p&gt;


	&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;UPDATE&lt;/span&gt;: actually, there is no need for such a task with git. You can just use &#8220;git commit -a&#8221;.&lt;/p&gt;


&lt;pre&gt;
&lt;code class=&quot;ruby&quot;&gt;
namespace :svn do

  desc &quot;Add files not under (sub)version control&quot; 
  task :add do
    files = `svn st | grep \?`.map{|f| f.gsub(/\?\s+(.+)\n/){$1} }
    puts &quot;No new files to add.&quot; and return if files.empty?
    files.each do |file|
      print &quot;... #{file} [Yn]&quot; 
     `svn add #{file}` unless STDIN.gets =~ /^n/i
    end
  end

end
&lt;/code&gt;
&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>David Lowenfels</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-04-21:215</id>
    <published>2008-04-21T05:01:00Z</published>
    <updated>2008-04-21T05:03:20Z</updated>
    <category term="math"/>
    <category term="ruby"/>
    <link href="http://blog.internautdesign.com/2008/4/21/simple-linear-regression-best-fit" rel="alternate" type="text/html"/>
    <title>simple linear regression (best fit) in ruby</title>
<content type="html">
            &lt;p&gt;I&#8217;m working on a project that does some graphing, I and needed to do a linear best fit.
A quick google search didn&#8217;t find the ruby code snippet I was looking for, so I converted one from &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt;.
( &lt;a href=&quot;http://blog.trungson.com/2005/11/linear-regression-php-class.html&quot;&gt;Original nasty looking quasi-OO &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; source&lt;/a&gt; )
Sorry, no unit tests :)&lt;/p&gt;


&lt;pre&gt;
&lt;code class=&quot;ruby&quot;&gt;
class LinearRegression
  attr_accessor :slope, :offset

  def initialize dx, dy=nil
    @size = dx.size
    dy,dx = dx,axis() unless dy  # make 2D if given 1D
    raise &quot;arguments not same length!&quot; unless @size == dy.size
    sxx = sxy = sx = sy = 0
    dx.zip(dy).each do |x,y|
      sxy += x*y
      sxx += x*x
      sx  += x
      sy  += y
    end
    @slope = ( @size * sxy - sx*sy ) / ( @size * sxx - sx * sx )
    @offset = (sy - @slope*sx) / @size
  end

  def fit
    return axis.map{|data| predict(data) }
  end

  def predict( x )
    y = @slope * x + @offset
  end

  def axis
    (0...@size).to_a
  end
end
&lt;/code&gt;
&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>David Lowenfels</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-03-26:214</id>
    <published>2008-03-26T18:05:00Z</published>
    <updated>2008-03-26T18:07:37Z</updated>
    <category term="delegate"/>
    <category term="rails"/>
    <link href="http://blog.internautdesign.com/2008/3/26/delegate-unless-nil" rel="alternate" type="text/html"/>
    <title>delegate unless nil</title>
<content type="html">
            &lt;p&gt;An issue came up yesterday that involved an strange nil error, where an attribute was being delegated to a nil association.&lt;/p&gt;


	&lt;p&gt;I recalled reading &lt;a href=&quot;http://dev.rubyonrails.org/ticket/4134&quot;&gt;this ticket&lt;/a&gt; opened by court3nay from &lt;a href=&quot;http://www.caboo.se/&quot;&gt;caboo.se&lt;/a&gt;  and thought it might come in handy for others if I put it in the blogosphere.&lt;/p&gt;


	&lt;p&gt;The ticket ended up being closed because it was deemed unnecessary. Someone with the moniker &#8220;protocool&#8221; (presumably trevor at http://protocool.com/) shared the final solution which works out of the box with no patches.
It goes something like this:&lt;/p&gt;


&lt;pre&gt;
&lt;code class=&quot;ruby&quot;&gt;
delegate :some_method, :to =&amp;gt; '(some_association or return nil)'
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The cool thing about this solution, over the proposed patch, is that you can do whatever you like (rather than simply return nil), such as return a default value or raise a custom exception.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>David Lowenfels</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-03-07:213</id>
    <published>2008-03-07T21:55:00Z</published>
    <updated>2008-03-07T22:05:39Z</updated>
    <category term="ruby"/>
    <category term="tricks"/>
    <link href="http://blog.internautdesign.com/2008/3/7/ruby-date-extensions" rel="alternate" type="text/html"/>
    <title>ruby Date extensions</title>
<content type="html">
            &lt;p&gt;I recently cooked these up, thought someone else might find them handy.
Now you can say date.first_of_month, date.first_of_year, etc.&lt;/p&gt;


&lt;pre&gt;
&lt;code class=&quot;ruby&quot;&gt;
module DateExtensions
  def first_of_week
    self - self.wday
  end

  def first_of_month
    self - self.day+1
  end

  def first_of_quarter
    (self &amp;lt;&amp;lt; (self.month%3 - 1 )%3) - self.day+1
  end

  def first_of_year
    (self &amp;lt;&amp;lt; self.month-1) - self.day+1  
  end
end
Date.send :include, DateExtensions
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Addendum:  oops, looks like there are already similar methods in Rails&#8217; ActiveSupport:&lt;/p&gt;


	&lt;p&gt;beginning_of_* rather than first_of_*&lt;/p&gt;


	&lt;p&gt;However, begining_of_week assumes Monday is the first day of the week, while my version assumes Sunday is the first day.&lt;/p&gt;


Here are my tests:
&lt;pre&gt;
&lt;code class=&quot;ruby&quot;&gt;
require File.dirname(__FILE__) + '/../test_helper'

class DateExtensionsTest &amp;lt; Test::Rails::TestCase

  def test_first_of_week
    day = Date.new( 2008, 1, 1 )
    w   = day.first_of_week
    # sunday is first day of week
    assert_equal 0, w.wday
    assert_equal 30, w.day
  end

  def test_first_of_month
    day = Date.new( 2008, 1, 15 )
    assert_equal 1, day.first_of_month.day
  end

  def test_first_of_year
    day = Date.new( 2008, 5, 27 )
    y   = day.first_of_year
    assert_equal 1, y.month
    assert_equal 1, y.day
  end

  def test_first_of_quarter_q1    
    [1,2,3].each do |month|
      day = Date.new( 2008, month, 13 )
      q   = day.first_of_quarter
      assert_equal 1, q.day
      assert_equal 1, q.month
    end
  end

  def test_first_of_quarter_q2
    [4,5,6].each do |month|
      day = Date.new( 2008, month, 13 )
      q   = day.first_of_quarter
      assert_equal 1, q.day
      assert_equal 4, q.month
    end
  end

  def test_first_of_quarter_q3
    [7,8,9].each do |month|
      day = Date.new( 2008, month, 13 )
      q   = day.first_of_quarter
      assert_equal 1, q.day
      assert_equal 7, q.month
    end
  end

  def test_first_of_quarter_q4
    [10,11,12].each do |month|
      day = Date.new( 2008, month, 13 )
      q   = day.first_of_quarter
      assert_equal 1, q.day
      assert_equal 10, q.month
    end    
  end

end
&lt;/code&gt;
&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>David Lowenfels</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-02-23:209</id>
    <published>2008-02-23T18:48:00Z</published>
    <updated>2008-02-23T18:49:28Z</updated>
    <category term="fixtures"/>
    <category term="rails"/>
    <category term="yaml"/>
    <category term="YAML"/>
    <link href="http://blog.internautdesign.com/2008/2/23/creating-yaml-fixtures-from-existing-data" rel="alternate" type="text/html"/>
    <title>creating YAML fixtures from existing data</title>
<content type="html">
            &lt;p&gt;I just came across this &lt;a href=&quot;http://rubyisawesome.com/2007/7/10/mysql-secrets-g-instead-of&quot;&gt;cool tip from Tom Preston-Werner of rubyisawesome.com&lt;/a&gt;.
Basically, if you end a mysql commandline query with \G instead of a semicolon, you&#8217;ll get a nicely formatted query that is suitable for pasting into a &lt;span class=&quot;caps&quot;&gt;YAML&lt;/span&gt; file.&lt;/p&gt;


	&lt;p&gt;But what if you are not using mysql? We have a project with postgres.
There&#8217;s probably a psql command to do something similar, but there is also a database agnostic way:
Just use the rails console! :)&lt;/p&gt;


&lt;pre&gt;
&lt;code class=&quot;ruby&quot;&gt;
&amp;gt;&amp;gt; puts User.find(:all).to_yaml
---
- !ruby/object:User 
  attributes: 
    status: unverified
    salt: L52b2pxGCL
    can_invite: &quot;0&quot; 
    hashed_password: L50/yIQjPCBiU
    is_admin: &quot;0&quot; 
    id: &quot;478674008&quot; 
    first_name: Mickey
    last_name: Mouse
    watchlist_by_email: &quot;0&quot; 
    created_at: 2008-01-30 15:34:26
    email: mickey@mouse.com
&lt;/code&gt;
          </content>  </entry>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>David Lowenfels</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-02-08:208</id>
    <published>2008-02-08T18:06:00Z</published>
    <updated>2008-02-08T18:11:22Z</updated>
    <category term="actionmailer"/>
    <category term="blocks"/>
    <category term="rails"/>
    <category term="ruby"/>
    <category term="testing"/>
    <link href="http://blog.internautdesign.com/2008/2/8/custom-assertion-for-testing-actionmailer-assert_email_sent" rel="alternate" type="text/html"/>
    <title>custom assertion for testing actionmailer: assert_email_sent</title>
<content type="html">
            &lt;p&gt;I&#8217;ve been playing with block helpers for some time now, and I love them for their semantic goodness and clarity.
I know that Rails 2 has an &lt;del&gt;assert_email method, which plays nicely with assert_select&lt;/del&gt; assert_select_email method.
However, as far as I could tell, this doesn&#8217;t allow you to assert any of the headers such as to, from, etc.&lt;/p&gt;


	&lt;p&gt;Enter assert_emails_sent:&lt;/p&gt;


&lt;pre&gt;
&lt;code class=&quot;ruby&quot;&gt;
  def test_send_crash_data
    device_id = 2
    assert_email_sent :to =&amp;gt; AdminNotifier::CLIENT_CRASH_EMAIL,
                      :from =&amp;gt; AdminNotifier::SYSTEM_EMAIL,
                      :subject =&amp;gt; &quot;Application crashed.&quot;,
                      :body =&amp;gt; /device id #{device_id} crashed.+whoops I crashed/ do
      AdminNotifier.deliver_send_crash_data('whoops I crashed', device_id)                      
    end
  end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;ul&gt;
	&lt;li&gt;One cool feature is that the values of :to and :from will be automagically sent #email, so you can just say :to =&amp;gt; @user, as long as @user.responds_to?(:email).&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;
	&lt;li&gt;Another bonus feature is that you can use either strings or regexps.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;grab the code &lt;a href=&quot;http://pastie.caboo.se/149356&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>David Lowenfels</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-02-08:207</id>
    <published>2008-02-08T06:44:00Z</published>
    <updated>2008-02-08T06:45:17Z</updated>
    <category term="ruby"/>
    <category term="shoulda"/>
    <category term="testing"/>
    <link href="http://blog.internautdesign.com/2008/2/8/staying-dry-with-custom-shoulds" rel="alternate" type="text/html"/>
    <title>staying DRY with custom shoulds</title>
<content type="html">
            &lt;p&gt;Recently I&#8217;ve been refactoring legacy Test::Unit code to use &lt;a href=&quot;http://thoughtbot.com/projects/shoulda&quot;&gt;shoulda&lt;/a&gt;.
Sometimes you can&#8217;t factor out repetitive stuff into a context, but you still want to be &lt;span class=&quot;caps&quot;&gt;DRY&lt;/span&gt;.
Below is an example case of refactoring into a custom should.
Note that you need to use class variables, and that the self.should_* method needs to be defined &lt;em&gt;before&lt;/em&gt; it&#8217;s actually used, since shoulda is built upon metaprogramming which gets evaluated at the class level.&lt;/p&gt;


&lt;pre&gt;
&lt;code class=&quot;ruby&quot;&gt;
...

  def self.should_request sym
    sym = sym.to_s
    context &quot;when requesting #{sym}&quot; do
      setup do
        get :download, {:id =&amp;gt; @@podcast.id, :type =&amp;gt; sym}, @@session
      end
      should &quot;ask for a device&quot; do
         assert_match /Please select a device to download content/,  @response.body
      end
      should &quot;increment the message count when sent #add_to_device&quot; do
        assert_difference 'Message.count( :conditions =&amp;gt; &quot;device_id = 1&quot;)', @@podcast.episodes.size do
          xhr :post, :add_to_device, {:id =&amp;gt; 1, :type =&amp;gt; 'all', :podcast_id =&amp;gt; @@podcast.id}, @@session
        end
      end
    end
  end

  context &quot;A valid account&quot; do
    setup do
      @account = accounts(:accounts_002)
      @@session = {:account_id =&amp;gt; @account.id}
      @account.devices &amp;lt;&amp;lt; Device.find(1)
      @@podcast = podcasts(:joe_cartoon)
      @@session.merge!({:podcast_episodes =&amp;gt; @@podcast.episodes})
    end

    should_request :all
    should_request :latest
    should_request :episode
  end

...
&lt;/code&gt;
&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>Rodney Carvalho</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-02-01:205</id>
    <published>2008-02-01T22:21:00Z</published>
    <updated>2008-02-01T22:27:41Z</updated>
    <category term="dog puppy naming name"/>
    <link href="http://blog.internautdesign.com/2008/2/1/namemypuppy-beta-launched" rel="alternate" type="text/html"/>
    <title>NameMyPuppy beta launched</title>
<content type="html">
            &lt;p&gt;NameMyPuppy has been a fun personal project of mine.  It is a way for friends and family to collaborate to help you name your puppy.  You can upload and describe your puppy and then send out invitations to friends and family.  They can suggest names and vote on their favorite names.  You can also make your puppy posting public so anyone on the site can suggest and vote on names to get even more people voting.  Happy naming!&lt;/p&gt;


	&lt;p&gt;&lt;a href=&quot;http://namemypuppy.net&quot;&gt;Name My Puppy&lt;/a&gt;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.internautdesign.com/">
    <author>
      <name>David Lowenfels</name>
    </author>
    <id>tag:blog.internautdesign.com,2008-01-24:200</id>
    <published>2008-01-24T22:40:00Z</published>
    <updated>2008-01-24T22:46:04Z</updated>
    <category term="rails"/>
    <category term="testing"/>
    <category term="textmate"/>
    <link href="http://blog.internautdesign.com/2008/1/24/stack-level-too-deep-error-with-textmate-tests-and-rails-2" rel="alternate" type="text/html"/>
    <title>"stack level too deep" error with textmate tests and Rails 2</title>
<content type="html">
            &lt;p&gt;I was suffering with a problem running tests inside of textmate on a Rails 2 project, until I found this comment by David Vrensk at the bottom of &lt;a href=&quot;http://robsanheim.com/2007/12/07/fixing-textmate-test-issues-blank_slate_method_added-stack-level-too-deep-systemstackerror/&quot;&gt;this blog post by Rob Sanheim&lt;/a&gt;:&lt;/p&gt;


	&lt;p&gt;Posted by David Vrensk
18 January 2008 @ 9am&lt;/p&gt;


	&lt;p&gt;I think the easiest solution can be gleaned from the TM ticket that you link to (http://macromates.com/ticket/show?ticket_id=F4DA8B03). I just modify test/test_helper.rb in my current projects so that it starts with&lt;/p&gt;


	&lt;p&gt;$:.reject! { |e| e.include? ‘TextMate’ }&lt;/p&gt;


	&lt;p&gt;No patching the distros, and svn still works the way it should.&lt;/p&gt;
          </content>  </entry>
</feed>
