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

  <title><![CDATA[Lapping it up with open arms]]></title>
  <link href="http://joecorcoran.github.com/atom.xml" rel="self"/>
  <link href="http://joecorcoran.github.com/"/>
  <updated>2012-01-11T17:42:09+00:00</updated>
  <id>http://joecorcoran.github.com/</id>
  <author>
    <name><![CDATA[Joe Corcoran]]></name>
    <email><![CDATA[joecorcoran@gmail.com]]></email>
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Judge: Simple JavaScript form validation for Rails]]></title>
    <link href="http://joecorcoran.github.com/2011/11/15/judge-simple-javascript-form-validation-for-rails"/>
    <updated>2011-11-15T13:17:00+00:00</updated>
    <id>http://joecorcoran.github.com/2011/11/15/judge-simple-javascript-form-validation-for-rails</id>
    <content type="html"><![CDATA[<p>Since version 1.0 has just been released and I&#8217;ve settled on what I hope is a friendly interface, I thought it was probably time I wrote a bit about <a href="https://rubygems.org/gems/judge">Judge</a>, my client side form validation gem for Rails 3.</p>

<!--more-->


<h2>Context</h2>

<p>There are some other gems out there that attempt to solve this problem, so it would be remiss of me not to mention them. <a href="https://github.com/bcardarella/client_side_validations" title="Client Side Validations gem by Brian Cardarella">Client Side Validations</a> is feature-rich and does a lot of work for you.  There&#8217;s also <a href="https://github.com/alechill/livevalidation" title="LiveValidation by Alec Hill">LiveValidation</a>, which has been around for a long time and might be a good choice for people with legacy Rails apps that need Prototype.</p>

<p>I wanted a solution that was as small as possible. This is a simple job – no need for middleware, no need for extensions to core Ruby classes.  I also wanted it to be as flexible as possible - no assumptions should be made about form markup, styles, classes and so on. I started hacking away, using the jQuery plugin pattern as a starting point for the client side code. It soon became clear that jQuery had to go. Writing Judge as a jQuery plugin was a mistake – the code I wrote ended up tightly coupled to specific event bindings, specific DOM elements etc. That kind of stuff should really be left for the user to decide.</p>

<h2>Examples</h2>

<h3>Setup</h3>

<p>Add validation to an attribute in your model.</p>

<figure class='code'><figcaption><span>app/models/thing.rb  </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">class</span> <span class="nc">Thing</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="n">validates</span> <span class="ss">:name</span><span class="p">,</span> <span class="ss">:presence</span> <span class="o">=&gt;</span> <span class="kp">true</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Use the <code>Judge::FormBuilder</code> in your view. Add <code>:validate => true</code> to the options hash of any form element that you wish to validate on the client side.</p>

<figure class='code'><figcaption><span>app/views/things/new.html.erb  </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="o">&lt;</span><span class="sx">%= form_for(@thing, :builder =</span><span class="o">&gt;</span> <span class="no">Judge</span><span class="o">::</span><span class="no">FormBuilder</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span> <span class="sx">%&gt;</span>
</span><span class='line'><span class="sx">  &lt;%= f.text_field :name, :validate =&gt;</span> <span class="kp">true</span> <span class="sx">%&gt;</span>
</span><span class='line'><span class="sx">&lt;% end %&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Quick validation</h3>

<p>The <code>judge</code> object has a static <code>validate</code> method for immediate validation:</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='javascript'><span class='line'><span class="nx">judge</span><span class="p">.</span><span class="nx">validate</span><span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">&#39;thing_name&#39;</span><span class="p">));</span>
</span><span class='line'>  <span class="c1">// =&gt; { valid: false, element: HTMLInputElement..., messages: { blank: &quot;Can&#39;t be blank&quot; } }</span>
</span></code></pre></td></tr></table></div></figure>


<p>The same method used within a <code>keyup</code> event handler (requires jQuery):</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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;input#thing_name&#39;</span><span class="p">).</span><span class="nx">keyup</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">input</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">judge</span><span class="p">.</span><span class="nx">validate</span><span class="p">(</span><span class="nx">input</span><span class="p">));</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that we are not passing a jQuery object into the <code>validate</code> method here, but the DOM element itself. The <a href="http://api.jquery.com/get/">jQuery <strong>get</strong> method</a> will often be of use when validating elements that have been wrapped by jQuery.</p>

<h3>Validation with watchers</h3>

<p>Most of the features of Judge are based around watchers &#8211; JavaScript objects that hold information about the form elements to which they point.</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>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="c1">// instantiate watcher</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">nameInput</span>   <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">&#39;thing_name&#39;</span><span class="p">),</span>
</span><span class='line'>    <span class="nx">nameWatcher</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">judge</span><span class="p">.</span><span class="nx">Watcher</span><span class="p">(</span><span class="nx">nameInput</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// some time later:</span>
</span><span class='line'><span class="nx">nameWatcher</span><span class="p">.</span><span class="nx">validate</span><span class="p">();</span>
</span><span class='line'>  <span class="c1">// =&gt; { valid: false, element: HTMLInputElement..., messages: { blank: &quot;Can&#39;t be blank&quot; } }</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Validating stored elements</h3>

<p>Most of the time, validating immediately on <code>keyup</code> or <code>change</code> will be enough to achieve the intended user experience.  But there are times when storing a reference to a form element, or a group of form elements, can be useful. For example, validating elements conditionally based on user triggers. This is where the Judge <code>store</code> comes in.</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>
<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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="c1">// store some text input elements</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">textInputs</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">querySelectorAll</span><span class="p">(</span><span class="s1">&#39;form.thing-form input[type=text]&#39;</span><span class="p">);</span>
</span><span class='line'><span class="nx">judge</span><span class="p">.</span><span class="nx">store</span><span class="p">.</span><span class="nx">save</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="nx">textInputs</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// retrieve watchers for your stored elements</span>
</span><span class='line'><span class="nx">judge</span><span class="p">.</span><span class="nx">store</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">);</span>
</span><span class='line'>  <span class="c1">// =&gt; [ { element: HTMLInputElement... }, { element: HTMLInputElement... }, ... ]</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// retrieve your stored elements</span>
</span><span class='line'><span class="nx">judge</span><span class="p">.</span><span class="nx">store</span><span class="p">.</span><span class="nx">getDOM</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">);</span>
</span><span class='line'>  <span class="c1">// =&gt; [ HTMLInputElement, HTMLInputElement, ... ]</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// validate all elements stored against key &#39;foo&#39;</span>
</span><span class='line'><span class="nx">judge</span><span class="p">.</span><span class="nx">store</span><span class="p">.</span><span class="nx">validate</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">);</span>
</span><span class='line'>  <span class="c1">// =&gt; [ { valid: true, ... }, { valid: false, ... }, ... ]</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Use it</h2>

<p>Go ahead! Feedback is all kinds of welcome.</p>

<figure class='code'><figcaption><span>Gemfile </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=''><span class='line'>gem "judge", "~> 1.0.0"</span></code></pre></td></tr></table></div></figure>


<ul>
<li><a href="https://rubygems.org/gems/judge">Judge on RubyGems.org</a></li>
<li><a href="http://judge.joecorcoran.co.uk">Judge documentation</a></li>
<li><a href="http://github.com/joecorcoran/judge">Judge source on GitHub</a></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Testing bound jQuery events]]></title>
    <link href="http://joecorcoran.github.com/2010/11/12/testing-bound-jquery-events"/>
    <updated>2010-11-12T21:29:00+00:00</updated>
    <id>http://joecorcoran.github.com/2010/11/12/testing-bound-jquery-events</id>
    <content type="html"><![CDATA[<p>I&#8217;ve been using <a href="http://pivotal.github.com/jasmine/">Jasmine</a>, along with velesin&#8217;s <a href="https://github.com/velesin/jasmine-jquery">jasmine-jquery</a>, to test all of my JavaScript work recently.  jasmine-jquery provides some really handy matchers, but one thing that&#8217;s missing is the ability to test whether an element has a bound event.</p>

<!--more-->


<p>I&#8217;ve come up with the following matcher to do just that:</p>

<div><script src='https://gist.github.com/674339.js?file=toHaveEvent.js'></script>
<noscript><pre><code>toHaveEvent: function(eventType){
  return (this.actual.data('events')) &amp;&amp; (typeof this.actual.data('events')[eventType] == 'object');
}</code></pre></noscript></div>


<p>Use it like so: <code>expect($('input#foo')).toHaveEvent('keyup');</code></p>

<p>I&#8217;d be interested to hear of any thoughts or improvements.  Likewise, I&#8217;ve started work on testing for events attached using <code>$.fn.live()</code>.  These are handled a bit differently, since live events aren&#8217;t actually bound to the selected element.  Instead they sit there, bound to the <code>document</code>, listening for the event to bubble up the <acronym title="Document Object Model">DOM</acronym> tree.</p>

<div><script src='https://gist.github.com/674339.js?file=toHaveLive.js'></script>
<noscript><pre><code>toHaveLive: function(eventType) {
  var hasLive, actual = this.actual;
  $.each($(document).data('events')['live'], function(i, item) {          
    hasLive = ((item.selector == actual.selector) &amp;&amp; (item.origType == eventType));
    if (hasLive) return false;          
  });        
  return hasLive;
}</code></pre></noscript></div>


<p>So: <code>expect($('input#bar')).toHaveLive('focus');</code></p>

<p>Again, any improvements or thoughts are welcome.</p>

<p>You can make these matchers available by using <a href="http://pivotal.github.com/jasmine/matchers.html" title="Jasmine matchers"><code>this.addMatchers</code></a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[When Sammy Met Jasmine]]></title>
    <link href="http://joecorcoran.github.com/2010/09/01/when-sammy-met-jasmine"/>
    <updated>2010-09-01T21:37:00+01:00</updated>
    <id>http://joecorcoran.github.com/2010/09/01/when-sammy-met-jasmine</id>
    <content type="html"><![CDATA[<h2>Testing Sammy JavaScript apps with Pivotal Labs&#8217; Jasmine BDD framework</h2>

<p>Aaron Quint&#8217;s route-driven JavaScript framework <a href="http://github.com/quirkey/sammy" title="Sammy framework on GitHub">Sammy</a>, ships with <a href="http://github.com/jquery/qunit/" title="QUnit jQuery testing framework on GitHub">QUnit</a> tests. This is an obvious choice, since QUnit is jQuery&#8217;s test framework and Sammy depends on jQuery. I, however, am not the biggest fan of QUnit. It works really well, but I find the syntax a little unwieldy.  On a recent project, I thought I&#8217;d try out <a href="http://github.com/pivotal/jasmine">Jasmine – a library-independent JavaScript <acronym title="Behaviour Driven Development">BDD</acronym> framework</a> with a more readable syntax.</p>

<p>In this post I&#8217;ll run through some typical Sammy scenarios and show you how I&#8217;ve written the corresponding Jasmine tests.  For reference, I&#8217;m using Jasmine 0.11.1 and Sammy 0.5.4.</p>

<!--more-->


<h2>Setting up Sammy</h2>

<p>Let&#8217;s start with a fairly simple Sammy app with some common features.</p>

<div><script src='https://gist.github.com/560735.js?file=app.js'></script>
<noscript><pre><code>var app = $.sammy(function() {
                           
    var context = this;
                           
    this.get('#/', function() {
        this.trigger('myEvent', { h1: 'My app',  text: 'world' });
    });
    
    this.bind('myEvent', function(e, opts) {
        $('h1').html(opts.h1);
        $('p#text').html(context.url.build('hello', opts.text));
    });
    
    this.url = {
        build: function(key, val) {
            return '#/'+key+'/'+val;
        }
    };
    
});

$(function() {
    app.run('#/');         
});</code></pre></noscript></div>


<p>At the top of the app, the <code>context</code> variable is assigned, providing easy and unambiguous access to the <code>Sammy.Application</code> instance itself, for use elsewhere.  A GET route is declared, which simply triggers an event named <code>myEvent</code>.  The second argument to <code>trigger</code> passes an object to the event:</p>

<div><script src='https://gist.github.com/560735.js?file=app_pt1.js'></script>
<noscript><pre><code>var context = this;
                           
this.get('#/', function() {
    this.trigger('myEvent', { h1: 'My app',  text: 'world' });
});</code></pre></noscript></div>


<p>Here is the event that was triggered in the GET route.  Bound events accept a custom object as their second argument, which is used in this example to pass in some arbitrary values:</p>

<div><script src='https://gist.github.com/560735.js?file=app_pt2.js'></script>
<noscript><pre><code>this.bind('myEvent', function(e, opts) {
    $('h1').html(opts.h1);
    $('p#text').html(context.url.build('hello', opts.text));
});</code></pre></noscript></div>


<p>Here, a (very) rough example of a modular design pattern is established for any non-event driven parts of the app.  I like to group functions by similarity of purpose.</p>

<div><script src='https://gist.github.com/560735.js?file=app_pt3.js'></script>
<noscript><pre><code>this.url = {
    build: function(key, val) {
        return '#/'+key+'/'+val;
    }
};</code></pre></noscript></div>


<h3>An aside: Can I read all this shit easily?</h3>

<p>I&#8217;ve started to ask myself this question frequently.  If the answer is no, it&#8217;s time to abstract.</p>

<p>Particularly where jQuery is involved, I often find code that looks similar to this:</p>

<div><script src='https://gist.github.com/563595.js?file=chain_soup.js'></script>
<noscript><pre><code>function DoTooMuchStuff(num) {
    $('p#mypar_'+num).slideUp(400).remove();
    $('h3#myheading_'+num).html('No records found');
    context.trigger('updateStoredRecords', { remove: num });
    var delimiter = ($('ul#removed &gt; li').size() &gt; 0) ? ', ' : '';
    $('ul#removed').append($('&lt;li/&gt;').html(delimiter+GetName(num)));
};
</code></pre></noscript></div>


<p>It only takes a few lines of jQuery, chained to the hilt, to achieve a lot.  This is seen as an advantage, and rightly so for small pieces of UI scripting, but it&#8217;s a huge pain when testing larger applications since it leads to bloated specs.  Imagine trying to test the above example – a long list of calls needs a long list of assertions.  But testing needn&#8217;t be a chore!  Whichever way a JavaScript project is structured, from fully modular and enclosed to fly-open-for-everyone-to-see, keeping functions succinct and limited in purpose makes for much easier testing.  Readable code == readable tests == clear statement of intentions to leave for future developers == painless development.</p>

<h2>Testing with Jasmine</h2>

<p>Here&#8217;s an initial attempt at testing the above Sammy app using Jasmine.  The specs are within <code>describe</code> blocks.  These aren&#8217;t strictly necessary but nesting them helps to describe your tests in relation to the structure of your app.  See how the nesting follows the namespacing of the <code>url.build</code> function, for example:</p>

<div><script src='https://gist.github.com/560735.js?file=appSpecWithout.js'></script>
<noscript><pre><code>describe('myEvent', function() {
                             
    beforeEach(function() {
        $('body').append('&lt;div id=&quot;jasmine&quot;&gt;&lt;h1&gt;Heading&lt;/h1&gt;&lt;p id=&quot;text&quot;&gt;Replace me&lt;/p&gt;&lt;/div&gt;');
        opts = {
            h1: 'My app',
            text: 'world'
        };
        app.trigger('myEvent', opts);
    });

    afterEach(function() {
        $('div#jasmine').empty().remove();
    });
    
    it('should set h1', function() {
        expect($('h1').html()).toMatch(/My app/);
    });
    
    it('should show something in place of default text', function() {
        expect($('p#text').html()).not.toMatch(/Replace me/);
        expect($('p#text').html().length).toBeGreaterThan(0);
    });
    
});

describe('url', function() {
    
    describe('build', function() {
        
        it('should return properly formed hash-based url', function() {
            var url = app.url.build('foo', 'bar');
            expect(url).toEqual('#/foo/bar');
        });
        
    });
    
});</code></pre></noscript></div>


<p>Since the app involves <acronym title="Document Object Model">DOM</acronym> insertion, it&#8217;s clear that some kind of setup and teardown of temporary DOM elements is required.  Using Jasmine&#8217;s <code>beforeEach</code> method, the relevant elements are inserted into the DOM before each spec is run. Since each spec within this <code>describe</code> block refers to the same event, the event trigger is here as well, to avoid repetition.  The DOM elements are cleaned up using <code>afterEach</code>.</p>

<div><script src='https://gist.github.com/560735.js?file=appSpecWithout_wrap.js'></script>
<noscript><pre><code>beforeEach(function() {
    $('body').append('&lt;div id=&quot;jasmine&quot;&gt;&lt;h1&gt;Heading&lt;/h1&gt;&lt;p id=&quot;text&quot;&gt;Replace me&lt;/p&gt;&lt;/div&gt;');
    opts = {
        h1: 'My app',
        text: 'world'
    };
    app.trigger('myEvent', opts);
});

afterEach(function() {
    $('div#jasmine').empty().remove();
});</code></pre></noscript></div>


<p>Specs are enclosed in the <code>it</code> blocks and assertions/expectations are made using <code>expect</code>.  The syntax, when read out loud, sounds pretty close to how I&#8217;d describe my tests.  &#8220;It <em>should etc.</em>&#8221;; &#8220;Expect <em>actual</em> to match <em>expression</em>&#8221;.  <a href="http://pivotal.github.com/jasmine/jsdoc/symbols/jasmine.Matchers.html">Jasmine has various matcher methods</a> such as <code>toMatch</code>.</p>

<div><script src='https://gist.github.com/560735.js?file=appSpecWithout_spec1.js'></script>
<noscript><pre><code>it('should set h1', function() {
    expect($('h1').html()).toMatch(/My app/);
});</code></pre></noscript></div>


<h3>Befriending the DOM</h3>

<p>There&#8217;s something a little clunky about certain aspects of the above tests.  In particular the DOM-related areas look a little hacky.  It would be unfair to call this a fault of the testing framework – DOM manipulation isn&#8217;t even on the Jasmine agenda.  So this is where <a href="http://github.com/velesin/jasmine-jquery">velesin&#8217;s jasmine-jquery extensions</a> step in.</p>

<p>jasmine-jquery allows for various improvements on the original tests:</p>

<div><script src='https://gist.github.com/560735.js?file=appSpec.js'></script>
<noscript><pre><code>describe('myEvent', function() {
                             
    beforeEach(function() {
        loadFixtures('fixtures/test.html');
        opts = {
            h1: 'My app',
            text: 'world'
        };
        app.trigger('myEvent', opts);
    });
    
    it('should set h1', function() {
        expect($('h1')).toHaveText(opts.h1);
    });
    
    it('should show something in place of default text', function() {
        expect($('p#text')).not.toHaveText('Replace me');
        expect($('p#text')).not.toBeEmpty();
    });
    
});

describe('url', function() {
    
    describe('build', function() {
        
        it('should return properly formed hash-based url', function() {
            var url = app.url.build('foo', 'bar');
            expect(url).toEqual('#/foo/bar');
        });
        
    });
    
});</code></pre></noscript></div>


<p>Firstly, the manual setup and teardown is no longer required, thanks to <code>loadFixtures</code>, which loads in the necessary DOM elements from an external file, inserts them into a wrapper div inside the document body, and cleans up automatically after each spec is run.  Big win.  People who are really smart might even try to load fixtures in from the same files as partials to avoid duplication (I have a feeling this could all come together beautifully with <a href="http://code.quirkey.com/sammy/docs/api.html#Sammy.Meld">Sammy&#8217;s new Meld feature</a>…).  An alternative is to use <code>setFixtures</code>, which accepts either an HTML string or a jQuery element as its only argument rather than loading from an external file.</p>

<div><script src='https://gist.github.com/560735.js?file=appSpec_fixture.js'></script>
<noscript><pre><code>beforeEach(function() {
    loadFixtures('fixtures/test.html');
    opts = {
        h1: 'My app',
        text: 'world'
    };
    app.trigger('myEvent', opts);
});</code></pre></noscript></div>


<p>jasmine-jquery also provides a set of <a href="http://github.com/velesin/jasmine-jquery#readme">custom matchers</a>.  Here, for example, there&#8217;s no need for the workaround of checking the innerHtml of an element against a regular expression, when <code>toHaveText</code> does the trick.</p>

<div><script src='https://gist.github.com/560735.js?file=appSpec_matchers.js'></script>
<noscript><pre><code>it('should set h1', function() {
    expect($('h1')).toHaveText(opts.h1);
});
    
it('should show something in place of default text', function() {
    expect($('p#text')).not.toHaveText('Replace me');
    expect($('p#text')).not.toBeEmpty();
});</code></pre></noscript></div>


<h2>Conclusions</h2>

<p>The Jasmine documentation could really do with a spruce up – especially in the less immediately obvious areas like spying, mocking and stubbing.  It&#8217;s possible to bundle through and get some pretty nifty async tests running (blog post on this topic pending) but clearer instructions would be a big help.  I&#8217;m sure better docs are in the pipeline with version 1.0.0 proper due to be released soon.</p>

<p>I find testing with Jasmine a more pleasant experience than any I&#8217;ve had with another JavaScript testing framework.  The only thing that has come close for me is <a href="http://visionmedia.github.com/jspec/" title="JSpec testing framework on GitHub">JSpec</a>, which has very similar syntax to Jasmine if you eschew the custom Rubyesque grammar.</p>

<p>Provided you structure your Sammy application in a sensible way, it&#8217;s easily testable with Jasmine.  I&#8217;d like a way of testing the Sammy routes themselves, something akin to functional testing as apposed to unit testing.  But I&#8217;m happy enough with relying on the Sammy internal tests and keeping the routes as simple trigger calls.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Announcing Tweet Fighter]]></title>
    <link href="http://joecorcoran.github.com/2010/02/01/announcing-tweet-fighter"/>
    <updated>2010-02-01T21:46:00+00:00</updated>
    <id>http://joecorcoran.github.com/2010/02/01/announcing-tweet-fighter</id>
    <content type="html"><![CDATA[<p>Firstly: allow me to bash the bible for a few lines.  Over the past year or so I&#8217;ve become enamoured with <a href="http://www.sinatrarb.com/" title="Sinatra, a Ruby HTTP framework">Sinatra, the Ruby microframework</a>.  I&#8217;ve toyed with it, pushed it around, made it do things it didn&#8217;t want to do, locked it out in the cold and like a terminally happy pet, it still keeps coming back for more.  Sinatra&#8217;s versatility is a product of its simplicity – it does its job well and the rest is up to you.  Anyway, enough proselytising… on to <a href="http://tweetfight.me">Tweet Fighter</a>.</p>

<!--more-->


<h2>Concept</h2>

<p>You submit two <a href="http://twitter.com">Twitter</a> users, who are then queried a handful of times through the Twitter <acronym title="Application Programming Interface">API</acronym> and a &#8220;fight&#8221; of sorts is created between the two users.  They are attributed points based on various &#8220;fight&#8221;-worthy criteria: bad language; negative language (words like &#8220;kill&#8221; or &#8220;hate&#8221;, for instance); previous Tweet Fighter fight record; and – this being Twitter – number of followers and number of tweets to date.  The (fuzzy) thinking behind this is that, along with the fighting talk, the mouthiest users with the most backup from their group would have an advantage in a fight situation.</p>

<p>It&#8217;s pretty silly, I know.  I considered hacking away at some kind of worthy cause, but I&#8217;m a big kid.  Maybe I&#8217;ll get around to creating a parking metre/carbon emissions cross-referencing Google Maps mashup one day when I&#8217;m older and crabbier.  Hopefully nobody reading this will assume that I&#8217;m advocating random violence by making a fight-based application, but we&#8217;ll see.</p>

<p>I lifted the visuals from <a href="http://en.wikipedia.org/wiki/Street_Fighter_II" title="Street Fighter II on Wikipedia">Street Fighter II</a>, which was one of my favourite games as a kid (please don&#8217;t cease-and-desist me, <a href="http://en.wikipedia.org/wiki/Capcom" title="Capcom on Wikipedia">Capcom</a>.  It&#8217;s a tribute).  I seem to remember playing as <a href="http://en.wikipedia.org/wiki/Vega_%28Street_Fighter%29" title="Vega, the Street Fighter character, on Wikipedia">Vega</a> a lot, despite him not being very good.</p>

<h2>Inside</h2>

<p>As mentioned before, Tweet Fighter is written in Ruby, on Sinatra.  My <acronym title="Object Relational Mapping">ORM</acronym> buddy of choice right now is <a href="http://sequel.rubyforge.org/" title="Sequel at Rubyforge">Sequel</a>.  I like the intuitive, chainable query syntax.  As such, I was happy to see the recent announcement regarding <a href="http://m.onkey.org/2010/1/22/active-record-query-interface" title="Pratik's post about the future of the Active Record's query interface">plans to make Active Record more, well… Sequel-esque</a>.</p>

<p>I&#8217;m also using <a href="http://github.com/jnunemaker/httparty" title="httparty source code at GitHub">httparty</a> to allow users to tweet directly from their fight pages – basic authorisation is easy with httparty and it felt like a much better fit than overkilling the project with <a href="http://oauth.net/" title="OAuth, the open authorisation protocol">OAuth</a>.</p>

<h2>Issues</h2>

<p>Overall, the Twitter API is easy to work with.  It&#8217;s a little weird having the search API split away from the remainder of it, but it&#8217;s no big deal.  One major limitation of the Twitter <acronym title="Application Programming Interface">API</acronym> – the unsearchable nature of any status updates over a certain age – actually turned out to be a blessing.  It means that fights involving the same Twitter users will be different from week to week (or even day to day), depending on how sweary/misanthropic the users have been feeling recently.  Sure, follower counts will remain roughly the same for weeks on end, but a couple of good old blue-tinted rants about your local transport provider will have you beating opponents in no time.</p>

<h2>Future</h2>

<p>A leaderboard is in order.  Anything else I think of <em>might</em> just get done too.  Any suggestions/bugs/breakages, please let me know.</p>

<p><a href="http://tweetfight.me" title="Tweet Fighter">I want to go to there</a></p>


<h2>Update, 12 Feb 2010</h2>

<p>I&#8217;ve just read that <a href="http://groups.google.com/group/twitter-development-talk/browse_thread/thread/c2c4963061422f28?pli=1" title="Twitter Development Talk on Google Groups">Twitter is phasing out basic authentication</a> much sooner than I&#8217;d anticipated.  Will have to get cracking on OAuth!</p>

<h2>Update, 18 June 2010</h2>

<p>I stripped out the basic auth tweet form and replaced it with a TweetBox from Twitter&#8217;s <a href="http://dev.twitter.com/anywhere" title="@Anywhere">@Anywhere</a> platform.  Pro: Trading a controller action and two gems for a few lines of JavaScript isn&#8217;t bad at all;  Con: non-JS users miss out again.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Practical uses for Sammy]]></title>
    <link href="http://joecorcoran.github.com/2009/07/02/practical-uses-for-sammy"/>
    <updated>2009-07-02T21:52:00+01:00</updated>
    <id>http://joecorcoran.github.com/2009/07/02/practical-uses-for-sammy</id>
    <content type="html"><![CDATA[<p>I&#8217;ve recently been looking into a new Javascript framework named <a href="http://code.quirkey.com/sammy/" title="Sammy homepage at Quirkey.com">Sammy</a>, written by the most excellent <a href="http://www.quirkey.com/blog/" title="Aaron Quint's blog">Aaron Quint</a>.  It was built on top of <a href="http://jquery.com/" title="jQuery site">jQuery</a> and intended to allow developers to build one-page applications, utilising the <acronym title="Uniform Resource Locator">URL</acronym> hash (the portion of the <acronym title="Uniform Resource Locator">URL</acronym> from the # symbol onwards) usually reserved for on-page anchors to give Javascript functions lovely descriptive routes.</p>

<p>I&#8217;m looking forward to writing applications in this way (and I&#8217;m trying to think of an excuse to do so), but what Sammy also kindly allows is direct, linkable access to the inner workings of some of the more usual Javascript implementations too.</p>

<p>I&#8217;ve put a few examples together to demonstrate.</p>

<!--more-->


<h2>jQuery Accordion</h2>

<p>Pretty much every developer out there who deals with the front-end will have come across collapsing accordion-style lists.  They&#8217;re in wide use nowadays as they make pages with lots of text look a lot friendlier (for example <acronym title="Frequently Asked Questions">FAQ</acronym> pages).  Also, people like seeing stuff move around.</p>

<p>But what if you wanted to retain the accordion effect, whilst providing users/clients with direct links to the page with a particular list item expanded?</p>

<p><a href="http://playground.joecorcoran.co.uk/sammy/accordion/" title="Sammy / Accordion example">http://playground.joecorcoran.co.uk/sammy/accordion/</a></p>

<p>Click around and notice that the <acronym title="Uniform Resource Locator">URL</acronym> in the address bar is changing. If you copy one of the new <acronym title="Uniform Resource Locator">URL</acronym>s, like</p>

<p><a href="http://playground.joecorcoran.co.uk/sammy/accordion/#/section/3" title="Sammy / Accordion example link straight to expanded section">http://playground.joecorcoran.co.uk/sammy/accordion/#/section/3</a></p>

<p>into a new window, you&#8217;ll be taken to the same page with the 3rd section expanded.</p>

<p><a href="http://gist.github.com/128728" title="Sammy Accordion code Gist">Here&#8217;s my Sammy / Accordion code</a>, to demonstrate how simple it is to implement.</p>

<h2>jQuery Cycle</h2>

<p><a href="http://playground.joecorcoran.co.uk/sammy/cycle/" title="Sammy / Cycle example">http://playground.joecorcoran.co.uk/sammy/cycle/</a></p>

<p>Another way in which Javascript is used quite frequently is to present slideshows. Again, we could be using Sammy to provide direct access to a slide that would usually be buried in the cycle:</p>

<p><a href="http://playground.joecorcoran.co.uk/sammy/cycle/#/slide/3" title="Sammy / Cycle example part 2">http://playground.joecorcoran.co.uk/sammy/cycle/#/slide/3</a></p>

<p>This one was slightly harder to implement, but still achievable with a bit of hacking around. I couldn&#8217;t get the &#8216;previous&#8217; and &#8216;next&#8217; options from the Cycle plugin working nicely with Sammy without screwing the transitions up, but I&#8217;m sure it&#8217;s possible with more work.</p>

<p><a href="http://gist.github.com/130003" title="Sammy Cycle slideshow code Gist">Here&#8217;s the Sammy slideshow code</a>.</p>

<h2>Friendly form wizard</h2>

<p>This one is less common, but it&#8217;s where my imagination took me after the first two.  It&#8217;s a single page, Sammy app-powered form wizard.</p>

<p><a href="http://playground.joecorcoran.co.uk/sammy/form/" title="Sammy / form wizard example">http://playground.joecorcoran.co.uk/sammy/form/</a></p>

<p>The stages of the wizard are simply fieldsets hidden/shown appropriately.  There&#8217;s a simple validation attempt at the end which checks for empty fields and gives helpful links back to any stage that needs to be amended.  Once all fields are filled, the submit button appears and Sammy handles the post request.  Obviously, performing more complicated validation on page would become unwieldy, but handling it with <acronym title="Asynchronous Javascript and XML">AJAX</acronym> is an option.</p>

<p><a href="http://gist.github.com/134843" title="Sammy form wizard code Gist">Here&#8217;s my Sammy form code</a>.</p>

<p>I&#8217;m really impressed with Sammy and it&#8217;s definitely changed the way I think about working with Javascript for the better.  I&#8217;d certainly recommend downloading <a href="http://github.com/quirkey/sammy/tree/master" title="Sammy on GitHub">the latest version</a> (0.2.0 at time of writing) and weaving it into some of your existing Javascript – it&#8217;s a nice way to familiarise yourself with Sammy whilst adding an extra slice of accessibility to your work.</p>

<h2>Update, 4 Jan 2011</h2>

<p>Just thought I&#8217;d better say this, since this post is still getting a lot of traffic.  While the examples I wrote here are quite useful, Sammy itself has changed quite a lot in the meantime.  Please make sure you check the <a href="http://code.quirkey.com/sammy/docs/index.html">Sammy docs</a> before you give yourself a headache.</p>
]]></content>
  </entry>
  
</feed>

