<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Paul Gross's Blog - Home</title>
  <id>tag:www.pgrs.net,2008:mephisto/</id>
  <generator version="0.7.3" uri="http://mephistoblog.com">Mephisto Noh-Varr</generator>
  <link href="http://www.pgrs.net/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://www.pgrs.net/" rel="alternate" type="text/html"/>
  <updated>2008-06-19T19:35:34Z</updated>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-06-19:2118</id>
    <published>2008-06-19T19:34:00Z</published>
    <updated>2008-06-19T19:35:34Z</updated>
    <link href="http://www.pgrs.net/2008/6/19/sendfile-does-not-work-on-live-cds" rel="alternate" type="text/html"/>
    <title>Sendfile does not work on Live CDs</title>
<content type="html">
            &lt;p&gt;&lt;a href=&quot;http://workingwithrails.com/person/6754-kent-spillner&quot;&gt;Kent Spillner&lt;/a&gt; and I were trying to set up an Ubuntu Live CD with a full rails stack running our current project.  We were seeing weird issues with &lt;a href=&quot;http://wiki.codemongers.com/Main&quot;&gt;nginx&lt;/a&gt;, such as connections being unexpectedly terminated and file uploads not working.&lt;/p&gt;


	&lt;p&gt;We asked around and &lt;a href=&quot;http://www.chris-read.net&quot;&gt;Chris Read&lt;/a&gt; pointed out that Live CDs use the UnionFS filesystem which does not support sendfile.&lt;/p&gt;


	&lt;p&gt;From the &lt;a href=&quot;http://www.am-utils.org/docs/unionfs-tos/unionfs.html&quot;&gt;Versatility and Unix Semantics in Namespace Unification&lt;/a&gt; paper:&lt;/p&gt;


	&lt;p&gt;&#8221;...the sendfile system call requires a matching file structure and address space structure. As Unionfs presently has no address space structure, we cannot properly implement sendfile, which is required for loop device mounts and improves performance for Web and &lt;span class=&quot;caps&quot;&gt;NFS&lt;/span&gt; servers.&#8221;&lt;/p&gt;


	&lt;p&gt;Once we turned off sendfile (&lt;a href=&quot;http://wiki.codemongers.com/NginxHttpCoreModule#sendfile&quot;&gt;http://wiki.codemongers.com/NginxHttpCoreModule#sendfile&lt;/a&gt;), everything worked perfectly.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-06-11:2032</id>
    <published>2008-06-11T04:20:00Z</published>
    <updated>2008-06-11T04:21:32Z</updated>
    <link href="http://www.pgrs.net/2008/6/11/announcing-new-plugin-rake_commit_tasks" rel="alternate" type="text/html"/>
    <title>Announcing new plugin: rake_commit_tasks</title>
<content type="html">
            &lt;p&gt;The last few projects I&#8217;ve been on have had a &#8220;rake commit&#8221; task for checking in.  This is documented at Jay&#8217;s blog: &lt;a href=&quot;http://blog.jayfields.com/2006/12/ruby-rake-commit.html&quot;&gt;Ruby: rake commit&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Each project has improved upon these tasks.  As a result, I thought it would be handy to create a plugin that projects can start with and contribute to.  Though the tasks are about subversion, I started a project on github for better collaboration: &lt;a href=&quot;http://github.com/pgr0ss/rake_commit_tasks/tree/master&quot;&gt;http://github.com/pgr0ss/rake_commit_tasks/tree/master&lt;/a&gt;&lt;/p&gt;


I started with the code at Jay&#8217;s blog and added some stuff from my last few projects:
	&lt;ul&gt;
	&lt;li&gt;Use readline instead of gets so you can use the arrow keys and backspace&lt;/li&gt;
		&lt;li&gt;Break if there are conflicts in svn up&lt;/li&gt;
		&lt;li&gt;Don&#8217;t add result of conflicts to subversion (.mine, etc)&lt;/li&gt;
		&lt;li&gt;Check cc.rb and prompt if build is failing&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Comments and patches are welcome.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-06-10:2029</id>
    <published>2008-06-10T03:06:00Z</published>
    <updated>2008-06-10T03:06:51Z</updated>
    <link href="http://www.pgrs.net/2008/6/10/announcing-pulse-0-3-0" rel="alternate" type="text/html"/>
    <title>Announcing pulse-0.3.0</title>
<content type="html">
            &lt;p&gt;I just released a new version of &lt;a href=&quot;http://pulse.rubyforge.org&quot;&gt;pulse&lt;/a&gt;.  &lt;a href=&quot;http://pulse.rubyforge.org&quot;&gt;Pulse&lt;/a&gt; adds an action to your rails project that can be used for external health checking. The most common use is by a http proxy such as &lt;a href=&quot;http://haproxy.1wt.eu/&quot;&gt;haproxy&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;This version returns a response with html and body tags, which fixes an issue with selenium on Internet Explorer.  Thanks to &lt;a href=&quot;http://www.aliaghareza.com/&quot;&gt;Ali Aghareza&lt;/a&gt; and &lt;a href=&quot;http://brandonbyars.com/blog/&quot;&gt;Brandon Byars&lt;/a&gt; for discovering the problem and sending me a patch.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-06-01:1937</id>
    <published>2008-06-01T18:50:00Z</published>
    <updated>2008-06-01T18:51:56Z</updated>
    <link href="http://www.pgrs.net/2008/6/1/the-state-of-flex-development" rel="alternate" type="text/html"/>
    <title>The state of Flex development</title>
<content type="html">
            &lt;p&gt;I&#8217;m currently working on a project which has a &lt;a href=&quot;http://www.adobe.com/products/flex/&quot;&gt;Flex&lt;/a&gt; front-end talking to a ruby on rails back-end.  Someone recently asked me what I thought of Flex, and my response is below.  I&#8217;ve only been working with Flex for a couple of months, so it&#8217;s very possible that there are solutions to the problems listed below.  If anyone knows a better way of doing it, please let me know.&lt;/p&gt;


	&lt;p&gt;Overall, I find the Flex (and ActionScript 3) world annoying and less productive than either ruby or java.&lt;/p&gt;


	&lt;h3&gt;Testing&lt;/h3&gt;


	&lt;p&gt;Testing is weak in &lt;span class=&quot;caps&quot;&gt;AS3&lt;/span&gt;.  &lt;a href=&quot;http://code.google.com/p/as3flexunitlib/&quot;&gt;FlexUnit&lt;/a&gt; doesn&#8217;t provide dynamic test suites (see my post on &lt;a href=&quot;http://www.pgrs.net/2008/4/29/dynamically-generating-flexunit-test-suite&quot;&gt;Dynamically generating FlexUnit test suite&lt;/a&gt;).  Flex cannot create dynamic classes, so we cannot create dynamic mocks.  We would like to mock out flash classes (such as DisplayObject), but instead, we are stuck with stubs and other work arounds.  There is no way to run a single test, so we have to compile the whole thing and run the entire suite in the browser.&lt;/p&gt;


	&lt;h3&gt;Language&lt;/h3&gt;


	&lt;p&gt;ActionScript 3 feels like a weird hybrid between java and javascript.  It allows you to specify the types in variable declarations and methods.  If you don&#8217;t specify a type, you get a warning, so the code winds up being completely statically typed.  They removed much of the flexibility of ActionScript 2 (for example, no more eval), which is part of the reason that mocking is hard.  It feels like I&#8217;m back in java but without the tool support (for example, &lt;a href=&quot;http://cglib.sourceforge.net/&quot;&gt;cglib&lt;/a&gt;).&lt;/p&gt;


	&lt;h3&gt;Tools&lt;/h3&gt;


	&lt;p&gt;The main &lt;span class=&quot;caps&quot;&gt;IDE&lt;/span&gt; is &lt;a href=&quot;http://www.adobe.com/products/flex/features/flex_builder/&quot;&gt;Adobe Flex Builder&lt;/a&gt;, which is based on eclipse.  Flex Builder provides some nice features like syntax highlighting, autocomplete, and debugging.  However, its compiler is slow and we constantly have to wait to run our code.  Refactoring is limited to rename.  There is no code formatter built in.&lt;/p&gt;


	&lt;h3&gt;Builds&lt;/h3&gt;


	&lt;p&gt;I prefer rake over ant, but FlexUnit provides a bunch of ant tasks instead of command line tools.  We will probably move towards a rake/ant hybrid at some point.  We were also seeing weird issues where test failures would fail the build on macs, but not on linux.  The tools all seem immature.&lt;/p&gt;


	&lt;h3&gt;Conclusion&lt;/h3&gt;


	&lt;p&gt;Flex is new and sexy, and it is fun to see the output of what we make.  However, whenever I switch from ruby/textmate to flex/flex builder, I groan a little.  If you want to develop Flex without using an &lt;span class=&quot;caps&quot;&gt;IDE&lt;/span&gt;, at least make sure you install the debug version of flash in your browser.  That way, when things blow up, you will see stack traces.  The regular version of flash will swallow all errors without any indication of what went wrong.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-05-08:1694</id>
    <published>2008-05-08T01:53:00Z</published>
    <updated>2008-05-08T01:53:57Z</updated>
    <link href="http://www.pgrs.net/2008/5/8/factory-pattern-with-syntactic-sugar" rel="alternate" type="text/html"/>
    <title>Factory pattern with syntactic sugar</title>
<content type="html">
            &lt;p&gt;&lt;a href=&quot;http://www.dcmanges.com&quot;&gt;Dan Manges&lt;/a&gt; has a nice write-up on why the factory pattern is better than Rails fixtures: &lt;a href=&quot;http://www.dcmanges.com/blog/38&quot;&gt;Rails: Fixin&#8217; Fixtures with Factory&lt;/a&gt;.  The factory is used to create valid objects for testing with default values for all of the fields.  These objects can be used in tests without cluttering the test with attributes we do not care about.&lt;/p&gt;


	&lt;p&gt;On my current project, we like the factory pattern but favor a slightly better syntax.  We wanted to replace:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
Factory.create_paperboy
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;with&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
Paperboy.build!
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;We use the build method (for lack of a better name) to create test data.  We are calling a class method on Paperboy in order to create an instance, which seems more consistent with object creation in ruby than calling a method on a Factory class:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
Paperboy.new
Paperboy.create
Paperboy.build!
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;In addition to build! (which is like create!), we added a build method which creates the object without saving it.  We put our code in a file called factory.rb (which we require in spec_helper.rb) that looks like:&lt;/p&gt;


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

  def self.included(base)
    base.extend(self)
  end

  def build(params = {})
    raise &quot;There are no default params for #{self.name}&quot; unless self.respond_to?(self.name.underscore)
    new(self.send(self.name.underscore).merge(params))
  end

  def build!(params = {})
    obj = build(params)
    obj.save!
    obj
  end

  def customer
    {
      :first_name =&amp;gt; &quot;Joe&quot;,
      :last_name  =&amp;gt; &quot;Guy&quot;,
      :paperboy   =&amp;gt; Paperboy.build,
    }
  end

  def newspaper
    {
      :customer =&amp;gt; Customer.build,
      :headline =&amp;gt; &quot;Read all about it!&quot;,
      :paperboy =&amp;gt; Paperboy.build,
    }
  end

  def paperboy
    {
      :first_name     =&amp;gt; &quot;Paper&quot;,
      :last_name      =&amp;gt; &quot;Boy&quot;,
      :delivery_route =&amp;gt; &quot;Main St Route&quot; 
    }
  end  
end

ActiveRecord::Base.class_eval do
  include Factory
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The build method uses the class name to find the default params, which are defined as a method per class.  Then, it merges any user supplied params and creates the object.  Now, when we see test code that looks like:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
Paperboy.new :first_name =&amp;gt; &quot;some&quot;, :last_name =&amp;gt; &quot;person&quot; 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;we can replace it with:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
Paperboy.build
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;or, if one of the fields is important for the test:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
Paperboy.build :first_name =&amp;gt; &quot;joe&quot; 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;It is easy to swap the word &#8220;new&#8221; or &#8220;create&#8221; for &#8220;build&#8221; or &#8220;build!&#8221; and then delete the params that we do not care about.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-04-29:1550</id>
    <published>2008-04-29T03:55:00Z</published>
    <updated>2008-04-29T03:57:18Z</updated>
    <link href="http://www.pgrs.net/2008/4/29/dynamically-generating-flexunit-test-suite" rel="alternate" type="text/html"/>
    <title>Dynamically generating FlexUnit test suite</title>
<content type="html">
            &lt;p&gt;I have been working with &lt;a href=&quot;http://www.adobe.com/products/flex&quot;&gt;Flex&lt;/a&gt; recently, which is a framework for building Flash applications.  The standard testing framework for Flex is &lt;a href=&quot;http://code.google.com/p/as3flexunitlib&quot;&gt;FlexUnit&lt;/a&gt;.  A good example of FlexUnit can be found at &lt;a href=&quot;http://www.darronschall.com/weblog/archives/000216.cfm&quot;&gt;How to use FlexUnit with FlexBuilder 2&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;One limitation of FlexUnit is that it does not have a way to dynamically build a test suite at runtime. All of the examples online have a manually created and maintained test suite that looks like:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
package {
  import flexunit.framework.TestSuite;
  import com.foo.FooTest;
  import com.bar.BarTest;

  public class Suite {

    public static function createTestSuite() : TestSuite {

      var ts : TestSuite = new TestSuite();
      ts.addTestSuite(FooTest);
      ts.addTestSuite(BarTest);
      // Many more additions
      return ts;
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;We believe in constantly writing and refactoring tests, and we did not want to manually maintain this test suite.  We first investigated reading the filesystem to get the list of available tests at runtime.  However, since the tests run inside of flash (in a browser or standalone player), there is no access to the filesystem.  &lt;a href=&quot;http://www.workingwithrails.com/person/6754-kent-spillner&quot;&gt;Kent Spillner&lt;/a&gt; and I brainstormed and came up with a build time solution instead.&lt;/p&gt;


	&lt;p&gt;First, we wrote a ruby script that would read the filesystem and generate the test suite file.  We called the script test_suite_generator.rb, and it looks something like:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
#!/usr/bin/env ruby

require 'find'

search_path = File.dirname(__FILE__) + '/test/'

test_cases = []
Find.find search_path do |path|
  filename = File.basename(path)
  Find.prune if filename =~ /^\./
  test_cases &amp;lt;&amp;lt; path.gsub(&quot;#{search_path}&quot;, '').gsub('.as', '').gsub('/', '.') if filename =~ /Test\.as$/
end

test_cases.sort!

File.open('test/Suite.as', 'w') do |file|
  file.puts &amp;lt;&amp;lt;EOF
// This file is generated by #{File.basename(__FILE__)}.
package {
  import flexunit.framework.TestSuite;
EOF
  test_cases.each {|tc| file.puts &quot;  import #{tc};&quot;}
  file.puts &amp;lt;&amp;lt;EOF

  public class Suite {

    public static function createTestSuite() : TestSuite {

      var ts : TestSuite = new TestSuite();
EOF
  test_cases.each {|tc| file.puts &quot;      ts.addTestSuite(#{tc});&quot;}
  file.puts &amp;lt;&amp;lt;EOF
      return ts;
    }
  }
}
EOF
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This script uses Find to get all of the test files in the test folder (files that end in Test.as).  For each file, it turns the path into a package (com/foo/HelloWorldTest.as -&amp;gt; com.foo.HelloWorldTest).  Then, it writes a file called Suite.as into the test directory which imports each of these packages and then adds them to a TestSuite object.&lt;/p&gt;


	&lt;p&gt;We use &lt;a href=&quot;http://www.adobe.com/products/flex/features/flex_builder&quot;&gt;Flex Builder&lt;/a&gt; for flex development, which is based on eclipse.  This allows us to add a custom builder which will run our ruby script.&lt;/p&gt;


	&lt;p&gt;In Project -&amp;gt; Properties -&amp;gt; Builders, choose New -&amp;gt; Program.  Type TestSuiteGenerator for the name, and browse for the ruby script in the Location field:&lt;/p&gt;


	&lt;p&gt;&lt;a href=&quot;/assets/2008/4/29/launch_configuration_main.png&quot;&gt;&lt;img src=&quot;/assets/2008/4/29/launch_configuration_main.png&quot; height=&quot;479px&quot; width=&quot;601px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Then, on the &#8220;Build Options&#8221; tab, select &#8220;During auto builds&#8221; and &#8220;Specify working set of relevant resources:&#8221;&lt;/p&gt;


	&lt;p&gt;&lt;a href=&quot;/assets/2008/4/29/launch_configuration_options.png&quot;&gt;&lt;img src=&quot;/assets/2008/4/29/launch_configuration_options.png&quot; height=&quot;479px&quot; width=&quot;601px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Finally, click on &#8220;Specify Resources&#8230;&#8221; and choose the folder where your tests live.  Make sure that you don&#8217;t select the folder that includes Suite.as, or the generation of the file will kick off the builder again.  If your tests are nested inside a &#8220;com&#8221; folder, then choose &#8220;com.&#8221;&lt;/p&gt;


	&lt;p&gt;Now, whenever we add or rename a test, Flex Builder runs test_suite_generator.rb and regenerates the Suite.as file.  We don&#8217;t have to manually maintain anything.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-03-13:1103</id>
    <published>2008-03-13T00:48:00Z</published>
    <updated>2008-03-13T00:49:04Z</updated>
    <link href="http://www.pgrs.net/2008/3/13/flying-towards-a-hub" rel="alternate" type="text/html"/>
    <title>Flying towards a hub</title>
<content type="html">
            &lt;p&gt;Most people who work for &lt;a href=&quot;http://www.thoughtworks.com&quot;&gt;ThoughtWorks&lt;/a&gt; in the US travel for a living, so we spend a fair amount of time talking about travel.  Lately, we have noticed a pattern on cancellations and formed a theory: Flights towards an airline hub are less likely to be canceled.&lt;/p&gt;


	&lt;p&gt;Most of our team currently flies United between Chicago and Newark.  One member flies the same route on Continental.  Chicago is a United hub, while Newark is a Continental hub.  When there is bad weather on Thursdays, Continental will often cancel all of its flights to Chicago.  United, however, generally makes it out.  Likewise, on Sundays, United is more likely to cancel all of its flights to Newark, while Continental does not.&lt;/p&gt;


	&lt;p&gt;Our reasoning is that airlines are afraid that their planes will be stranded in non-hub cities.  Continental does not want to send its planes to Chicago if it fears that they will not make it back (many of the flights bounce back and forth between these two cities).  If the planes are already out, the hubs want them back, so flights towards the hub will probably make it out.&lt;/p&gt;


	&lt;p&gt;Taking this theory into account, it is better to fly an airline that has a hub in your home city.  Flights out are more likely to be canceled, but at least you spend the night at home.  Returning flights are less likely to be canceled, so you have a better chance of getting home.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-03-13:1102</id>
    <published>2008-03-13T00:39:00Z</published>
    <updated>2008-03-13T00:39:34Z</updated>
    <link href="http://www.pgrs.net/2008/3/13/alphabetize-schema-rb-columns" rel="alternate" type="text/html"/>
    <title>Alphabetize schema.rb columns</title>
<content type="html">
            &lt;p&gt;I wrote previously about &lt;a href=&quot;http://www.pgrs.net/2007/9/21/automated-testing-of-database-rollback-scripts-in-rails&quot;&gt;automated testing of database rollback scripts in rails&lt;/a&gt;.  After running the rollback scripts, we verify our database schema by comparing the schema.rb file from before the upgrade with the one after rollback.  The problem is that the columns for each table in schema.rb seem to appear in the order that they were created, not in alphabetical order.  So, if a column is dropped and then re-added, it will move down to the bottom of the list.  For example, we start with a table like:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
  create_table &quot;foo&quot;, :force =&amp;gt; true do |t|
    t.string &quot;first&quot; 
    t.string &quot;second&quot; 
  end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now, version 6 drops the &#8220;a&#8221; column, and the rollback adds it back.  Then, the schema.rb file will look like:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
  create_table &quot;foo&quot;, :force =&amp;gt; true do |t|
    t.string &quot;second&quot; 
    t.string &quot;first&quot; 
  end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;In order to compare schema.rb files before and after upgrades, we wanted to alphabetize the column list.  We did this by monkey patching the columns method on ActiveRecord::Base.connection.  We only want to change the columns method when dumping the schema.  We do not want to change the running application in any way. so we only run our monkey patch when calling the db:schema:dump task.  Our solution looks like:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
task :'db:schema:dump' =&amp;gt; :'db:alphabetize_columns'

task :'db:alphabetize_columns' do
  class &amp;lt;&amp;lt; ActiveRecord::Base.connection
    alias_method :old_columns, :columns unless self.instance_methods.include?(&quot;old_columns&quot;)

    def columns(*args)
      old_columns(*args).sort_by(&#38;:name)
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-02-26:914</id>
    <published>2008-02-26T02:58:00Z</published>
    <updated>2008-02-26T02:59:38Z</updated>
    <link href="http://www.pgrs.net/2008/2/26/reducing-build-time" rel="alternate" type="text/html"/>
    <title>Reducing build time</title>
<content type="html">
            &lt;p&gt;A short build time is a critical element of &lt;a href=&quot;http://www.martinfowler.com/articles/continuousIntegration.html&quot;&gt;continuous integration&lt;/a&gt;.  I&#8217;ve been involved in a number of build improvements on my current project (both local and on the build server), and I thought I would share some of them.  Using the tricks below, we cut our build time in half (and have more to go).  Obviously, every build and project are different, so many of these may not apply to other situations.&lt;/p&gt;


	&lt;h3&gt;Profile the build&lt;/h3&gt;


	&lt;p&gt;We profiled our build before we made any changes.  We found the slowest tasks using rake&#8212;trace, which prints out task timing.  Furthermore, we watched the output from the &#8220;top&#8221; command while the builds were running on the build server to reveal bottlenecks.&lt;/p&gt;


	&lt;h3&gt;Turn down logging&lt;/h3&gt;


	&lt;p&gt;We noticed while watching &#8220;top&#8221; that kjournald was constantly running.  This indicates a lot of disk activity.  Our app logs to files and syslog while running in production, but this is unnecessary during builds.  We turned off nearly all logging in test mode.&lt;/p&gt;


	&lt;h3&gt;Make sure the build machine is only performing builds&lt;/h3&gt;


	&lt;p&gt;We discovered that our build machine had become a bit of a playground for trying new things.  This is fine in moderation, but some of these projects were not cleaned up.  There were a lot of unnecessary processes running on the build machine.  We killed everything that was unnecessary, and increased the &lt;a href=&quot;http://en.wikipedia.org/wiki/Nice_(Unix)&quot;&gt;nice&lt;/a&gt; value of processes that were not critical.  For example, &lt;a href=&quot;http://cruisecontrolrb.thoughtworks.com/&quot;&gt;cruisecontrol.rb&lt;/a&gt; runs the web interface as a separate process from the builders.  We increased the nice value of the web interface so building our projects would take precedence.&lt;/p&gt;


	&lt;h3&gt;Parallelizaton&lt;/h3&gt;


	&lt;p&gt;If you have more than one processor (or core), and the machine is running a giant set of tests, there is a good chance that the other processor is doing very little.  We discovered huge gains by running tests in parallel.  Projects like &lt;a href=&quot;http://www.somethingnimble.com/bliki/deep-test&quot;&gt;deeptest&lt;/a&gt; and &lt;a href=&quot;http://selenium-grid.openqa.org/&quot;&gt;selenium grid&lt;/a&gt; make this easy.  We went a simpler route and run our functional and acceptance tests at the same time in different processes.&lt;/p&gt;


	&lt;h3&gt;Build database from dumps&lt;/h3&gt;


	&lt;p&gt;Our project is on release six, so we have six versions of the database to build.  We ran ddl and dml for every release to build a version six database.  Each time we release, we add a new version and the build gets a little longer.  We started saving build time by dumping the database schema and data from the previous release.  Now, when we want to build a version six database, we restore a version five dump and then build up from there.&lt;/p&gt;


	&lt;h3&gt;Trim selenium suite&lt;/h3&gt;


	&lt;p&gt;&lt;a href=&quot;http://selenium.openqa.org/&quot;&gt;Selenium&lt;/a&gt; is a great testing tool, but it runs slowly.  Opening new browsers and clicking through the site is slow.  We looked at our acceptance test suite more carefully and trimmed it.  Some of the excess was duplication that was covered in another test (or easily added to another test).  Other logic was better tested at the functional level and did not need an acceptance test.  We reduced our suite to a few long passes through the application, rather than many smaller tests.&lt;/p&gt;


	&lt;h3&gt;Better hardware&lt;/h3&gt;


	&lt;p&gt;Hardware is relatively cheap compared with developer time.  It is worth investing in great hardware.  That said, it does not reduce the need for the above improvements.  Unfortunately, in many organizations, getting new hardware can be slow.  Rather than wait a month, we can work on the build and see results today.  And once the new hardware arrives, it will make things even faster.&lt;/p&gt;


	&lt;p&gt;If you have other build improvement strategies, please let me know in the comments.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-02-21:860</id>
    <published>2008-02-21T03:44:00Z</published>
    <updated>2008-02-21T03:44:32Z</updated>
    <link href="http://www.pgrs.net/2008/2/21/activerecord-serialize-only-saves-data" rel="alternate" type="text/html"/>
    <title>ActiveRecord serialize only saves data</title>
<content type="html">
            &lt;p&gt;We ran into an interesting gotcha on our project the other day.  We use serialize on ActiveRecord to save ruby objects to the database.  This is described in &lt;a href=&quot;http://blog.jayfields.com/2007/03/rails-activerecord-serialize-method.html&quot;&gt;Jay Fields Thoughts: Rails: ActiveRecord Serialize method&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Serialize uses &lt;a href=&quot;http://www.ruby-doc.org/core/classes/YAML.html#M007407&quot;&gt;&lt;span class=&quot;caps&quot;&gt;YAML&lt;/span&gt;.dump&lt;/a&gt; and &lt;a href=&quot;http://www.ruby-doc.org/core/classes/YAML.html#M007408&quot;&gt;&lt;span class=&quot;caps&quot;&gt;YAML&lt;/span&gt;.load&lt;/a&gt; to serialize/deserialize objects to strings.  These methods only deal with the data of an object, not the methods.  The objects we serialized used metaprogramming to dynamically define methods.  When they were loaded from the database, they no longer had the new methods.&lt;/p&gt;


	&lt;p&gt;Here is a contrived example.  The Foo class creates a foo method in the initialize:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
class Foo
  def initialize
    class &amp;lt;&amp;lt; self
      define_method :foo, lambda { 10 }
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
&amp;gt;&amp;gt; Foo.new.foo
=&amp;gt; 10
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;A dump of the Foo class has no knowledge of this foo method:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
&amp;gt;&amp;gt; require 'yaml'
&amp;gt;&amp;gt; YAML.dump(Foo.new)
=&amp;gt; &quot;--- !ruby/object:Foo {}\n\n&quot; 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Therefore, the loaded version of Foo will not have the foo method:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
&amp;gt;&amp;gt; YAML.load(YAML.dump(Foo.new)).foo
NoMethodError: undefined method `foo' for #&amp;lt;Foo:0xb7985af0&amp;gt;
        from (irb):16
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;In our case, we changed our code to store only the data from the domain object in the database (in columns).  We recreate the domain object from these columns when we need it.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-02-17:825</id>
    <published>2008-02-17T18:14:00Z</published>
    <updated>2008-02-19T01:15:58Z</updated>
    <link href="http://www.pgrs.net/2008/2/17/handling-nil-in-method-calls" rel="alternate" type="text/html"/>
    <title>Handling nil in method calls</title>
<content type="html">
            &lt;p&gt;On my current project, we noticed common pattern when dealing with nil.  We would often check an object to see if it was nil before calling a method on that object:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
name = person ? person.name : nil
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;To reduce duplication, &lt;a href=&quot;http://www.klankboomklang.com/&quot;&gt;Patrick Farley&lt;/a&gt; and &lt;a href=&quot;http://www.aliaghareza.com&quot;&gt;Ali Aghareza&lt;/a&gt; created a nil_or method which handles this.  The above code becomes:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
name = person.nil_or.name
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The nil_or causes the expression to return nil if the target is nil.  If not, the name method is called.&lt;/p&gt;


	&lt;p&gt;The code for nil_or looks like:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
module ObjectExtension
  def nil_or
    return self unless self.nil?
    Class.new do
      def method_missing(sym, *args); nil; end
    end.new
  end  
end

class Object
  include ObjectExtension
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The nil_or method returns self if self is not nil.  If self is nil, it creates a new Object which eats all method calls and returns nil.&lt;/p&gt;


	&lt;p&gt;We use a fair amount of delegation on this project using forwardable, so &lt;a href=&quot;http://schubert.cx/&quot;&gt;Michael Schubert&lt;/a&gt; and &lt;a href=&quot;http://www.workingwithrails.com/person/11263-toby-tripp&quot;&gt;Toby Tripp&lt;/a&gt; created a delegator which has the same effect.  For example, you can replace this delegation:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
class Person
  extend Forwardable
  def_delegator :@job, :title, :job_title
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;with this one:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
class Person
  extend Forwardable
  def_delegator_or_nil :@job, :title, :job_title
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This delegation is equivalent to this code:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
class Person
  def job_title
    @job ? @job.title : nil
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The code for def_delegator_or_nil looks like:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
module ForwardableExtension
  def def_delegator_or_nil(accessor, method, new_method = method)
    accessor = accessor.id2name if accessor.kind_of?(Integer)
    method = method.id2name if method.kind_of?(Integer)
    new_method = new_method.id2name if new_method.kind_of?(Integer)

    module_eval(&amp;lt;&amp;lt;-EOS, &quot;(__FORWARDABLE_EXTENSION__)&quot;, 1)
      def #{new_method}(*args, &#38;block)
        begin
          if #{accessor}.nil?
            nil
          else
            #{accessor}.__send__(:#{method}, *args,&#38;block)
          end
        rescue Exception
          $@.delete_if{|s| /^\\(__FORWARDABLE_EXTENSION__\\):/ =~ s} unless Forwardable::debug
          Kernel::raise
        end
      end
    EOS
  end
end

module Forwardable
  include ForwardableExtension
end
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-02-08:725</id>
    <published>2008-02-08T22:46:00Z</published>
    <updated>2008-02-08T22:49:10Z</updated>
    <link href="http://www.pgrs.net/2008/2/8/loading-rails-sessions-manually" rel="alternate" type="text/html"/>
    <title>Loading rails sessions manually</title>
<content type="html">
            &lt;p&gt;On my current project, we wanted to write some code to load a specific user&#8217;s session data (not the current user).  This turned out to be a little trickier than we thought.&lt;/p&gt;


	&lt;p&gt;We use active_record_store for our sessions, so session data is stored in a sessions table in the database.  In theory, we should be able to read the session with code like:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
&amp;gt;&amp;gt; CGI::Session::ActiveRecordStore::Session.find_by_id(1).data
ArgumentError: undefined class/module Foo
        from /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session/active_record_store.rb:84:in `load'
        from /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session/active_record_store.rb:84:in `unmarshal'
        from /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session/active_record_store.rb:122:in `data'
        from (irb):1
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Unfortunately, if the session contains any custom classes, this code will fail.  Behind the scenes, session data is stored as a Base64 encoded, Marshal dumped string.  If there are classes in the dump that ruby does not know about yet, the Marshal.load will fail.&lt;/p&gt;


	&lt;p&gt;If we manually load the class, it will work:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
&amp;gt;&amp;gt; Foo
=&amp;gt; Foo
&amp;gt;&amp;gt; CGI::Session::ActiveRecordStore::Session.find_by_id(1).data
=&amp;gt; {:foo=&amp;gt;#&amp;lt;Foo:0xb7a5f3cc&amp;gt;, &quot;flash&quot;=&amp;gt;{}}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Our sessions contain a bunch of custom classes, and we did not want to manually load them.  Since we knew rails handled this properly, we dug into the depths of rails and found this code in &lt;a href=&quot;http://dev.rubyonrails.org/svn/rails/tags/rel_2-0-2/actionpack/lib/action_controller/cgi_process.rb&quot;&gt;cgi_proceess.rb&lt;/a&gt; (Rails 2.0.2):&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
def stale_session_check!
  yield
rescue ArgumentError =&amp;gt; argument_error
  if argument_error.message =~ %r{undefined class/module ([\w:]*\w)}
    begin
      # Note that the regexp does not allow $1 to end with a ':'
      $1.constantize
    rescue LoadError, NameError =&amp;gt; const_error
      raise ActionController::SessionRestoreError, &amp;lt;&amp;lt;-end_msg
Session contains objects whose class definition isn\'t available.
Remember to require the classes for all objects kept in the session.
(Original exception: #{const_error.message} [#{const_error.class}])
end_msg
    end

    retry
  else
    raise
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This code assumes you pass in a block which loads the session.  If an error is raised trying to load a class, it calls constantize on the class name, which forces rails to find and load the class.  It does this repeatedly until all load errors have been resolved.&lt;/p&gt;


	&lt;p&gt;We can use this method now to load our session:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
&amp;gt;&amp;gt; ActionController::CgiRequest.new(CGI.new).instance_eval do
?&amp;gt;   stale_session_check! do
?&amp;gt;     CGI::Session::ActiveRecordStore::Session.find_by_id(1).data
&amp;gt;&amp;gt;   end
&amp;gt;&amp;gt; end
=&amp;gt; {:foo=&amp;gt;#&amp;lt;Foo:0xb79f46a8&amp;gt;, &quot;flash&quot;=&amp;gt;{}}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This is not the prettiest code, but it allows us to get at the logic rails uses to load sessions.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-01-30:595</id>
    <published>2008-01-30T01:45:00Z</published>
    <updated>2008-02-01T15:43:56Z</updated>
    <link href="http://www.pgrs.net/2008/1/30/popup-when-leaving-website" rel="alternate" type="text/html"/>
    <title>Popup when leaving website</title>
<content type="html">
            &lt;p&gt;On my current project, we had a requirement to pop up a message to the user when they leave our site (e.g., close the browser window).  We discovered a number of sites talking about a window.close event, but it is not supported in modern browsers (IE 6, Firefox 2, Safari 2).  The closest event is an unload event which gets fired when the page is unloaded.&lt;/p&gt;


	&lt;p&gt;We tried using this event in a javascript file that is included on every page:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
window.onunload = popup;

function popup() {
  alert('I see you are leaving the site');
}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Unfortunately, an unload event is fired when the user leaves the page for any reason (such as clicking on a link).  We did not want to show the popup if the user clicked on a link which stayed on the site.&lt;/p&gt;


	&lt;p&gt;Our next idea was to add an onclick to every link which would turn off the popup.  Then, if the user clicks a link, nothing happens.  If they leave the site another way, they get the popup.&lt;/p&gt;


	&lt;p&gt;&lt;a href=&quot;http://www.workingwithrails.com/person/11263-toby-tripp&quot;&gt;Toby Tripp&lt;/a&gt; had the great idea to add these onclicks dynamically in javascript.  We use &lt;a href=&quot;http://www.prototypejs.org/&quot;&gt;prototype&lt;/a&gt;, so the code looks like:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
staying_in_site = false;

$$('a').each(function(link) {
  link.observe('click', function() {staying_in_site = true;});
});

window.onunload = popup;

function popup() {
  if(staying_in_site) {
    return;
  }
  alert('I see you are leaving the site');
}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This code creates a variable which determines whether or not to show the popup.  Then, we find every link with $$(&#8216;a&#8217;).  This is a &lt;a href=&quot;http://www.w3.org/TR/REC-CSS2/selector.html&quot;&gt;&lt;span class=&quot;caps&quot;&gt;CSS&lt;/span&gt; selector&lt;/a&gt; which selects all of the a elements.  We iterate over these links and add a listener which sets the staying_in_site flag to true.  Now, our popup code checks this flag and knows whether the user is staying on the site.&lt;/p&gt;


	&lt;p&gt;It is important to include this javascript at the bottom of the file (and not in the head).  The javascript must be executed after the page is written or the links will not exist yet.&lt;/p&gt;


	&lt;p&gt;This solution is not perfect, but it accomplishes most of what we need.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Update (1/31/08):&lt;/strong&gt; &lt;a href=&quot;http://www.pubbitch.org/blog&quot;&gt;Simon Stewart&lt;/a&gt; suggested a cleaner method in the comments:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
staying_in_site = false;

Event.observe(document.body, 'click', function(event) {
  if (Event.element(event).tagName == 'A') {
    staying_in_site = true;
  }
});

window.onunload = popup;

function popup() {
  if(staying_in_site) {
    return;
  }
  alert('I see you are leaving the site');
}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Instead of attaching an event listener to every link, we can attach a click listener to the whole body.  Now, any click will call our function, which checks to see if you clicked on a link.  Since we are not iterating over existing links, this javascript does not have to be below the links in the page.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-01-23:536</id>
    <published>2008-01-23T03:11:00Z</published>
    <updated>2008-01-23T03:16:14Z</updated>
    <link href="http://www.pgrs.net/2008/1/23/remove-files-that-are-not-in-subversion" rel="alternate" type="text/html"/>
    <title>Remove files that are not in subversion</title>
<content type="html">
            &lt;p&gt;One of the great features of version control is that I can easily revert back to a known good state.  I can do this in &lt;a href=&quot;http://subversion.tigris.org&quot;&gt;Subversion&lt;/a&gt; with the following command:&lt;/p&gt;


&lt;pre&gt;
% svn revert -R .
&lt;/pre&gt;

	&lt;p&gt;However, if I have new files that are not in Subversion, this command will not delete them.  Here is a fun ruby one liner to remove those files:&lt;/p&gt;


&lt;pre&gt;
svn st | ruby -ne 'File.delete($_[1..-1].strip) if $_.match(/^\?/)'
&lt;/pre&gt;

	&lt;p&gt;This command loops over svn status and deletes all files from lines that start with ?.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.pgrs.net/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:www.pgrs.net,2008-01-11:454</id>
    <published>2008-01-11T17:31:00Z</published>
    <updated>2008-01-17T02:41:49Z</updated>
    <link href="http://www.pgrs.net/2008/1/11/postgresql-allows-duplicate-nulls-in-unique-columns" rel="alternate" type="text/html"/>
    <title>PostgreSQL allows duplicate nulls in unique columns</title>
<content type="html">
            &lt;p&gt;It seems strange, but duplicate null values do not violate unique constraints in &lt;a href=&quot;http://www.postgresql.org&quot;&gt;PostgreSQL&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Inserting the same non-null value twice in a unique column fails as expected:&lt;/p&gt;


&lt;pre&gt;
# create table test (
  a varchar unique
);

# insert into test values(1);
INSERT 0 1

# insert into test values(1);
ERROR:  duplicate key violates unique constraint &quot;test_a_key&quot; 
&lt;/pre&gt;

	&lt;p&gt;However, the same is not true for null:&lt;/p&gt;


&lt;pre&gt;
test=# insert into test values(null);
INSERT 0 1

test=# insert into test values(null);
INSERT 0 1

# select * from test;
 a
---
 1

(3 rows)
&lt;/pre&gt;

	&lt;p&gt;I think this is misleading, but PostgreSQL says that it is following the &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; standard: &lt;a href=&quot;http://www.postgresql.org/docs/8.2/interactive/ddl-constraints.html#AEN2058&quot;&gt;Unique Constraints&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Update (1/16/08):&lt;/strong&gt; &lt;a href=&quot;http://www.sadalage.com/&quot;&gt;Pramod Sadalage&lt;/a&gt; showed me that Oracle actually behaves just like PostgreSQL.    I&#8217;m not sure why I was seeing different behavior, but I could not reproduce the problem.&lt;/p&gt;
          </content>  </entry>
</feed>
