<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Fozworks]]></title>
  <link href="http://www.fozworks.com/atom.xml" rel="self"/>
  <link href="http://www.fozworks.com/"/>
  <updated>2012-01-11T09:24:40+01:00</updated>
  <id>http://www.fozworks.com/</id>
  <author>
    <name><![CDATA[Jeremy Seitz]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Announcing: MF60 Ruby Gem]]></title>
    <link href="http://www.fozworks.com/blog/2012/01/10/announcing-mf60-ruby-gem/"/>
    <updated>2012-01-10T08:47:00+01:00</updated>
    <id>http://www.fozworks.com/blog/2012/01/10/announcing-mf60-ruby-gem</id>
    <content type="html"><![CDATA[<p><img src="http://dl.dropbox.com/u/385855/Screenshots/p8ui.png" alt="mf60 on the train" /></p>

<p>For the past several months I have been commuting by train to Zurich and enjoy hacking on my laptop. For a while, I was using my iPhone to tether, but it was slow and unreliable, especially in the longer stretches between towns. So I got myself an <a href="http://www.modem3g.com/zte-mf60-mobile-hotspot.html">MF60 Hotspot</a> from Swisscom. It acts as a WIFI router, does several 3G/2G bands and auto-switches pretty quickly. The battery life is not great, I only get about 3-4 hours tops, but connected to the laptop via USB, it will keep going for a long time.</p>

<p>Managing it, however, is annoying. Sadly, the built-in web administrative interface is not a shining example of usability. It&#8217;s functional but klunky - you can do all that you would expect with an access point: manage the settings and view usage stats. That&#8217;s important since my account with Swisscom has a limit on monthly data, I needed to keep track of my usage. But it is a pain to use with the tiny links and non-sensical organization of the menu pages.</p>

<p>Another problem is that the network gets stuck sometimes (like after a long tunnel). This requires a reset, which means powering off/on (a 20-30 sec ordeal), or going through the previously-deemed crap admin interface.</p>

<p>I too on the task of peeking through the HTML and Javascript with Firebug, and wrote a set of scripts to help me do basic things fro the command-line: view stats, reconnect, check signal strength, etc. Last week, I refactored it and <a href="https://rubygems.org/gems/mf60">released it as a Gem</a>.</p>

<pre><code>$ sudo gem install mf60
</code></pre>

<p>More details are at the <a href="https://github.com/somebox/MF60">project Github page</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Quest for a Nicer Prompt]]></title>
    <link href="http://www.fozworks.com/blog/2011/12/15/quest-for-a-nicer-prompt/"/>
    <updated>2011-12-15T01:06:00+01:00</updated>
    <id>http://www.fozworks.com/blog/2011/12/15/quest-for-a-nicer-prompt</id>
    <content type="html"><![CDATA[<p>Fancy console prompts are fun, but its a tradeoff. The problem is, once you put all this extra info in there it gets too long.</p>

<p><img src="http://dl.dropbox.com/u/385855/Screenshots/x1bx.png" title="too many chaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" alt="Fuuuuuu" /></p>

<p>The prompt should not get in the way. It should show as few characters as possible but be highly helpful, informative, and pleasant.</p>

<h2>Home Sweet Home</h2>

<p><img src="http://dl.dropbox.com/u/385855/Screenshots/ivuv.png" title="The simplest prompt of all." alt="the simplest prompt" /></p>

<p>Logged in as myself, on my local machine, in my home directory. It just reads <strong>at home</strong> - three characters and a cursor.</p>

<p>Once I&#8217;ve logged into a remote box as a different user, and switched away from my home directory, the same PS1 shows a more conventional prompt:</p>

<p><img src="http://dl.dropbox.com/u/385855/Screenshots/qku7.png" title="the prompt parts" alt="anatomy of the prompt" /></p>

<p>The path is underlined not just because it looks nice. Command-clicking on pathnames opens a Finder window in <a href="http://www.iterm2.com/">iTerm2</a>, and I think that it looks more like a clickable link in this way.</p>

<h2>A Shorter Path</h2>

<p>Back to how I find it annoying when the path gets too long and commands start wrapping lines as I type. 40 chars is about the maximum to show for a path. There are two strategies to shorten paths:</p>

<h3>1. First-class Website Roots</h3>

<p>As a web developer I spend most of my time inside of a web project root folders. Locally, that&#8217;s <code>~/Sites/sitename.com</code> but on remote servers it&#8217;s usually <code>/var/www/vhosts/sitename.com</code>. For these cases, the path is reduced to a web-like scheme prefix (<code>//</code>):</p>

<p><img src="http://dl.dropbox.com/u/385855/Screenshots/m-kj.png" title="web project prompt shortening" alt="web projects" /></p>

<h3>2. Ellipsis in the Middle</h3>

<p>Long paths are truncated using two methods:</p>

<ul>
<li>Split the path on the slashes, show the first three parts and the last two, join with &#8220;…&#8221;</li>
<li>Use the first 25 characters of the path and the last 15, join with &#8220;…&#8221;</li>
<li>display whichever version ends up being shorter</li>
</ul>


<p>The result is that paths are split up more naturally. Really long directory names end up being chopped, wheres shorter paths make it through ok. Its a good balance, and you can still figure out where you are.</p>

<p><img src="http://dl.dropbox.com/u/385855/Screenshots/~z5l.png" title="longer paths truncated with a ellipsis …" alt="truncate long paths" /></p>

<p>A ruby one-liner does this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">d</span><span class="o">=</span><span class="no">ENV</span><span class="o">[</span><span class="s2">&quot;PWD&quot;</span><span class="o">].</span><span class="n">gsub</span><span class="p">(</span><span class="sr">%r{^</span><span class="si">#{</span><span class="no">ENV</span><span class="o">[</span><span class="s2">&quot;HOME&quot;</span><span class="o">]</span><span class="si">}</span><span class="sr">}</span><span class="p">,</span><span class="s2">&quot;~&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">%r{^~/Sites/}</span><span class="p">,</span><span class="s2">&quot;//&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">%r{^/var/www/vhosts/}</span><span class="p">,</span><span class="s2">&quot;//&quot;</span><span class="p">);</span><span class="n">p1</span><span class="o">=</span><span class="n">d</span><span class="o">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">25</span><span class="o">]+</span><span class="s2">&quot;…&quot;</span><span class="o">+</span><span class="n">d</span><span class="o">[-</span><span class="mi">15</span><span class="p">,</span><span class="mi">15</span><span class="o">].</span><span class="n">to_s</span><span class="p">;</span><span class="n">a</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="p">);</span><span class="n">p2</span><span class="o">=</span><span class="n">a</span><span class="o">.</span><span class="n">first</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">+[</span><span class="s2">&quot;…&quot;</span><span class="o">]+</span><span class="n">a</span><span class="o">.</span><span class="n">last</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span><span class="n">p2</span><span class="o">=</span><span class="n">p2</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="p">);</span><span class="nb">puts</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">size</span><span class="o">&gt;[</span><span class="n">p1</span><span class="o">.</span><span class="n">size</span><span class="p">,</span><span class="n">p2</span><span class="o">.</span><span class="n">size</span><span class="o">].</span><span class="n">min</span><span class="p">)</span><span class="sc">?(</span><span class="p">(</span><span class="n">p1</span><span class="o">.</span><span class="n">size</span><span class="o">&lt;</span> <span class="n">p2</span><span class="o">.</span><span class="n">size</span><span class="p">)?</span><span class="n">p1</span><span class="ss">:p2</span><span class="p">)</span><span class="ss">:d</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Git Branch and Status</h3>

<p>Finally, the git branch is displayed in brackets, for any directory under git control. If there are any uncommitted changes, a red asterisk is also helpfully displayed. It would be nice to have the branch name be truncated as well (they too can get longish), and maybe showing ahead/behind status. But that&#8217;s not there yet.</p>

<p><img src="http://dl.dropbox.com/u/385855/Screenshots/sxs6.png" title="current branch and status shown" alt="shows current git branch and status" /></p>

<h3>Other Stuff</h3>

<ul>
<li>sets the title bar for the terminal window (without the colors or git branch info)</li>
<li>The <code>root</code> user is always shown in red</li>
<li>hosts on local subnets (<code>192.*</code>, <code>10.*</code>) are shown in green</li>
<li>many other colors and effects are defined for tweaking</li>
</ul>


<h2>What&#8217;s your PS1?</h2>

<p>I&#8217;ve been slowly customizing my <code>PS1</code> bash prompt over the last few years to try and get something really pretty and usable working. So this is what I&#8217;m currently satisfied with, a fairly involved collection of bash functions, mixed with some inline ruby code. Looks chaotic, but it should pretty easy to customize.</p>

<p><strong>The script</strong>: <a href="https://gist.github.com/1494881">https://gist.github.com/1494881</a></p>

<p>To get it set up, save the source locally:</p>

<pre><code>curl -o ~/.ps1 https://raw.github.com/gist/1494881/b1a323956126a4346b803c09b691e1a1e013c7ff/nicer_ps1.sh
</code></pre>

<p>Then edit your <code>.bashrc</code> (or <code>.bash_profile</code>) add these lines:</p>

<pre><code>DEFAULT_USERNAME=jeremy    # change this to yours!
SERVERNAME=ninja           # only if you want to override the default hostname
source ~/.ps1
</code></pre>

<p>Now do this for all your shell accounts. Or hey, make a dotfiles repository and use that. Or use Puppet or something else!</p>

<hr id="notes"/>


<p><em>thanks to <a href="http://ivanjovanovic.com/">Ivan Jovanovic</a> for sharing the git branch and bash functions approach with me, and to the dotfiles of <a href="https://github.com/ryanb/dotfiles">Ryan Bates</a> and <a href="https://github.com/mathiasbynens/dotfiles">Mathias Bynens</a> for further inspirations</em></p>

<p>* I prefer <a href="https://fedorahosted.org/liberation-fonts/">Liberation Mono</a> font for programming.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Dropbox and Seemingly Incredible Upload Speeds]]></title>
    <link href="http://www.fozworks.com/blog/2009/12/19/dropbox-knows-quite-a-lot-actually/"/>
    <updated>2009-12-19T00:00:00+01:00</updated>
    <id>http://www.fozworks.com/blog/2009/12/19/dropbox-knows-quite-a-lot-actually</id>
    <content type="html"><![CDATA[<p><a href="http://dropbox.com">Dropbox</a> is a really cool service. I love being able to share files with friends and co-workers with such ease.</p>

<p>But recently I noticed something a bit surprising.</p>

<p>When I copied a large disk image to my Dropbox, within a few seconds, it had finished uploading: &#8220;All Files up to date.&#8221; Ok, I have a pretty good connection, but it seemed impossible that I could somehow upload hundreds of megabytes in just a few seconds. I then went to download that file from a different computer, and sure enough, the whole file came down, in tact.</p>

<p>How was Drobox able to upload so damn quickly? I tested this again several times using various large files (music, movies, whatever). What I found was that certain large files uploaded almost immediately, while others took hours. Was it compression? Maybe delayed uploads?</p>

<p>Here&#8217;s a screencast showing this in action (without sound):</p>

<object width="640" height="512"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8270464&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8270464&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="640" height="512"></embed></object>


<p><a href="http://vimeo.com/8270464">dropbox exposed</a> from <a href="http://vimeo.com/user1587018">Jeremy Seitz</a> on <a href="http://vimeo.com">Vimeo</a>.</p>


<p>From what I can tell, Dropbox calculates and remembers the unique fingerprints of files. So if user A uploads a big file, and later, user B uploads the SAME file, then Dropbox recognizes that. There&#8217;s no need to waste the bandwidth (and presumably, the disk space in Dropbox&#8217;s cloud) for that second upload.</p>

<p>This is brilliant, because I&#8217;m sure it saves them huge amounts of bandwidth and disk resources. However, in my testing, the files I uploaded were <b>were not shared with anyone, and they were not public</b>. Dropbox was able to &#8220;magically&#8221; upload huge files that I had never put in my account before. I can only assume that another user had uploaded the same files to their Dropbox before I had.</p>

<p>Maybe everyone is cool with it, but I think the privacy implications are pretty significant. Dropbox <a href="https://www.dropbox.com/features">claims that their employees can&#8217;t see the CONTENTS of your files</a>. But apparently, they know that different users have the SAME FILES.</p>

<p>With that in mind, here&#8217;s some hypothetical ways in which this could be exploited:</p>

<ul>
<li><p>Suppose a movie industry lawyer uploads a pirated film to Dropbox. If the upload finished instantly, they could potentially prove that at least <i>one</i> user had that file. Perhaps this is enough to convince a court to force Dropbox to release information?</p></li>
<li><p>Suppose a recording studio wanted to make sure that a hot new album, ready for release, had not been leaked to the Internet. So they put the files in their own Dropbox folder. If they uploaded right away, they would realize that there was a security problem at hand.</p></li>
<li><p>For a hacker, knowing when a file is NOT UNIQUE could be particularly interesting. Let&#8217;s suppose that company A has a file on Dropbox that contains encrypted data. Evil company B has some information about that file, but they don&#8217;t have the encryption key. Could they use this feature to crack it?</p></li>
</ul>


<p>Maybe this sounds overly paranoid.  Again, I like Dropbox, but I would not put sensitive or private data there. Clever and cool tech? Definitely. Scary? I think it is, a little.</p>

<p><b>UPDATE:</b> According to the <a href="">Wikipedia page for Dropbox</a>, they use Delta Technology: &#8220;Files are split into chunks, the chunks are hashed and only those chunks that have never been uploaded before by any user are transmitted again. This makes uploading popular files very efficient and helps if only small portions of a large file has changed.&#8221; That certainly explains what I observed.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Mocking GeoKit's Geocoder]]></title>
    <link href="http://www.fozworks.com/blog/2009/12/01/mocking-geokit-s-geocoder/"/>
    <updated>2009-12-01T00:00:00+01:00</updated>
    <id>http://www.fozworks.com/blog/2009/12/01/mocking-geokit-s-geocoder</id>
    <content type="html"><![CDATA[<p>I&#8217;ve been using <a href="http://github.com/andre/geokit-rails/">GeoKit</a> a lot in Rails, most often for geocoding. Models like Points, Companies and Users usually have an address, city, state, postal code and country - which might need to be geocoded so we can place them on a map, or check distances.</p>




<p>It&#8217;s definitely bad to let geocoder calls happen in tests. Not only does it slow things down, but if for some reason your network is not so good, tests will fail unexpectedly. Here are the helpers I like to use when writing the tests. This is using Test::Unit and <a href="http://github.com/floehopper/mocha">Mocha</a>:</p>




<script src="http://gist.github.com/246367.js?file=geocoder_mocks.rb"></script>




<p>In use, it&#8217;s pretty straightforward. You either expect the geocoder to succeed (a certain number of times), fail, or not be used at all. That last point is important because it&#8217;s easy to forget and have a model that geocodes on every save (via a before_save callback, for instance). That will slow down site response time because of the network delays.</p>




<p>Finally, here are some generic examples of each of the test helper methods in use:</p>




<script src="http://gist.github.com/246345.js?file=geocode_mocks_examples.rb"></script>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[A really simple fix for memory bloat]]></title>
    <link href="http://www.fozworks.com/blog/2009/11/25/a-really-simple-fix-for-memory-bloat-in-rails/"/>
    <updated>2009-11-25T00:00:00+01:00</updated>
    <id>http://www.fozworks.com/blog/2009/11/25/a-really-simple-fix-for-memory-bloat-in-rails</id>
    <content type="html"><![CDATA[<p>A common problem with Rails in a production environment is memory bloat. EngineYard recently <a href="http://www.engineyard.com/blog/2009/thats-not-a-memory-leak-its-bloat/">posted about this very issue</a> on thier blog (and I fear that several of my support tickets were maybe related!).</p></p>

<p>For example, suppose you have some code that loads tons of records, or uses an external library that needs a lot of RAM. After the task is finished, that memory can get garbage collected, but the Ruby VM doesn&#8217;t shrink. Anyone who&#8217;s needed to parse large XML files will know what I&#8217;m talking about.</p>




<p>This problem can be fixed by changing your code, so it doesn&#8217;t use too much memory at once. Or, you could run <a href="http://god.rubyforge.org/">God</a> or something similar to kill off greedy processes. But what if you just really DO need to use a lot of memory?</p>




<p>I recently wrote a rake task to resize a bunch of <a href="http://github.com/technoweenie/attachment_fu/">attachment_fu</a> images. The model looked like this:</p>




<script src="http://gist.github.com/242707.js?file=gistfile1.rb"></script>




<p>Then I had a rake task:</p>




<script src="http://gist.github.com/242711.js?file=gistfile1.rb"></script>




<p>As you might imagine, when run, this task consumed all of the RAM on our server in no time. And when the downsample! method was called in production, bam, Mongrel bloat. Big FAIL.</p>




<h3>The Solution</h3>




<p>Then, it dawned upon me. I had been using the wonderful <a href="http://github.com/tra/spawn">spawn plugin</a> on projects to handle high-latency tasks in controllers, and to parallelize batch processing. It basically can thread or fork out a block of ruby code, and you can optionally wait for the job to finish. So I wrapped the code like this:</p>




<script src="http://gist.github.com/242716.js?file=gistfile1.rb"></script>




<p>And there you have it. When running the rake task again, it never grew beyond 50m in size, even after processing 1000s of large images! And bye-bye to mongrel bloat.</p>




<p>Even though this specific example could be fixed in other ways (using MiniMagick for example), <b>my point is</b> that the same simple technique could be applied to any bloat-inducing code. In this case, it was just a couple lines of code.</p>




<p>Spawning processes is not without issues, however. But, <a href="http://github.com/tra/spawn">spawn</a> does a good job for simple things. There&#8217;s also <a href="http://github.com/purzelrakete/workling">workling</a>, which is more flexible, and solutions that do job queuing, such as <a href="http://github.com/tobi/delayed_job"> delayed_job</a> and <a href="http://github.com/ncr/background-fu">background-fu</a>. These libraries are mainly designed to offload <b>slow</b> tasks, but as I discovered, they can be effective for <b>memory-intesive</b> tasks as well.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Peggy2 Halloween Sketch]]></title>
    <link href="http://www.fozworks.com/blog/2009/10/31/peggy2-halloween-sketch/"/>
    <updated>2009-10-31T00:00:00+01:00</updated>
    <id>http://www.fozworks.com/blog/2009/10/31/peggy2-halloween-sketch</id>
    <content type="html"><![CDATA[<p>I made a little Arduino sketch for my <a href="http://evilmadscience.com/tinykitlist/75">Peggy2</a>.</p>

<object width="400" height="300"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=7362447&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=7362447&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="300"></embed></object>


<p><a href="http://vimeo.com/7362447">Peggy2 Halloween</a> from <a href="http://vimeo.com/user1587018">Jeremy Seitz</a> on <a href="http://vimeo.com">Vimeo</a>.</p>


<p>Source code is here:
<a href="http://github.com/somebox/peggy2-halloween">http://github.com/somebox/peggy2-halloween</a></p>

<p>Happy Halloween!!!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Automated Sphinx Install in Mac OS X Using Rake]]></title>
    <link href="http://www.fozworks.com/blog/2008/09/05/rake-installation-of-sphinx-in-mac-osx/"/>
    <updated>2008-09-05T00:00:00+02:00</updated>
    <id>http://www.fozworks.com/blog/2008/09/05/rake-installation-of-sphinx-in-mac-osx</id>
    <content type="html"><![CDATA[<p>One challenge for any team building a Rails project with <a href="http://www.sphinxsearch.com/">Sphinx</a>: keeping everyone up to date, on the same version of searchd. We wanted to make sure it was installed the same way, same version, on everyone&#8217;s dev machine. And we all work remotely of course :)</p>

<p>The solution for us was a <strong>rake task that downloads, compiles and installs Sphinx on OS X</strong> Leopard. We assume that you previously installed the developer tools (Mac OS X Install Disc 2, XCode).</p>

<p>To get the compile to work, you need to install mysql5 via <a href="http://macports.org">MacPorts</a> so you have the correct libs available. <code>sudo port install mysql5</code> Alas, we never were able to get Leopard&#8217;s MySQL libs to compile with Sphinx correctly. But never fear, installing MySQL via MacPorts will not affect the standard Apple mysqld server or client in any way.</p>

<h3>lib/tasks/sphinx.rake:</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="no">RAILS_ROOT</span><span class="si">}</span><span class="s2">/config/environment.rb&quot;</span>
</span><span class='line'><span class="no">SPHINX_SOURCE</span><span class="o">=</span><span class="s1">&#39;http://www.sphinxsearch.com/downloads/sphinx-0.9.8-rc2.tar.gz&#39;</span> <span class="c1"># r1234</span>
</span><span class='line'>
</span><span class='line'><span class="n">namespace</span> <span class="ss">:sphinx</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">desc</span> <span class="s2">&quot;Install Sphinx from source&quot;</span>
</span><span class='line'>  <span class="n">task</span> <span class="ss">:install</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">build_dir</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="no">ENV</span><span class="o">[</span><span class="s1">&#39;HOME&#39;</span><span class="o">]</span><span class="si">}</span><span class="s2">/tmp/sphinx&quot;</span>
</span><span class='line'>    <span class="nb">system</span> <span class="s2">&quot;rm -rf </span><span class="si">#{</span><span class="n">build_dir</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class='line'>    <span class="nb">system</span> <span class="s2">&quot;mkdir -p </span><span class="si">#{</span><span class="n">build_dir</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class='line'>    <span class="nb">puts</span> <span class="s2">&quot;Downloading Sphinx indexer from </span><span class="si">#{</span><span class="no">SPHINX_SOURCE</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class='line'>    <span class="n">cd</span> <span class="n">build_dir</span> <span class="k">do</span>
</span><span class='line'>      <span class="n">uri</span> <span class="o">=</span> <span class="no">URI</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="no">SPHINX_SOURCE</span><span class="p">)</span>
</span><span class='line'>      <span class="n">tarball</span> <span class="o">=</span> <span class="no">File</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">uri</span><span class="o">.</span><span class="n">path</span><span class="p">)</span>
</span><span class='line'>      <span class="no">Net</span><span class="o">::</span><span class="no">HTTP</span><span class="o">.</span><span class="n">start</span><span class="p">(</span><span class="n">uri</span><span class="o">.</span><span class="n">host</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">http</span><span class="o">|</span>
</span><span class='line'>        <span class="n">resp</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">uri</span><span class="o">.</span><span class="n">path</span><span class="p">)</span>
</span><span class='line'>        <span class="nb">open</span><span class="p">(</span><span class="n">tarball</span><span class="p">,</span> <span class="s2">&quot;wb&quot;</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">file</span><span class="o">|</span>
</span><span class='line'>          <span class="n">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">resp</span><span class="o">.</span><span class="n">body</span><span class="p">)</span>
</span><span class='line'>        <span class="k">end</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>      <span class="nb">system</span><span class="p">(</span><span class="s2">&quot;tar -xzf </span><span class="si">#{</span><span class="n">tarball</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
</span><span class='line'>      <span class="n">cd</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">build_dir</span><span class="si">}</span><span class="s2">/</span><span class="si">#{</span><span class="n">tarball</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="s1">&#39;.tar.gz&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="si">}</span><span class="s2">&quot;</span> <span class="k">do</span>
</span><span class='line'>        <span class="nb">system</span><span class="p">(</span><span class="s2">&quot;./configure --with-mysql-libs=/opt/local/lib/mysql5/mysql/ --with-mysql-includes=/opt/local/include/mysql5/mysql/&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="nb">system</span><span class="p">(</span><span class="s2">&quot;make&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="nb">puts</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Running &#39;sudo make install&#39; - this will install Sphinx.&quot;</span>
</span><span class='line'>        <span class="nb">system</span><span class="p">(</span><span class="s2">&quot;sudo make install&quot;</span><span class="p">)</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="nb">system</span><span class="p">(</span><span class="s2">&quot;sudo mkdir -p /opt/local/var/db/sphinx&quot;</span><span class="p">)</span>
</span><span class='line'>  <span class="nb">system</span><span class="p">(</span><span class="s2">&quot;sudo chown -R `whoami` /opt/local/var/db/sphinx&quot;</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The Worst Rails Code]]></title>
    <link href="http://www.fozworks.com/blog/2008/06/07/the-worst-rails-code/"/>
    <updated>2008-06-07T00:00:00+02:00</updated>
    <id>http://www.fozworks.com/blog/2008/06/07/the-worst-rails-code</id>
    <content type="html"><![CDATA[<p>I just came back from <a href="http://en.oreilly.com/rails2008/public/content/home">RailsConf 2008</a> in Portland. This year was great. There were a lot of exciting developments to talk about, like <a href="http://ruby.gemstone.com/">MagLev</a>, <a href="http://skynet.rubyforge.org/">SkyNet</a>, <a href="http://www.modrails.com/">mod_rails</a> and <a href="http://weblog.rubyonrails.org/2008/6/1/rails-2-1-time-zones-dirty-caching-gem-dependencies-caching-etc">Rails 2.1</a>.</p>

<p>The talks seemed better this year as well. The one I was most looking forward to was from <a href="http://blog.obiefernandez.com/">Obie Fernandez</a>, who wrote <a href="http://www.amazon.com/gp/product/0321445619?ie=UTF8&tag=obiefernandez-20&link_code=as3&camp=211189&creative=373489&creativeASIN=0321445619">The Rails Way</a>, published last fall. I can easily say this is the best Rails book published to date (sorry, Pragmatic). It&#8217;s packed with useful information, best practices, and real-world code. Obie&#8217;s excellent writing style along with contributions from numerous Rails coders make it a great read too. My copy is already showing wear. And at 900+ pages, it&#8217;s like a phone book.</p>

<p>Obie&#8217;s talk was given to a packed room, despite being scheduled on Sunday morning at 9am. The title of the talk, &#8221;<a href="http://blog.obiefernandez.com/content/2008/06/railsconf-2008.html">The Worst Rails Code You&#8217;ve Ever Seen (and how not to write it yourself)</a>&#8221;, discouraged my friends from attending (&#8220;sounds depressing&#8221;, one said). During the first lightning round, we had seen some pretty bad code proudly presented (to which Ryan Davis publicly expressed his horror).</p>

<p>But the talk was worth getting up for. Through a series of real-world examples, Obie (and co-presenter <a href="http://reinh.com/">Rein Henrichs</a>) showed the audience just how bad Rails coding can get. Some of the code was truly appalling, like a 1200+ line app in a single controller (no, really). Other examples looked, well, kind of familiar. Having been involved in several Rails projects myself since 2005, I&#8217;ve seen (and written) my share of bad code.</p>

<p>The talk started out with a bit of an elitist air, with the presenters snobbishly laughing at common mistakes. But as the talk wore on, the tone changed and became more helpful. At one point Obie showed some pretty ugly code and then admitted that he had actually written it.</p>

<p>But what really struck me about this talk was how programmers (myself included) often are unaware of &#8220;best practices&#8221;, or simply don&#8217;t understand Rails and Ruby well enough.</p>

<p>Now some of you reading this may think it&#8217;s all pretty obvious, basic stuff. But we all write bad code sometimes, even the slickest meta-programmers I know do. Here&#8217;s what I took away from the talk:</p>

<h2>Common Reasons People Write Bad Code</h2>

<p>1) Folks just don&#8217;t write tests, and as a result, they are afraid to refactor (and therefore improve) their code. It&#8217;s painful and dangerous to refactor without tests.</p>

<p>2) They are too lazy to look up the correct way to do something, either because they don&#8217;t like to read and research, or because they assume it will take longer than just &#8220;figuring it out&#8221;.</p>

<p>3) They Google for a solution to a problem and come across bad or misleading examples on someone&#8217;s blog (or a DZone snippet). You just can&#8217;t assume that because it&#8217;s posted on someone&#8217;s blog that it&#8217;s  correct. In reality, you should be suspicious of posts without comments or attribution. There are a ton of bad snippets and pasties out there.</p>

<h2>What Is Bad Code?</h2>

<p>It&#8217;s so important to understand the Rails framework. Rails already provides us with many best practices and time savers. It&#8217;s silly to waste time re-inventing an already solved problem.</p>

<p>Many of the examples from the talk confirmed that people are struggling with parameters, hashes, and passing data between controller actions.</p>

<ul>
<li>Ruby&#8217;s <a href="http://www.ruby-doc.org/core/classes/Hash.html">Hash</a> class gives us <a href="http://www.ruby-doc.org/core/classes/Hash.html#M000191">merge</a> and other useful methods, so there&#8217;s often no need to iterate over values or put in explicit assignments.</li>
<li>The Rails <strong>session</strong> object is the proper way to pass data between controller actions, and the <strong>flash</strong> object is there for displaying messages to the user (and let&#8217;s not forget <code>flash.now</code> for AJAX actions). Don&#8217;t use instance variables to pass messages to the view.</li>
<li>If you have validations on parameters in the controller, they should probably be in the model instead (<code>validates_presence_of</code>, etc).</li>
<li>Manipulating parameters outside of the context of a model (or other container) feels like PHP. It&#8217;s much nicer to call <code>@MyModel.new(params[:my_model])</code> and then validate. Why muck around with things like <code>@params[person][date][i1]</code>, etc?</li>
</ul>


<p>Other less-obvious suggestions to help clean up bad code included:</p>

<ul>
<li>Use <code>attr_protected</code> in models instead of worrying about how certain parameters might be set via the url or hacked forms</li>
<li>Avoid creating a big config class. Instead, use constants, and put them in the classes that need them.</li>
<li><code>application.rb</code> should only be for actions, put utility methods somewhere else. Like a module, or in models.</li>
<li>Consider using the <a href="http://blog.jayfields.com/2007/03/rails-presenter-pattern.html">presenter pattern</a> to clean up code</li>
</ul>


<h2>Where To Turn For Help</h2>

<ul>
<li><a href="http://www.amazon.com/Ruby-Way-Second-Addison-Wesley-Professional/dp/0672328844">The Ruby Way, 2nd ed.</a> (<a href="http://rubyhacker.com/">Hal Fulton</a>) and <a href="http://www.amazon.com/Rails-Way-Addison-Wesley-Professional-Ruby/dp/0321445619">The Rails Way</a>. They are both excellent.</li>
<li>Refer to the Rails API and Ruby standard library documentation as a primary resource</li>
<li>When you are having trouble understanding how to use a plugin, Gem, or Rails itself, don&#8217;t be afraid to <strong>look at the source code</strong>. It&#8217;s Ruby after all, and often surprisingly easy to understand.</li>
<li>Search (or post) on an appropriate IRC channel or forum</li>
<li>Study other people&#8217;s code. I learn a lot this way. Some of my favorites: Rick Olson, Ryan Davis, Eric Hodel and Evan Weaver.</li>
<li>Use <code>script/console</code>: this is often a really fast way to try a concept out, play with it and verify that it will really work.</li>
<li>Keep a library of your own code snippets. When you run across something useful, copy and paste it into a file locally that you can search through later.</li>
</ul>


<h2>Don&#8217;t Code Solo</h2>

<p>Obie also strongly recommended pair programming. Working with other programmers is a really great way to learn quickly. He specifically suggested pairing with a senior coder (like, a Smalltalk guru). For most of this, that&#8217;s not a realistic option, unless you are lucky enough to have expert contacts who are willing to spend that kind of time with you. But I can certainly agree that it&#8217;s so helpful to work through code with another person.</p>

<p>To that extent, I also found the talk about <a href="http://en.oreilly.com/rails2008/public/schedule/detail/1924">Remote Pair Programing</a> interesting, especially since I spend a lot of time in Europe while working on projects in the US. I definitely plan to pair program more often now!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[DemocracyNow.com is a Webby Honoree]]></title>
    <link href="http://www.fozworks.com/blog/2008/05/23/democracynow-com-is-a-webby-honoree/"/>
    <updated>2008-05-23T00:00:00+02:00</updated>
    <id>http://www.fozworks.com/blog/2008/05/23/democracynow-com-is-a-webby-honoree</id>
    <content type="html"><![CDATA[<p>Wow, I just found out today that <a href="http://democracynow.org">Democracy Now!</a> was a honoree this year in the <a href="http://www.webbyawards.com/webbys/current_honorees.php?media_id=96&category_id=45&season=12">News</a>, <a href="http://www.webbyawards.com/webbys/current_honorees.php?category_id=50">Political</a> and <a href="http://www.webbyawards.com/webbys/current_honorees.php?category_id=89">Podcast</a> categories. Congrats to everyone on the web dev team - it&#8217;s exciting to see our Rails effort up there ranked with such big names!</p>

<p><a href="http://democracynow.org" style="display: inline; padding-right: 2px"><img src="http://www.fozworks.com/assets/2008/5/23/dn-logo-for-podcast.png" alt="Democracy Now!" /></a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Geodistance Searching with Ultrasphinx]]></title>
    <link href="http://www.fozworks.com/blog/2008/05/01/geodistance-searching-with-ultrasphinx/"/>
    <updated>2008-05-01T00:00:00+02:00</updated>
    <id>http://www.fozworks.com/blog/2008/05/01/geodistance-searching-with-ultrasphinx</id>
    <content type="html"><![CDATA[<p>I&#8217;m happy to annouce a patch for <a href="http://blog.evanweaver.com/files/doc/fauna/ultrasphinx/files/README.html">Ultrasphinx</a> that enables access to the geographical distance searching in the <a href="http://www.sphinxsearch.com/">Sphinx</a> full-text search engine.</p>

<h3>Why</h3>


<p>Through my company <a href="http://somebox.com">Somebox</a>, I recently led a team that launched <a href="http://travelskoot.com">TravelSkoot.com</a> for NBC Digital Innovation, which is a Google Maps mash-up that allows people to group travel destinations together (called &#8220;skoots&#8221;) along with comments, ratings, etc. The entire project was built in Rails in about four months, and is living at <a href="http://engineyard.com">EngineYard</a>.</p>

<p>One of the challenges was to come up with an efficient way to search a large number of points, along with other metadata and fulltext searching. Normally, we would use <a href="http://geokit.rubyforge.org/">GeoKit</a> for this kind of thing, but once you combine fulltext with lots of other filters, things get complicated (and slow). That&#8217;s where <a href="http://www.sphinxsearch.com/">Sphinx</a> really shines.</p>

<p>I knew that Sphinx had <a href="http://www.sphinxsearch.com/doc.html#api-func-setgeoanchor">support for geodistance</a>, but unfortunately none of the Rails plugins did at the time. UltraSphinx offers more features than any of the other Rails/Sphinx plugins, and is based on Pat Allan&#8217;s excellent <a href="http://riddle.freelancing-gods.com/">Riddle Client</a>, which already had the basics for geodistance baked in (and required only <a href="http://code.google.com/p/rails-oceania/source/detail?r=559">a tiny patch</a> to make it work right). The rest of the changes were then made to the UltraSphinx plugin to make it usable.</p>

<h3>How it Works</h3>


<p>To set up UltraSphinx for geodistance searches, you need to declare your latitude and longitude columns in the model. Since Sphinx expects these to be stored in radians, you can use the :function_sql option of is_indexed to do the conversion:</p>

<pre>
class Point < ActiveRecord::Base
  is_indexed :fields => [
    :title,
    :description,
    {:field => "lat", :function_sql => 'RADIANS(?)'},
    {:field => "lng", :function_sql => 'RADIANS(?)'}
  ]
end
</pre>


<p>The search itself ends up looking like this:</p>

<pre>
@search = Ultrasphinx::Search.new(
  :query          => 'pizza',
  :sort_mode   => 'extended',
  :sort_by        => 'distance asc',
  :filters        => {'distance' => 0..10000},
  :location => {
    :units => 'degrees',
    :lat => 40.343,
    :long => -74.233
  }
)
@search.run
</pre>


<p>The actual distance is then available in your models (in meters):</p>

<pre>
@search.results.first.distance
</pre>


<p>You can also filter and sort results by distance and combine all the other features of UltraSphinx (faceting, weighting, etc).</p>

<h3>Thanks</h3>


<p>Open source rocks. <a href="http://www.workingwithrails.com/person/7739-evan-weaver">Evan Weaver</a> was very encouraging in helping this patch along, cleaning up the API, and guiding discussion. A lot of support came through <a href="http://www.workingwithrails.com/person/12039-dr-mark-lane">Dr. Mark Lane</a>, who helped guide me through the internals, rewrote the tests in his roll-up patches, and pushed me to finish. Also thanks to <a href="http://workingwithrails.com/person/8564-michael-hill">Michael Hill</a>, <a href="http://bumi.wordpress.com/">Michael Burmann</a>, and <a href="http://rubyforge.org/users/jasonlee9/">Jason Lee</a> for testing and feedback.</p>

<h3>Footnotes</h3>


<p>In order to use the geodistance features of UltraSphinx, you need to be using version 1.10 or higher (which also requires at minimum Sphinx v1198). These geo features are still young, and not without some minor issues. Be sure to consult the <a href="http://rubyforge.org/forum/forum.php?forum_id=14244">forum</a> for answers (or submit a patch!), or leave a comment here if you need help. And if you end up using this patch in your project, <a href="http://workingwithrails.com/recommendation/new/person/8561-jeremy-seitz">recommend me at WWR</a>!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Syntactical Sugar]]></title>
    <link href="http://www.fozworks.com/blog/2008/04/28/syntactical-sugar/"/>
    <updated>2008-04-28T00:00:00+02:00</updated>
    <id>http://www.fozworks.com/blog/2008/04/28/syntactical-sugar</id>
    <content type="html"><![CDATA[<p>On recent rails projects, I found myself clinging to a few useful helpers and additions. Here&#8217;s a few.</p>

<h2>hide_unless</h2>

<p>Often in views, I find I want to hide a particular div or element, but only if certain conditions are met. An ajax call might later reveal it, or replace the contents of the div with something.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'> <span class="k">def</span> <span class="nf">hide_unless</span><span class="p">(</span><span class="n">condition</span><span class="p">)</span>
</span><span class='line'>    <span class="n">condition</span> <span class="p">?</span> <span class="s1">&#39;&#39;</span> <span class="p">:</span> <span class="s1">&#39;display:none&#39;</span>
</span><span class='line'> <span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>In use:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='rhtml'><span class='line'><span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;edit_pane&quot;</span> <span class="na">style=</span><span class="s">&quot;</span><span class="cp">&lt;%=</span> <span class="n">hide_unless</span><span class="p">(</span><span class="vi">@story</span><span class="p">)</span> <span class="cp">%&gt;</span><span class="s">&quot;</span><span class="nt">&gt;&lt;/div&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<h2>present?</h2>

<p>Rails gives us .blank?, but I hate writing things like:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='rhtml'><span class='line'>  <span class="cp">&lt;%</span> <span class="k">if</span> <span class="o">!</span><span class="vi">@stories</span><span class="o">.</span><span class="n">blank?</span> <span class="cp">%&gt;</span>
</span><span class='line'>    ... etc
</span><span class='line'>  <span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>So, I add this as an extension in my projects:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Object</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">present?</span>
</span><span class='line'>    <span class="o">!</span><span class="n">blank?</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>And obviously it works on anything: arrays, hashes, nil, etc.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='rhtml'><span class='line'><span class="cp">&lt;%</span> <span class="k">if</span> <span class="n">session</span><span class="o">[</span><span class="ss">:setting</span><span class="o">].</span><span class="n">present?</span> <span class="cp">%&gt;</span>
</span><span class='line'>   etc...
</span><span class='line'>  <span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>UPDATE (29-Jun-2008)</strong>: DHH just <a href="http://github.com/rails/rails/commit/a3caf28da3a22c1326d3d98dcf71483a8edaa55a">committed this to Edge Rails</a>. I am certain that I had nothing to do with it, but I&#8217;ll pretend :)</p>

<h2>user_owns_it</h2>

<p>A common task is to check if the current user owns a particular resource.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'>  <span class="k">def</span> <span class="nf">user_owns_it</span><span class="p">(</span><span class="n">asset</span><span class="p">)</span>
</span><span class='line'>    <span class="n">asset</span><span class="o">.</span><span class="n">respond_to?</span><span class="p">(</span><span class="ss">:created_by</span><span class="p">)</span> <span class="ow">and</span> <span class="n">asset</span><span class="o">.</span><span class="n">created_by</span> <span class="ow">and</span> <span class="n">current_user</span> <span class="ow">and</span> <span class="n">asset</span><span class="o">.</span><span class="n">created_by</span><span class="o">.</span><span class="n">id</span> <span class="o">==</span> <span class="n">current_user</span><span class="o">.</span><span class="n">id</span>
</span><span class='line'>  <span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>This allows easy checking in views:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='rhtml'><span class='line'><span class="cp">&lt;%</span> <span class="k">if</span> <span class="n">user_owns_it</span><span class="p">(</span><span class="vi">@post</span><span class="p">)</span> <span class="cp">%&gt;</span>
</span><span class='line'>   link_to &quot;Edit Your Post&quot;, edit_post_path(@post)
</span><span class='line'><span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span>
</span></code></pre></td></tr></table></div></figure>




<hr/>


<p>Please share if you have other interesting tidbits from your toolbox!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Fix for slow gem updates]]></title>
    <link href="http://www.fozworks.com/blog/2008/04/18/fix-for-slow-gem-updates/"/>
    <updated>2008-04-18T00:00:00+02:00</updated>
    <id>http://www.fozworks.com/blog/2008/04/18/fix-for-slow-gem-updates</id>
    <content type="html"><![CDATA[<p>Lately, rubygems seems to be slow when updating. I guess there&#8217;s a lot more gems being released than ever before. As a result, running gem update is painful:</p>

<pre>
$ sudo gem update
Updating installed gems...
Updating metadata for 345 gems from http://gems.rubyforge.org
....................... 
</pre>


<p>Argh! Turns out that there&#8217;s a &#8220;buik update&#8221; setting, and the default threshold is 1000. Metadata will be downloaded a gem at a time if there are 999 to get. Fortunately, it can be changed, by passing the -B flag to gem commands, or you can put this in ~/.gemrc :</p>

<pre>
update: -B 10
install: -B 10
</pre>


<p>Now your gem updates will be much faster.</p>

<p><strong>Update:</strong> <a href="http://blog.zenspider.com/">zenspider</a> notes: <em>the latest (last 2 actually) version of rubygems has http keepalive so it should be much much much faster and the bulk update threshold setting shouldn&#8217;t be necessary.</em></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Autotest Sounds with playlists!]]></title>
    <link href="http://www.fozworks.com/blog/2008/04/09/autotest-sounds-with-playlists/"/>
    <updated>2008-04-09T00:00:00+02:00</updated>
    <id>http://www.fozworks.com/blog/2008/04/09/autotest-sounds-with-playlists</id>
    <content type="html"><![CDATA[<p>Ken Collins put together an awesome update to the autotest sound plugin. His version supports a playlist directory, so you can easily cycle through different init, red and green sounds. His sounds are hilarious!</p>

<p>&#8220;http://www.metaskills.net/2008/4/6/autotest-playlist-for-red-green-feedback&#8221;:http://www.metaskills.net/2008/4/6/autotest-playlist-for-red-green-feedback</p>

<p>I&#8217;ve been using it all day :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My .irbrc]]></title>
    <link href="http://www.fozworks.com/blog/2007/12/23/my-irbrc/"/>
    <updated>2007-12-23T00:00:00+01:00</updated>
    <id>http://www.fozworks.com/blog/2007/12/23/my-irbrc</id>
    <content type="html"><![CDATA[<p>Some collected recipes to make irb and script/console a bit nicer to use:</p>

<ul>
<li>pretty printing (<code>pp whatever</code>)</li>
<li>enhanced tab completion</li>
<li>rails logging to console</li>
<li>saves command history between sessions</li>
<li>use readline extensions module</li>
</ul>


<p>Tips were collected from posts by &#8220;Dr. Nic&#8221;:http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/, &#8220;Toolman Tim&#8221;:http://toolmantim.com/article/2007/2/6/system_wide_script_console_logging and &#8220;Dzone Snippets&#8221;:http://snippets.dzone.com/posts/show/4371.</p>

<h2>~/.irbrc</h2>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'>  <span class="nb">require</span> <span class="s1">&#39;pp&#39;</span>
</span><span class='line'>  <span class="nb">require</span> <span class="s1">&#39;irb/completion&#39;</span>
</span><span class='line'>  <span class="no">ARGV</span><span class="o">.</span><span class="n">concat</span> <span class="o">[</span> <span class="s2">&quot;--readline&quot;</span><span class="p">,</span> <span class="s2">&quot;--prompt-mode&quot;</span><span class="p">,</span> <span class="s2">&quot;simple&quot;</span> <span class="o">]</span>
</span><span class='line'>  <span class="no">IRB</span><span class="o">.</span><span class="n">conf</span><span class="o">[</span><span class="ss">:AUTO_INDENT</span><span class="o">]=</span><span class="kp">true</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># load console_with_helpers if possible</span>
</span><span class='line'>  <span class="n">script_console_running</span> <span class="o">=</span> <span class="no">ENV</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="s1">&#39;RAILS_ENV&#39;</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="no">IRB</span><span class="o">.</span><span class="n">conf</span><span class="o">[</span><span class="ss">:LOAD_MODULES</span><span class="o">]</span> <span class="o">&amp;&amp;</span> <span class="no">IRB</span><span class="o">.</span><span class="n">conf</span><span class="o">[</span><span class="ss">:LOAD_MODULES</span><span class="o">].</span><span class="n">include?</span><span class="p">(</span><span class="s1">&#39;console_with_helpers&#39;</span><span class="p">)</span>
</span><span class='line'>  <span class="n">rails_running</span> <span class="o">=</span> <span class="no">ENV</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="s1">&#39;RAILS_ENV&#39;</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="p">(</span><span class="no">IRB</span><span class="o">.</span><span class="n">conf</span><span class="o">[</span><span class="ss">:LOAD_MODULES</span><span class="o">]</span> <span class="o">&amp;&amp;</span> <span class="no">IRB</span><span class="o">.</span><span class="n">conf</span><span class="o">[</span><span class="ss">:LOAD_MODULES</span><span class="o">].</span><span class="n">include?</span><span class="p">(</span><span class="s1">&#39;console_with_helpers&#39;</span><span class="p">))</span>
</span><span class='line'>  <span class="n">irb_standalone_running</span> <span class="o">=</span> <span class="o">!</span><span class="n">script_console_running</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">rails_running</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># log Rails stuff to STDOUT</span>
</span><span class='line'>  <span class="k">if</span> <span class="n">script_console_running</span>
</span><span class='line'>    <span class="nb">require</span> <span class="s1">&#39;logger&#39;</span>
</span><span class='line'>    <span class="no">Object</span><span class="o">.</span><span class="n">const_set</span><span class="p">(</span><span class="ss">:RAILS_DEFAULT_LOGGER</span><span class="p">,</span> <span class="no">Logger</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="no">STDOUT</span><span class="p">))</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># Keep command-line history between startups</span>
</span><span class='line'>  <span class="nb">require</span> <span class="s1">&#39;irb/ext/save-history&#39;</span>
</span><span class='line'>  <span class="no">IRB</span><span class="o">.</span><span class="n">conf</span><span class="o">[</span><span class="ss">:SAVE_HISTORY</span><span class="o">]</span> <span class="o">=</span> <span class="mi">100</span>
</span><span class='line'>  <span class="no">IRB</span><span class="o">.</span><span class="n">conf</span><span class="o">[</span><span class="ss">:HISTORY_FILE</span><span class="o">]</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="no">ENV</span><span class="o">[</span><span class="s1">&#39;HOME&#39;</span><span class="o">]</span><span class="si">}</span><span class="s2">/.irb-save-history&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p><em>Update:</em> my friend &#8220;Joannou&#8221;:http://joannou.tumblr.com/ pointed out <a href="http://utilitybelt.rubyforge.org/">Utility Belt</a> which looks pretty nice also.</p>

<p><em>Update 2 (a month later)</em>: Been using Utility Belt for a while and noticed some problems&#8230; it has a tendency to conflict with ActiveRecord validations and trigger bogus errors during callbacks. Also seems to destroy some of the init process with certain plugins like ActiveScaffold. Perhaps it&#8217;s too clever. I ended up rolling back to my old .irbc - YMMV.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Autotest: Now, With Sound Effects!]]></title>
    <link href="http://www.fozworks.com/blog/2007/07/28/autotest-sound-effects/"/>
    <updated>2007-07-28T00:00:00+02:00</updated>
    <id>http://www.fozworks.com/blog/2007/07/28/autotest-sound-effects</id>
    <content type="html"><![CDATA[<p style="padding: 6px;background-color: rgb(255, 255, 153);" >Update April 9, 2008: Ken Collins has released a <a href="http://www.metaskills.net/2008/4/6/autotest-playlist-for-red-green-feedback">new version of the sound plugin with playlist support</a>!</strong></p>


<p>We&#8217;ve all been enjoying <em>autotest</em>, part of the <a href="http://www.zenspider.com/ZSS/Products/ZenTest">ZenTest</a> gem. If you&#8217;ve tricked out your kit, then you have <a href="http://ph7spot.com/articles/getting_started_with_autotest">plugins</a> configured, so at minimum you&#8217;re <a href="http://www.ridaalbarazi.com/blog/2007/04/05/autotest-growling-in-red-green/">red, green and growling</a>.
Now, things get really fun.</p>

<h3><a href="http://www.fozworks.com/static/autotest-sound10.html">Watch a screencast of autotest running with sound effects</a></h3>

<p>I&#8217;m stoked to announce the <strong>sound plugin for autotest</strong>. This simple chunk of code will fire off sounds for different events in autotest. I&#8217;ve provided a set of custom-made sounds, produced with my trusty <a href="http://www.clavia.se/products/nordmodular/models.htm">Nord Modular</a> synthesizer and fine-tuned for an optimal testing experience. You should be able to use these all day without annoying your neighbors too much.</p>

<p>Here&#8217;s what you need to do:</p>

<h2>1. Install mgp321</h2>

<p>in OS X:</p>

<pre><code>$ sudo port install mpg321
</code></pre>

<p>for Linux:</p>

<pre><code>$ sudo apt-get install mpg321
</code></pre>

<h2>2. Download and extract the plugin</h2>

<p>The starter sound fx are in the zip file. Extract it in your home directory, it will create <code>~/autotest/sound</code>.</p>

<p><img src="http://www.fozworks.com/static/pdf.gif" alt="static" /> <a href="http://www.fozworks.com/static/autotest-sound-1_2.zip">autotest-sound-1_2.zip</a> (86k)</p>

<h2>3. Configure your ~/.autotest file:</h2>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;~/autotest/sound/sound.rb&#39;</span>
</span><span class='line'><span class="no">Autotest</span><span class="o">::</span><span class="no">Sound</span><span class="o">.</span><span class="n">sound_path</span> <span class="o">=</span> <span class="s2">&quot;~/autotest/sound/sound_fx/&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Enjoy <a href="http://en.wikipedia.org/wiki/Test-driven_development">TDD</a> with audio feedback!</h2>

<p>I&#8217;ve been using this setup for several weeks now. I initially wrote it as a gag, but I have since found it to be incredibly useful. It&#8217;s nice know what your testing status via audio - you don&#8217;t have to switch windows or take your eyes off the code. I&#8217;ve even turned off Growl, I don&#8217;t need it any more. audio makes testing more fun. :)</p>

<p>If there are any problems or feedback, please post a comment here.</p>

<p><strong>UPDATE:</strong> Plugin instructions and zip file updated, now with Windows support. Thanks, John and Jamie.</p>

<p><strong>UPDATE #2:</strong> Fixed bad path in instructions and doc fixes in zip file. (thanks, Matt)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Healthy Migrations]]></title>
    <link href="http://www.fozworks.com/blog/2007/06/01/how-to-fix-fixtures/"/>
    <updated>2007-06-01T00:00:00+02:00</updated>
    <id>http://www.fozworks.com/blog/2007/06/01/how-to-fix-fixtures</id>
    <content type="html"><![CDATA[<p>Continuing with the <a href="http://www.fozworks.com/2007/5/23/validating-fixtures">fixtures/test theme</a>, I want to focus on the place where fixtures actually live - the database. Migrations are the blueprint, however, they often break and we don&#8217;t notice. You should alway be able to do this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ rake db:migrate VERSION=0
</span><span class='line'>$ rake db:migrate</span></code></pre></td></tr></table></div></figure>


<p>I used to say &#8220;what does it matter? We&#8217;re never going back to migration 3, we&#8217;re on 156 now!&#8221; This kind of thinking showed how I didn&#8217;t understand the <em>usefulness</em> of migrations:</p>

<ul>
<li>Setting up a new development system should not require a recent database snapshot.</li>
<li>Automated tests and build notifications are simpler when the migrations are clean.</li>
<li><em>cap rollback</em> will save your life some day</li>
</ul>


<p>Migrations are not just a historical record of your database design. They instead give you a way to build your database up from scratch, doing more than just creating a schema. You can seed data, create indexes, and make transformations.</p>

<p>When you first start a rails project, and everything is golden, migrations are easy. Eventually, you run into problems. It happens a lot, because we typically don&#8217;t test the entire migration sequence. For example:</p>

<h2>A model changes somewhere, and breaks a dependent migration</h2>

<p>Using models in migrations is a common way to seed the database, or manipulate things:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">CreateNewsSection</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Migration</span>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">up</span>
</span><span class='line'>    <span class="no">Section</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:name</span> <span class="o">=&gt;</span> <span class="s1">&#39;news&#39;</span><span class="p">,</span> <span class="ss">:title</span><span class="o">=&gt;</span><span class="s2">&quot;News&quot;</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">down</span>
</span><span class='line'>    <span class="no">Section</span><span class="o">.</span><span class="n">find_by_name</span><span class="p">(</span><span class="s1">&#39;news&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">destroy</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you delete or refactor the Section model later, this migration will likely break. The <a href="http://toolmantim.com/article/2006/2/23/migrating_with_models">solution for this one</a> is to define the model in the migration:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">CreateNewsSection</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Migration</span>
</span><span class='line'>  <span class="k">class</span> <span class="nc">Section</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span><span class="p">;</span> <span class="k">end</span>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">up</span>
</span><span class='line'>    <span class="no">Section</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:name</span> <span class="o">=&gt;</span> <span class="s1">&#39;news&#39;</span><span class="p">,</span> <span class="ss">:title</span><span class="o">=&gt;</span><span class="s2">&quot;News&quot;</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>  <span class="o">.</span><span class="n">.</span><span class="o">.</span> <span class="n">etc</span><span class="o">.</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Someone on the team checks in a migration that has a bug</h2>

<p>If the problem is trivial, they might be tempted to skip reporting it and just fix it in the database to keep things moving. Or, they may not even notice the problem, depending on <em>when</em> they updated. These issues can lead to inconsistencies, and tests that pass for one developer, but not another!</p>

<h2>Developers only migrate up</h2>

<p>Migrating down should work too, what if you need to roll back to fix something in production? Always write a sensible down method and test it. It does not have to perfectly reverse the database, it just needs to return it to a state that will enable the previous migration to run it&#8217;s down method. I&#8217;ve seen horrific migrations checked in like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">down</span>
</span><span class='line'>    <span class="c1"># no need to do this</span>
</span><span class='line'>  <span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h2>The team works from a production db snapshot based on a deployed site</h2>

<p>This is bad, because it means the team is probably not using <a href="http://en.wikipedia.org/wiki/Test-driven_development">TDD</a>, and are instead relying on browser interaction to develop the app. At minimum, they are blind to migration issues. Relying on an external database for development is an unwise dependency. It also complicates setup for testing.</p>

<h2>Keep Migrations Working!</h2>

<p>Each time you add a migration, or refactor a number models, you should check that  <em>all</em> the migrations are working. There are a number of solutions for doing this - the most obvious is to drop the dev db and migrate up from scratch, see if it works.</p>

<p><a href="http://errtheblog.com/post/3">Err the blog</a> posted a task a while back. There&#8217;s also this <a href="http://snippets.dzone.com/posts/show/2031">often referenced snippet</a> that works. And today, I noticed <a href="http://ryandaigle.com/articles/2007/5/29/what-s-new-in-edge-rails-new-database-rake-tasks">this post on Ryan&#8217;s Scraps</a> &#8211; it looks like Rails itself now has <a href="http://dev.rubyonrails.org/changeset/6849">a task to do this</a>.</p>

<p>However, my favorite solution at the moment is sitting in a <a href="http://dev.rubyonrails.org/ticket/8389">patch #8389</a> (not committed at this date), which offers this bit of sugar:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># in config/environment.rb:</span>
</span><span class='line'><span class="n">config</span><span class="o">.</span><span class="n">active_record</span><span class="o">.</span><span class="n">schema_format</span> <span class="o">=</span> <span class="ss">:migrations</span>
</span></code></pre></td></tr></table></div></figure>


<p>This setting would force rails to build the database schema from migrations, not from sql or <code>db/schema.rb</code>.</p>

<p>So before you check in migrations, make sure you can run them up from scratch. And then, don&#8217;t forget to make sure <a href="http://fozworks.com/2007/5/23/validating-fixtures">your fixtures are still valid</a>, too!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Validating Fixtures]]></title>
    <link href="http://www.fozworks.com/blog/2007/05/23/validating-fixtures/"/>
    <updated>2007-05-23T00:00:00+02:00</updated>
    <id>http://www.fozworks.com/blog/2007/05/23/validating-fixtures</id>
    <content type="html"><![CDATA[<h2>Fixtures Are Painful</h2>

<p>I just got back from RailsConf 2007, it was a brain-expanding conference. I spent a lot of time talking to Rails coders about how they do testing and use fixtures. I saw some patterns emerge. Generally, people agree that fixtures are painful:</p>

<ul>
<li>referencing ids in the fixtures is annoying and prone to error</li>
<li>the lack of a grouping mechanism makes selecting them harder</li>
<li>fixtures get out of date with model changes</li>
</ul>


<p>I plan to do a few posts about fixtures in the next coming weeks, to share some of what I have learned. For now, I&#8217;ll focus on validation.</p>

<h2>Fixtures Break</h2>

<p>The project I&#8217;m currently working on has a large number of fixtures (over 100 models). It&#8217;s become really hard to manage them all. Over time, some fixtures busted, and it became hard to diagnose random problems in tests.</p>

<p>We are fortunate to be working with &#8220;zenspider&#8221;:http://blog.zenspider.com/, he&#8217;s helping us get our tests, dev process and performance in better shape. I complained a lot about invalid fixtures, and how I longed for a Rake task that could identify the broken ones. He offered up this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">namespace</span> <span class="ss">:db</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">namespace</span> <span class="ss">:fixtures</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">task</span> <span class="ss">:validate</span> <span class="o">=&gt;</span> <span class="ss">:environment</span> <span class="k">do</span>
</span><span class='line'>      <span class="n">name_map</span> <span class="o">=</span> <span class="no">Hash</span><span class="o">.</span><span class="n">new</span> <span class="p">{</span> <span class="o">|</span><span class="n">h</span><span class="p">,</span><span class="n">k</span><span class="o">|</span> <span class="n">h</span><span class="o">[</span><span class="n">k</span><span class="o">]</span> <span class="o">=</span> <span class="n">k</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>      <span class="no">Dir</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s2">&quot;app/models&quot;</span><span class="p">)</span> <span class="k">do</span>
</span><span class='line'>        <span class="n">map</span> <span class="o">=</span> <span class="sb">`grep set_table_name *.rb`</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">/[:\&#39;\&quot;]+|set_table_name/</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">split</span>
</span><span class='line'>        <span class="no">Hash</span><span class="o">[*</span><span class="n">map</span><span class="o">].</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">file</span><span class="p">,</span> <span class="nb">name</span><span class="o">|</span>
</span><span class='line'>          <span class="n">name_map</span><span class="o">[</span><span class="nb">name</span><span class="o">]</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sr">/\.rb$/</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
</span><span class='line'>        <span class="k">end</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>      <span class="no">Dir</span><span class="o">[</span><span class="s2">&quot;test/fixtures/*.yml&quot;</span><span class="o">].</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">fixture</span><span class="o">|</span>
</span><span class='line'>        <span class="n">fixture</span> <span class="o">=</span> <span class="n">name_map</span><span class="o">[</span><span class="no">File</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">fixture</span><span class="p">,</span> <span class="s2">&quot;.yml&quot;</span><span class="p">)</span><span class="o">]</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">begin</span>
</span><span class='line'>          <span class="n">klass</span> <span class="o">=</span> <span class="n">fixture</span><span class="o">.</span><span class="n">classify</span><span class="o">.</span><span class="n">constantize</span>
</span><span class='line'>          <span class="n">klass</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="ss">:all</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">thing</span><span class="o">|</span>
</span><span class='line'>            <span class="k">unless</span> <span class="n">thing</span><span class="o">.</span><span class="n">valid?</span> <span class="k">then</span>
</span><span class='line'>              <span class="nb">puts</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">fixture</span><span class="si">}</span><span class="s2">: id #</span><span class="si">#{</span><span class="n">thing</span><span class="o">.</span><span class="n">id</span><span class="si">}</span><span class="s2"> is invalid:&quot;</span>
</span><span class='line'>              <span class="n">thing</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">full_messages</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">msg</span><span class="o">|</span>
</span><span class='line'>                <span class="nb">puts</span> <span class="s2">&quot;   - </span><span class="si">#{</span><span class="n">msg</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class='line'>              <span class="k">end</span>
</span><span class='line'>            <span class="k">end</span>
</span><span class='line'>          <span class="k">end</span>
</span><span class='line'>        <span class="k">rescue</span> <span class="o">=&gt;</span> <span class="n">e</span>
</span><span class='line'>          <span class="nb">puts</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">fixture</span><span class="si">}</span><span class="s2">: skipping: </span><span class="si">#{</span><span class="n">e</span><span class="o">.</span><span class="n">message</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class='line'>        <span class="k">end</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>    <span class="k">end</span> <span class="c1"># validate</span>
</span><span class='line'>  <span class="k">end</span> <span class="c1"># fixtures</span>
</span><span class='line'><span class="k">end</span> <span class="c1"># db</span>
</span></code></pre></td></tr></table></div></figure>


<p>Put it in your project Rakefile, and you can then run:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'><span class="nv">$ </span>rake db:fixtures:validate
</span></code></pre></td></tr></table></div></figure>


<p>You will get back a list of all the fixtures that are not valid, with the validation messages.</p>

<p>But what about edge cases? What about &#8220;bad&#8221; form data? DHH has declared that <a href="http://dev.rubyonrails.org/changeset/6227">most folks want all fixtures loaded</a> at the start of the tests. The data in fixtures does not have to pass model validation before it is loaded into the db.</p>

<p>After discussing this with a number of folks, I&#8217;m starting to believe:</p>

<ul>
<li>All fixtures should be valid at all times</li>
<li>Fixtures should provide stuff required by your app to run tests (your admin user, default options, etc)</li>
<li>Edge cases should be created in tests, not fixtures</li>
<li>Test invalid data by loading a good fixture and changing it</li>
</ul>


<p><code>rake db:fixtures:validate</code> is helpful for keeping them valid. I plan to use it whenever I do a migration, just to ensure that nothing got broken.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[RailsConf 2007]]></title>
    <link href="http://www.fozworks.com/blog/2007/05/16/railsconf-2007/"/>
    <updated>2007-05-16T00:00:00+02:00</updated>
    <id>http://www.fozworks.com/blog/2007/05/16/railsconf-2007</id>
    <content type="html"><![CDATA[<p>Heading out to Portland tomorrow, I&#8217;m very excited. There are a whole lot of great presentations this year. Here&#8217;s what <a href="http://myconfplan.com/users/somebox/conferences/RailsConf2007">I plan to see</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rake task to run a custom group of tests]]></title>
    <link href="http://www.fozworks.com/blog/2007/04/16/rake-task-to-run-a-custom-group-of-tests/"/>
    <updated>2007-04-16T00:00:00+02:00</updated>
    <id>http://www.fozworks.com/blog/2007/04/16/rake-task-to-run-a-custom-group-of-tests</id>
    <content type="html"><![CDATA[<p>I have not really been getting into blogging yet, obviously. It&#8217;s been several months since the most recent post. I&#8217;m in New York at the moment, getting ready to launch a rails web project. More details about that later.</p>

<p>For now, I just had to post something I did recently that was handy: Using rake to run a specific group of files.</p>

<p>There are some great built-in tasks provided by Rake. Let&#8217;s look at the testing-related ones (run <code>rake -T</code> to see them all):</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>rake test:functionals               # Run the functional tests in test/functional
</span><span class='line'>rake test:integration               # Run the integration tests in test/integration
</span><span class='line'>rake test:plugins                   # Run the plugin tests in vendor/plugins/*/**/test (or specify with PLUGIN=name)
</span><span class='line'>rake test:recent                    # Test recent changes
</span><span class='line'>rake test:uncommitted               # Test changes since last checkin (only Subversion)
</span><span class='line'>rake test:units                     # Run the unit tests in test/unit</span></code></pre></td></tr></table></div></figure>


<p>These are all very useful. But on the site I&#8217;m currently working on, I wanted to run a set of tests that were related to a <em>section</em> of the web site: the web store. I wanted to be able to run a specific combination of unit, functional, and integration tests.</p>

<p>I looked at the source for the tasks that rails provides for rake. The testing tasks are defined in lib/tasks/testing.rake in the rails distribution. The code for the unit testing task, for example:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'>  <span class="n">desc</span> <span class="s2">&quot;Run the unit tests in test/unit&quot;</span>
</span><span class='line'>  <span class="no">Rake</span><span class="o">::</span><span class="no">TestTask</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">:units</span> <span class="o">=&gt;</span> <span class="s2">&quot;db:test:prepare&quot;</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">t</span><span class="o">|</span>
</span><span class='line'>    <span class="n">t</span><span class="o">.</span><span class="n">libs</span> <span class="o">&lt;&lt;</span> <span class="s2">&quot;test&quot;</span>
</span><span class='line'>    <span class="n">t</span><span class="o">.</span><span class="n">pattern</span> <span class="o">=</span> <span class="s1">&#39;test/unit/**/*_test.rb&#39;</span>
</span><span class='line'>    <span class="n">t</span><span class="o">.</span><span class="n">verbose</span> <span class="o">=</span> <span class="kp">true</span>
</span><span class='line'>  <span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The files are specified as a glob. But, I did not want to use a file pattern. I consulted the <a href="http://rake.rubyforge.org/">rake API docs</a> and discovered the <code>test_files=</code> setter method of <a href="http://rake.rubyforge.org/classes/Rake/TestTask.html">Rake::TestTask</a>. Here&#8217;s what I ended up with:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">namespace</span> <span class="ss">:test</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">tests</span> <span class="o">=</span> <span class="o">[]</span>
</span><span class='line'>  <span class="n">tests</span> <span class="o">&lt;&lt;</span> <span class="s2">&quot;test/integration/store_test.rb&quot;</span>
</span><span class='line'>  <span class="c1"># models</span>
</span><span class='line'>  <span class="sx">%w(product product_category product_categorization product_option product_option_value </span>
</span><span class='line'><span class="sx">    line_item cart order shipment)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">file</span><span class="o">|</span>
</span><span class='line'>      <span class="n">tests</span> <span class="o">&lt;&lt;</span> <span class="s2">&quot;test/unit/</span><span class="si">#{</span><span class="n">file</span><span class="si">}</span><span class="s2">_test.rb&quot;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>  <span class="c1"># controllers</span>
</span><span class='line'>  <span class="sx">%w(cart checkout store)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">file</span><span class="o">|</span>
</span><span class='line'>    <span class="n">tests</span> <span class="o">&lt;&lt;</span> <span class="s2">&quot;test/functional/</span><span class="si">#{</span><span class="n">file</span><span class="si">}</span><span class="s2">_controller_test.rb&quot;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>  <span class="c1"># admin controllers</span>
</span><span class='line'>  <span class="sx">%w(products product_option_values product_options orders)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">file</span><span class="o">|</span>
</span><span class='line'>    <span class="n">tests</span> <span class="o">&lt;&lt;</span> <span class="s2">&quot;test/functional/admin/</span><span class="si">#{</span><span class="n">file</span><span class="si">}</span><span class="s2">_controller_test.rb&quot;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>  <span class="no">Rake</span><span class="o">::</span><span class="no">TestTask</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">:store</span> <span class="o">=&gt;</span> <span class="s2">&quot;db:test:prepare&quot;</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">t</span><span class="o">|</span>
</span><span class='line'>    <span class="n">t</span><span class="o">.</span><span class="n">libs</span> <span class="o">&lt;&lt;</span> <span class="s2">&quot;test&quot;</span>
</span><span class='line'>    <span class="n">t</span><span class="o">.</span><span class="n">test_files</span> <span class="o">=</span> <span class="n">tests</span>
</span><span class='line'>    <span class="n">t</span><span class="o">.</span><span class="n">verbose</span> <span class="o">=</span> <span class="kp">true</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>  <span class="no">Rake</span><span class="o">::</span><span class="no">Task</span><span class="o">[</span><span class="s1">&#39;test:store&#39;</span><span class="o">].</span><span class="n">comment</span> <span class="o">=</span> <span class="s2">&quot;Run the store-related tests&quot;</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>I saved this task in lib/tasks/store_tests.rake under my project directory. Now, I can do this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'><span class="nv">$ </span>rake <span class="nb">test</span>:store
</span><span class='line'><span class="o">(</span>in ~/rails_project<span class="o">)</span>
</span><span class='line'>/opt/local/bin/ruby -Ilib:test <span class="s2">&quot;/opt/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader.rb&quot;</span> <span class="s2">&quot;test/integration/store_test.rb&quot;</span> <span class="s2">&quot;test/unit/product_test.rb&quot;</span> <span class="s2">&quot;test/unit/product_category_test.rb&quot;</span> <span class="s2">&quot;test/unit/product_categorization_test.rb&quot;</span>  <span class="o">[</span>... etc ...<span class="o">]</span>
</span><span class='line'>Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader
</span><span class='line'>Started
</span><span class='line'>....................................................................
</span><span class='line'>Finished in 10.799886 seconds.
</span><span class='line'>
</span><span class='line'>68 tests, 682 assertions, 0 failures, 0 errors
</span></code></pre></td></tr></table></div></figure>


<p>All the store tests are run as a group. This is a nice way to verify that things are working in the money-making part of the site.</p>

<p>However, I have to remember to update the task whenever I refactor or make additions. I&#8217;m thinking about symbolic links, now. Perhaps the tests could be grouped together in a directory (like test/groups/store) by making symbolic links to them in there. And, subversion deals with symlinks well&#8230;</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[WYMeditor]]></title>
    <link href="http://www.fozworks.com/blog/2006/12/18/wymeditor/"/>
    <updated>2006-12-18T00:00:00+01:00</updated>
    <id>http://www.fozworks.com/blog/2006/12/18/wymeditor</id>
    <content type="html"><![CDATA[<h2><a href="http://www.wymeditor.org/en/">wymeditor</a></h2>

<p>I&#8217;ve been looking for a great javascript/css editor for a long time. I have tried out several of them, but I was turned off by how busy and complicated they seemed. Too many icons, or not css-centric. A site with a clean CSS design can get easily bungled by badly formated content.</p>

<p>I was fortunate to meet <a href="http://www.peterkrantz.com/">Peter Krantz</a> at <a href="http://railsconf.org">Railsconf 2006</a>. Peter runs the <a href="http://www.standards-schmandards.com">Standards-Schmandards</a> website, which I have been following for years. He recently <a href="http://www.standards-schmandards.com/2006/wysiwym/">posted about</a> <a href="http://www.wymeditor.org/en/">WYSiwym</a>, a visual editor built in javascript that uses pure css to power a creative approach to the problem of web-based rich text editing. Take a minute to check out <a href="http://demo.wymeditor.org/editor/editor.htm">the demo</a>.</p>

<p>I believe this project has a bright future. Web-based rich text editing should respect the CSS rules put in place by the site&#8217;s designers. Less is more when it comes to formatting, and the &#8220;block-based&#8221; approach to formatting makes a lot of sense for most rich text web applications. I definitely plan to try this out on my next CMS project!</p>
]]></content>
  </entry>
  
</feed>
