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

  <title><![CDATA[Scott W. Bradley]]></title>
  <link href="http://scottwb.com/atom.xml" rel="self"/>
  <link href="http://scottwb.com/"/>
  <updated>2013-05-05T21:05:30-07:00</updated>
  <id>http://scottwb.com/</id>
  <author>
    <name><![CDATA[Scott W. Bradley]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Always-On HTTPS With Rails Behind an ELB]]></title>
    <link href="http://scottwb.com/blog/2013/02/06/always-on-https-with-rails-behind-an-elb/"/>
    <updated>2013-02-06T11:43:00-08:00</updated>
    <id>http://scottwb.com/blog/2013/02/06/always-on-https-with-rails-behind-an-elb</id>
    <content type="html"><![CDATA[<p>So you want to run your Rails site such that it always uses HTTPS, and you want all HTTP URLs to redirect to their HTTPS counterparts? Typically you use <code>config.force_ssl = true</code> in your initializer, or you use <code>force_ssl</code> in your controllers. For various reasons having to do with late-binding configuration, I have typically not been able to use the <code>config.force_ssl</code> method. This means the easiest way to force the whole site to use HTTPS has been to use <code>force_ssl</code> on the base <code>ApplicationController</code>, 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>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># app/controllers/application_controller.rb</span>
</span><span class='line'><span class="k">class</span> <span class="nc">ApplicationController</span> <span class="o">&lt;</span> <span class="no">ActionController</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="n">force_ssl</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>However&#8230;when you deploy this to Amazon EC2 behind an ELB (Elastic Load Balancer), you can run into problems.</p>

<!-- MORE -->


<h2>Problem: ELB Health Check vs Rails force_ssl</h2>

<p>Even if you have your ELB configured with your SSL certificate and you have it proxying port 443 to port 80 on your Rails app, you may still have trouble getting the ELB to accept your instance as an upstream server if it cannot get an <code>HTTP 200 OK</code> from the health check action.</p>

<p>Once you have your Rails app using a global <code>force_ssl</code>, the ELB HealthCheck will hit your server over HTTP (because you don&#8217;t actually have your Rails server setup as an SSL endpoint), and your server will return it a 301 redirect. This causes the ELB to think your instance is unhealthy and won&#8217;t proxy any requests to it.</p>

<h2>Solution: Custom HTTP-able Health Check Action</h2>

<p>I&#8217;ve found the easiest way to deal with this is to create a special action that you use for the health check, and override the <code>force_ssl</code> for that action. Unfortunately, the stock implementation of <code>ActionController::Base.force_ssl</code>, when applied globally in the <code>ApplicationController</code>, does not allow other controllers to override that setting. That means we have to tackle this in two steps.</p>

<p>First, re-implement the <code>force_ssl</code> method to allow controllers to override it:</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># app/controllers/application_controller.rb</span>
</span><span class='line'><span class="k">class</span> <span class="nc">ApplicationController</span> <span class="o">&lt;</span> <span class="no">ActionController</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">force_ssl</span><span class="p">(</span><span class="n">options</span> <span class="o">=</span> <span class="p">{})</span>
</span><span class='line'>    <span class="n">host</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="ss">:host</span><span class="p">)</span>
</span><span class='line'>    <span class="n">before_filter</span><span class="p">(</span><span class="n">options</span><span class="p">)</span> <span class="k">do</span>
</span><span class='line'>      <span class="k">if</span> <span class="o">!</span><span class="n">request</span><span class="o">.</span><span class="n">ssl?</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="no">Rails</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">development?</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="p">(</span><span class="nb">respond_to?</span><span class="p">(</span><span class="ss">:allow_http?</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="n">allow_http?</span><span class="p">)</span>
</span><span class='line'>        <span class="n">redirect_options</span> <span class="o">=</span> <span class="p">{</span><span class="ss">:protocol</span> <span class="o">=&gt;</span> <span class="s1">&#39;https://&#39;</span><span class="p">,</span> <span class="ss">:status</span> <span class="o">=&gt;</span> <span class="ss">:moved_permanently</span><span class="p">}</span>
</span><span class='line'>        <span class="n">redirect_options</span><span class="o">.</span><span class="n">merge!</span><span class="p">(</span><span class="ss">:host</span> <span class="o">=&gt;</span> <span class="n">host</span><span class="p">)</span> <span class="k">if</span> <span class="n">host</span>
</span><span class='line'>        <span class="n">redirect_options</span><span class="o">.</span><span class="n">merge!</span><span class="p">(</span><span class="ss">:params</span> <span class="o">=&gt;</span> <span class="n">request</span><span class="o">.</span><span class="n">query_parameters</span><span class="p">)</span>
</span><span class='line'>        <span class="n">redirect_to</span> <span class="n">redirect_options</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><span class='line'>  <span class="n">force_ssl</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The above is a direct copy of this method from Rails 3.2, with the additional clause: <code>&amp;&amp; !(respond_to?(:allow_http?) &amp;&amp; allow_http?)</code>. That clause allows any controller to implement an <code>allow_http?</code> method, which is executed in the context of a request&#8217;s <code>before_filter</code>. If this method exists and returns <code>true</code> for a given request, then it will be allowed to continue over HTTP without being redirected to HTTPS.</p>

<p>For the second part, we need to create an unprotected action that can be used for the health check. The easiest way to do this is with a new controller (and matching route, if necessary):</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># app/controllers/heath_check_controller.rb</span>
</span><span class='line'><span class="k">class</span> <span class="nc">HealthCheckController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">index</span>
</span><span class='line'>    <span class="n">render</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">&quot;I am alive!</span><span class="se">\n</span><span class="s2">&quot;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="kp">protected</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">allow_http?</span>
</span><span class='line'>    <span class="kp">true</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>




<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="c1"># config/routes.rb</span>
</span><span class='line'><span class="no">MyApp</span><span class="o">::</span><span class="no">Application</span><span class="o">.</span><span class="n">routes</span><span class="o">.</span><span class="n">draw</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">get</span> <span class="s2">&quot;health_check&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;health_check#index&quot;</span>
</span><span class='line'>  <span class="c1"># ...</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now, all you need to do is change your ELB Health Check to use <code>/health_check</code> instead of <code>/index.html</code>. This way the ELB will check that your Rails app is responding using HTTP (since that is the appropriate protocol between the ELB and Rails if you are using the ELB as your SSL endpoint). Your instance will register as healthy as long as your Rails app is up, and Rails will redirect all other HTTP traffic to HTTPS.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[HSTS on Rails]]></title>
    <link href="http://scottwb.com/blog/2013/02/06/hsts-on-rails/"/>
    <updated>2013-02-06T09:47:00-08:00</updated>
    <id>http://scottwb.com/blog/2013/02/06/hsts-on-rails</id>
    <content type="html"><![CDATA[<p><a href="http://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security">HTTP Strict Transport Security</a> (HSTS) is a recent specification aimed at stopping a certain type of man-in-the-middle attack known as <em>SSL Stripping</em>. By default, when a user types &#8220;example.com&#8221; into their browser, the browser prefixes that with &#8220;http://&#8221;. A man-in-the-middle attack can hijack the connection before the server redirect to HTTPS gets back to the browser, spoofing the site and potentially luring the user into providing sensitive data to the attacker.</p>

<p>You can read a nice explanation of this attack, and how HSTS helps to prevent it <a href="http://www.imperialviolet.org/2012/07/19/hope9talk.html">here</a>.</p>

<p>The <a href="http://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security">wikipedia page on HSTS</a> provides some examples on how to enable this in your web server (apache, nginx, etc). However, when running behind an ELB in Amazon Web Services, where you cannot configure this at the reverse proxy, you may wish to do this in your application.</p>

<p>Here is how to achieve that in Ruby on Rails, using a <code>before_filter</code> in your base <code>ApplicationController</code>:</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">ApplicationController</span> <span class="o">&lt;</span> <span class="no">ActionController</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="n">before_filter</span> <span class="ss">:strict_transport_security</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">strict_transport_security</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">ssl?</span>
</span><span class='line'>      <span class="n">response</span><span class="o">.</span><span class="n">headers</span><span class="o">[</span><span class="s1">&#39;Strict-Transport-Security&#39;</span><span class="o">]</span> <span class="o">=</span> <span class="s2">&quot;max-age=31536000; includeSubDomains&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></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Cohort Analysis]]></title>
    <link href="http://scottwb.com/blog/2013/01/21/cohort-analysis/"/>
    <updated>2013-01-21T13:58:00-08:00</updated>
    <id>http://scottwb.com/blog/2013/01/21/cohort-analysis</id>
    <content type="html"><![CDATA[<p>We had a pretty big traffic surge for our product, <a href="https://vera.savelovegive.com/?utm_source=scottwb&amp;utm_campaign=cohort">VERA</a>, last week after being featured on <a href="http://abcn.ws/WiH1w7">ABC World News with Diane Sawyer</a> and <a href="http://abcn.ws/WbBU43">Nightline</a>, and we continue to see some decent sustained load since then.</p>

<p>This is, of course, great news for the company, but it also sparks some interesting discussions about metrics. After a few conversations with some colleagues of mine about statistics, metrics, and analytics, I thought I&#8217;d dig up some good articles to summarize some of the key Lean Startup principles surrounding metrics and data-driven decision-making for those who haven&#8217;t had the pleasure of reading Eric Reis&#8217;s book <em>Lean Startup</em>. That turned into a writeup about what metrics we are reporting internally, and why we focus on <em>Actionable Metrics</em> over <em>Vanity Metrics</em>. I figured I&#8217;d share some of that as a blog post here as well.</p>

<p> My goal is to provide a little background on <em>Actionable Metrics</em> and how they differ from <em>Vanity Metrics</em>. Understanding this is a central theme of the book <em>Lean Startup</em>, by Eric Reis, and the writings and teachings of a number of other prominent startup/entrepreneur/lean proponents such as Steve Blank, Dave McClure, and Ash Maurya. Fred Wilson of Union Square Ventures, has been quoted saying, &#8220;one of our firm&#8217;s favorite measurements is the cohort analysis&#8221;.</p>

<!-- MORE -->


<h2>Three Links You Should Read</h2>

<h3>1. <a href="http://www.ashmaurya.com/2010/07/3-rules-to-actionable-metrics/">3 Rules To Actionable Metrics - Ash Maurya</a></h3>

<p>In this post, Ash Maurya, author of <em>Running Lean</em> and creator of <em>Lean Canvas</em> starts right out with the definitions of Actionable Metrics and Vanity Metrics. The key summary being:</p>

<blockquote><p><strong>Actionable Metric:</strong> ties specific and repeatable actions to observed results</p>

<p><strong>Vanity Metric:</strong> only serves to document the current state of the product but offers no insight into how we got here or what to do next.</p></blockquote>

<p>In the <em>Tracking Long LifeCycle Events</em> section of this post, where Maurya talks about cohort analysis, his recommendation is that the first report you implement &#8211; the canary in the coal mine &#8211; is exactly the kind of reporting we put first on our internal statistics console:</p>

<blockquote><p>The first report I recommend implementing is a “Weekly Cohort Report by Join Date”. This report functions like a canary in the coal mine and is a great alerting tool for picking up on actions that had overall positive or negative impact.</p></blockquote>

<h3>2. <a href="http://www.purlem.com/blog/2012/03/track-what-matters-with-cohort-analysis/">Track What Matters With Cohort Analysis - Martin Thomas</a></h3>

<p>This is a nice short blog post, by Martin Thomas, founder of Purlem, about using cohort analysis with a simple real-world example. Importantly, he calls out the definition of a cohort:</p>

<blockquote><p>A cohort is a group of people who share a common characteristic or experience within a defined period</p></blockquote>

<p>Note the focus on measuring &#8220;within a defined period&#8221;. As with Maurya&#8217;s post, his example is to group cohorts by signup date (exactly what we&#8217;re doing), and then track what those cohorts have as their initial experience (his is over a month, ours is currently over a 24-hour period &#8211; we really want to measure that &#8220;Day One Aha!&#8221; experience). He quotes Eric Reis on the issue of using vanity metrics:</p>

<blockquote><p>Before using cohort analysis, I was tracking the cumulative number of paying users. Eric Reis calls this vanity metrics as they give the “rosiest possible picture” of a startup’s progress, but does not track how people are actually interacting with the application.</p>

<p>At the end of the day, using cohort analysis helps you to track the numbers that matter to the progress of your company.</p></blockquote>

<h3>3. <a href="http://www.slideshare.net/njvitto/lean-startup-metrics-analytics">Lean Startup Metrics &amp; Analytics - Nocola Junior Vitto</a></h3>

<p>This is a great slide deck about metrics and analytics in a startup. It&#8217;s a bit long but I think it is worth looking at the slides and understanding them. If you don&#8217;t go through them all, at least check out what I consider to be the highlights:</p>

<ul>
<li><p><strong>Slides 4-8:</strong> The perfect picture of vanity metrics. I love that Slide 7 calls Google Analytics Realtime Overview a &#8220;drug that can kill you&#8221;. So true.</p></li>
<li><p><strong>Slides 14-15:</strong> Good definitions of actionable metrics</p></li>
<li><p><strong>Slide 28:</strong> Nice visualization of the conversion funnel</p></li>
<li><p><strong>Slide 33:</strong> Good progression of online marketing. We need to work toward getting solidly into the 3rd Generation territory.</p></li>
<li><p><strong>Slides 49-56:</strong> Good summary of metrics surrounding user acquisition. This is exactly why I&#8217;ve been so adamant about getting the people sharing links to use tracking codes properly: &#8220;Use <strong>unique urls</strong> (tracking parameters) on <strong>every url</strong> you create/give-out/pay for&#8221;</p></li>
<li><p><strong>Slides 71-77:</strong> Good overview of the &#8220;Viral Coefficient&#8221;. In our app we track this using AddThis analytics (which is one of the reasons we chose to use AddThis instead of custom Facebook integration)</p></li>
<li><p><strong>Slides 78-81:</strong> Good overview of the &#8220;Net Promoter Score&#8221; (NPS). We intend to measure this using Qualaroo (formerly KISSinsights), but we are not doing that yet.</p></li>
<li><p><strong>Slide 98:</strong> I love this slide &#8211; it applies the &#8220;OODA Loop&#8221; to the Lean Startup. <a href="http://en.wikipedia.org/wiki/OODA_loop">The OODA Loop</a> is a term uses in martial arts, combatics, military, and law-enforcement. It stands for <em>Observe, Orient, Decide, Act</em>. Then repeat. Eric Reis talks about the &#8220;Build-Measure-Learn&#8221; cycle. They really are the same thing. I love the idea of applying the OODA Loop to startups because it imbues a sense of urgency. This slide has a nice picture merging the two.</p></li>
<li><p><strong>Slides 99-100:</strong> Innovation Accounting. I&#8217;d call it a wake-up-call. &#8220;Everything you do should attempt to change a metric&#8221;. I.e.: anything we are not doing that is not specifically aimed at changing an actionable metric is something we need to stop doing.</p></li>
<li><p><strong>Slide 106:</strong> Kanban board. Looks like our Trello board!</p></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Provision and Bootstrap Windows EC2 Instances With Chef]]></title>
    <link href="http://scottwb.com/blog/2012/12/13/provision-and-bootstrap-windows-ec2-instances-with-chef/"/>
    <updated>2012-12-13T05:17:00-08:00</updated>
    <id>http://scottwb.com/blog/2012/12/13/provision-and-bootstrap-windows-ec2-instances-with-chef</id>
    <content type="html"><![CDATA[<p>This post illustrates how you can have a single script on your workstation (yes, of course, it&#8217;s a Mac) that provisions a new Windows EC2 instance and bootstraps it using Opscode Chef &#8211; written from the point of view of someone who is used to doing this all the time with ease for Linux instances using the <code>knife-ec2</code> gem. I&#8217;ll assume the reader:</p>

<ul>
<li>has a basic working knowledge of Opscode Chef</li>
<li>is using Hosted Chef</li>
<li>already has a working chef-repo workstation with <code>knife</code> configured</li>
<li>already has (or can figure out) <code>knife-ec2</code> installed and configured with AWS API credentials</li>
<li>is on their own for creating actual cookbooks and roles to configure their Windows instances</li>
</ul>


<p>This is fairly easy to do with Linux instances. Using <code>knife ec2 server create</code> and a bunch of parameters, a single command provisions a new Linux instance in EC2, waits for it to come up, connects to it over SSH using the specified key pair, installs <code>chef-client</code>, and bootstraps the node using the specified run_list. Done.</p>

<p>However, things are not so simple for Windows Server instances.</p>

<!-- MORE -->


<p>Working with Windows instances in EC2 using Chef presents a few hurdles:</p>

<ul>
<li>Windows doesn&#8217;t natively support SSH.</li>
<li>The <code>knife ec2 server create</code> command waits for the instance to accept SSH connections. There is no option to circumvent this.</li>
<li>Windows takes forever to provision.</li>
<li>Windows instances typically get a random Administrator password generated for them that takes over 15 minutes to retrieve.</li>
<li>The <code>knife-windows</code> gem provides a <code>knife bootstrap windows winrm</code> command that can bootstrap an <em>existing</em> Windows instance with Chef, but cannot provision a <em>new</em> instance.</li>
<li>The <code>knife bootstrap windows winrm</code> command requires WinRM to be configured on the instance (which it isn&#8217;t be default), requires the Administrator password of the instnace (which defaults to a random value), and requires the public IP address of the instance (which we don&#8217;t know until the instance is up).</li>
</ul>


<p>Below I&#8217;ll provide a simplified example script that demonstrates how we can hack together a few techniques to create an all-in-one solution for bringing up new Windows Server nodes in the Amazon cloud. Other than <code>knife</code> and all the other pre-requisites mentioned above, you&#8217;ll need to make sure you have the following Ruby gems installed:</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='bash'><span class='line'>% gem install knife-ec2
</span><span class='line'>% gem install knife-windows
</span></code></pre></td></tr></table></div></figure>


<h2>The Tricks</h2>

<p>The Ruby script below uses a few nasty tricks to make this all work:</p>

<p>First, we write a temporary &#8220;user data&#8221; file to pass to the EC2 API. This gets executed by the new instance when it is first provisioned. There are two tricks we need to stick into the user data file:</p>

<ul>
<li>A BAT <code>&lt;script&gt;</code> that configures WinRM (Windows Remote Management), which is what we&#8217;ll use to connect to and bootstrap the instance.</li>
<li>A <code>&lt;powershell&gt;</code> script that sets the Administrator password to a value we define. This makes it so we don&#8217;t have to wait 15+ minutes for EC2 to generate a password for us, and retrieve it manually through the GUI.</li>
</ul>


<p>Then, we use <code>knife ec2 server create</code> to provision the Windows instance to specification, passing in that user data file. This works great for provisioning the instance, but since it was not really designed for Windows and WinRM, there are two tricks we have to employ here:</p>

<ul>
<li>Execute the <code>knife</code> command in a sub-process and read its <code>STDOUT</code> until we see it output the new instances public IP address. We&#8217;ll grab that and save that for the next step.</li>
<li>This is also our cue to bail out of <code>knife ec2 server create</code>. If you were doing this manually, you&#8217;d hit <code>CTRL-C</code> here, which <code>knife</code> is saying &#8220;Waiting for sshd&#8221; (which is never going to come up). We do that by sending the sub-process a <code>SIGTERM</code> signal.</li>
</ul>


<p>Now, we can&#8217;t just move on to bootstrapping the node, because it is still booting up, and WinRM may not be configured yet. The trick here is to create a TCP socket to the WinRM port, using the IP address we aquired in the previous step, and wait for it to connect. If it fails to connect, try again until it does. By the time this succeeds, we know WinRM is up and accepting connections. However, we don&#8217;t know if the rest of the system is ready. Moving on to the next bootstrapping step immediately will run into intermittent errors. I&#8217;ve seen this manifest as an authentication erorr, presumably because we tried to bootstrap over WinRM before the PowerShell script set the password. There may be other mysteries of the Windows universe lurking here as well. My solution: sleep for two minutes. Lame, I know&#8230;but so far it is the only thing that has reliably worked.</p>

<p>Finally, we can bootstrap the new running Windows instance with the <code>knife bootstrap windows winrm</code> command, using the IP address we acquired, the password we specified in the user data, and the other <code>knife</code> params we want to use such as the run_list and environment.</p>

<h2>The Script</h2>

<p>Here is a stripped down version of this script demonstrating all these tricks. As you can see, all the custom configuration is hard-coded in constants at the top of the script. You would obviously fill in your own information however you like &#8211; via command-line params, interactive prompts, config files, etc.</p>

<p>Big thanks to my colleauge <a href="https://twitter.com/jgroh9">Jeremy Groh</a> who paired through this with me and did the bulk of the heavy lifting on the Windows side, especially with the WinRM and password-reset parts.</p>

<figure class='code'><figcaption><span>bootstrap-windows.rb </span><a href='https://gist.github.com/4276748'>View Gist</a></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>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
<span class='line-number'>81</span>
<span class='line-number'>82</span>
<span class='line-number'>83</span>
<span class='line-number'>84</span>
<span class='line-number'>85</span>
<span class='line-number'>86</span>
<span class='line-number'>87</span>
<span class='line-number'>88</span>
<span class='line-number'>89</span>
<span class='line-number'>90</span>
<span class='line-number'>91</span>
<span class='line-number'>92</span>
<span class='line-number'>93</span>
<span class='line-number'>94</span>
<span class='line-number'>95</span>
<span class='line-number'>96</span>
<span class='line-number'>97</span>
<span class='line-number'>98</span>
<span class='line-number'>99</span>
<span class='line-number'>100</span>
<span class='line-number'>101</span>
<span class='line-number'>102</span>
<span class='line-number'>103</span>
<span class='line-number'>104</span>
<span class='line-number'>105</span>
<span class='line-number'>106</span>
<span class='line-number'>107</span>
<span class='line-number'>108</span>
<span class='line-number'>109</span>
<span class='line-number'>110</span>
<span class='line-number'>111</span>
<span class='line-number'>112</span>
<span class='line-number'>113</span>
<span class='line-number'>114</span>
<span class='line-number'>115</span>
<span class='line-number'>116</span>
<span class='line-number'>117</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">#!/usr/bin/env/ruby</span>
</span><span class='line'>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;socket&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># AWS API Credentials</span>
</span><span class='line'><span class="no">AWS_ACCESS_KEY_ID</span>     <span class="o">=</span> <span class="s2">&quot;your-aws-access-key-id&quot;</span>
</span><span class='line'><span class="no">AWS_SECRET_ACCESS_KEY</span> <span class="o">=</span> <span class="s2">&quot;your-aws-secret-access-key&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># Node details</span>
</span><span class='line'><span class="no">NODE_NAME</span>         <span class="o">=</span> <span class="s2">&quot;webserver-01.example.com&quot;</span>
</span><span class='line'><span class="no">CHEF_ENVIRONMENT</span>  <span class="o">=</span> <span class="s2">&quot;production&quot;</span>
</span><span class='line'><span class="no">INSTANCE_SIZE</span>     <span class="o">=</span> <span class="s2">&quot;m1.large&quot;</span>
</span><span class='line'><span class="no">EBS_ROOT_VOL_SIZE</span> <span class="o">=</span> <span class="mi">70</span>   <span class="c1"># in GB</span>
</span><span class='line'><span class="no">REGION</span>            <span class="o">=</span> <span class="s2">&quot;us-west-2&quot;</span>
</span><span class='line'><span class="no">AVAILABILITY_ZONE</span> <span class="o">=</span> <span class="s2">&quot;us-west-2b&quot;</span>
</span><span class='line'><span class="no">AMI_NAME</span>          <span class="o">=</span> <span class="s2">&quot;ami-46c54c76&quot;</span>
</span><span class='line'><span class="no">SECURITY_GROUP</span>    <span class="o">=</span> <span class="s2">&quot;Web Servers&quot;</span>
</span><span class='line'><span class="no">RUN_LIST</span>          <span class="o">=</span> <span class="s2">&quot;role[base],role[iis]&quot;</span>
</span><span class='line'><span class="no">USER_DATA_FILE</span>    <span class="o">=</span> <span class="s2">&quot;/tmp/userdata.txt&quot;</span>
</span><span class='line'><span class="no">USERNAME</span>          <span class="o">=</span> <span class="s2">&quot;Administrator&quot;</span>
</span><span class='line'><span class="no">PASSWORD</span>          <span class="o">=</span> <span class="s2">&quot;YourAdminPassword&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># Write user data file that sets up WinRM and sets the Administrator password.</span>
</span><span class='line'><span class="no">File</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="no">USER_DATA_FILE</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span>
</span><span class='line'>  <span class="n">f</span><span class="o">.</span><span class="n">write</span> <span class="o">&lt;&lt;</span><span class="no">EOT</span>
</span><span class='line'><span class="sh">&lt;script&gt;</span>
</span><span class='line'><span class="sh">winrm quickconfig -q &amp; winrm set winrm/config/winrs @{MaxMemoryPerShellMB=&quot;300&quot;} &amp; winrm set winrm/config @{MaxTimeoutms=&quot;1800000&quot;} &amp; winrm set winrm/config/service @{AllowUnencrypted=&quot;true&quot;} &amp; winrm set winrm/config/service/auth @{Basic=&quot;true&quot;}</span>
</span><span class='line'><span class="sh">&lt;/script&gt;</span>
</span><span class='line'><span class="sh">&lt;powershell&gt;</span>
</span><span class='line'><span class="sh">$admin = [adsi](&quot;WinNT://./administrator, user&quot;)</span>
</span><span class='line'><span class="sh">$admin.psbase.invoke(&quot;SetPassword&quot;, &quot;#{PASSWORD}&quot;)</span>
</span><span class='line'><span class="sh">&lt;/powershell&gt;</span>
</span><span class='line'><span class="no">EOT</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># Define the command to provision the instance</span>
</span><span class='line'><span class="n">provision_cmd</span> <span class="o">=</span> <span class="o">[</span>
</span><span class='line'>  <span class="s2">&quot;knife ec2 server create&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--aws-access-key-id </span><span class="si">#{</span><span class="no">AWS_ACCESS_KEY_ID</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--aws-secret-access-key </span><span class="si">#{</span><span class="no">AWS_SECRET_ACCESS_KEY</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--tags &#39;Name=</span><span class="si">#{</span><span class="no">NODE_NAME</span><span class="si">}</span><span class="s2">&#39;&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--environment &#39;</span><span class="si">#{</span><span class="no">CHEF_ENVIRONMENT</span><span class="si">}</span><span class="s2">&#39;&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--flavor </span><span class="si">#{</span><span class="no">INSTANCE_SIZE</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--ebs-size </span><span class="si">#{</span><span class="no">EBS_ROOT_VOL_SIZE</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--region </span><span class="si">#{</span><span class="no">REGION</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--availability_zone </span><span class="si">#{</span><span class="no">AVAILABILITY_ZONE</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--image </span><span class="si">#{</span><span class="no">AMI_NAME</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--groups &#39;</span><span class="si">#{</span><span class="no">SECURITY_GROUP</span><span class="si">}</span><span class="s2">&#39;&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--user-data </span><span class="si">#{</span><span class="no">USER_DATA_FILE</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--verbose&quot;</span>
</span><span class='line'><span class="o">].</span><span class="n">join</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># Run `knife ec2 server create` to provision the new instance and</span>
</span><span class='line'><span class="c1"># read the output until we know it&#39;s public IP address. At that point,</span>
</span><span class='line'><span class="c1"># knife is going to wait until the instance responds on the SSH port. Of</span>
</span><span class='line'><span class="c1"># course, being Windows, this will never happen, so we need to go ahead and</span>
</span><span class='line'><span class="c1"># kill knife and then proceed with the rest of this script to wait until</span>
</span><span class='line'><span class="c1"># WinRM is up and we can bootstrap the node with Chef over WinRM.</span>
</span><span class='line'><span class="n">ip_addr</span> <span class="o">=</span> <span class="kp">nil</span>
</span><span class='line'><span class="no">IO</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="n">provision_cmd</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">pipe</span><span class="o">|</span>
</span><span class='line'>  <span class="k">begin</span>
</span><span class='line'>    <span class="k">while</span> <span class="n">line</span> <span class="o">=</span> <span class="n">pipe</span><span class="o">.</span><span class="n">readline</span>
</span><span class='line'>      <span class="nb">puts</span> <span class="n">line</span>
</span><span class='line'>      <span class="k">if</span> <span class="n">line</span> <span class="o">=~</span> <span class="sr">/^Public IP Address: (.*)$/</span>
</span><span class='line'>        <span class="n">ip_addr</span> <span class="o">=</span> <span class="vg">$1</span><span class="o">.</span><span class="n">strip</span>
</span><span class='line'>        <span class="no">Process</span><span class="o">.</span><span class="n">kill</span><span class="p">(</span><span class="s2">&quot;TERM&quot;</span><span class="p">,</span> <span class="n">pipe</span><span class="o">.</span><span class="n">pid</span><span class="p">)</span>
</span><span class='line'>        <span class="k">break</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="no">EOFError</span>
</span><span class='line'>    <span class="c1"># done</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">if</span> <span class="n">id_addr</span><span class="o">.</span><span class="n">nil?</span>
</span><span class='line'>  <span class="nb">puts</span> <span class="s2">&quot;ERROR: Unable to get new instance&#39;s IP address&quot;</span>
</span><span class='line'>  <span class="nb">exit</span> <span class="o">-</span><span class="mi">1</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># Now the new instance is provisioned, but we have no idea when it will</span>
</span><span class='line'><span class="c1"># be ready to go. The first thing we&#39;ll do is wait until the WinRM port</span>
</span><span class='line'><span class="c1"># responds to connections.</span>
</span><span class='line'><span class="nb">puts</span> <span class="s2">&quot;Waiting for WinRM...&quot;</span>
</span><span class='line'><span class="n">start_time</span> <span class="o">=</span> <span class="no">Time</span><span class="o">.</span><span class="n">now</span>
</span><span class='line'><span class="k">begin</span>
</span><span class='line'>  <span class="n">s</span> <span class="o">=</span> <span class="no">TCPSocket</span><span class="o">.</span><span class="n">new</span> <span class="n">ip_addr</span><span class="p">,</span> <span class="mi">5985</span>
</span><span class='line'><span class="k">rescue</span> <span class="no">Errno</span><span class="ss">:ETIMEOUT</span> <span class="o">=&gt;</span> <span class="n">e</span>
</span><span class='line'>  <span class="nb">puts</span> <span class="s2">&quot;Still waiting...&quot;</span>
</span><span class='line'>  <span class="k">retry</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="n">s</span><span class="o">.</span><span class="n">close</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># You&#39;d think we&#39;d be good to go now...but NOPE! There is still more Windows</span>
</span><span class='line'><span class="c1"># bootstrap crap going on, and we have no idea what we need to wait on. So,</span>
</span><span class='line'><span class="c1"># in a last-ditch effort to make this all work, we&#39;ve seen that 120 seconds</span>
</span><span class='line'><span class="c1"># ought to be enough...</span>
</span><span class='line'><span class="n">wait_time</span> <span class="o">=</span> <span class="mi">120</span>
</span><span class='line'><span class="k">while</span> <span class="n">wait_time</span> <span class="o">&gt;</span> <span class="mi">0</span>
</span><span class='line'>  <span class="nb">puts</span> <span class="s2">&quot;Better wait </span><span class="si">#{</span><span class="n">wait_time</span><span class="si">}</span><span class="s2"> more seconds...&quot;</span>
</span><span class='line'>  <span class="nb">sleep</span> <span class="mi">1</span>
</span><span class='line'>  <span class="n">wait_time</span> <span class="o">-=</span> <span class="mi">1</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="nb">puts</span> <span class="s2">&quot;Finally ready to try bootstrapping instance...&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># Define the command to bootstrap the already-provisioned instance with Chef</span>
</span><span class='line'><span class="n">bootstrap_cmd</span> <span class="o">=</span> <span class="o">[</span>
</span><span class='line'>  <span class="s2">&quot;knife bootstrap windows winrm </span><span class="si">#{</span><span class="n">ip_addr</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;-x </span><span class="si">#{</span><span class="no">USERNAME</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;-P &#39;</span><span class="si">#{</span><span class="no">PASSWORD</span><span class="si">}</span><span class="s2">&#39;&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--environment </span><span class="si">#{</span><span class="no">CHEF_ENVIRONMENT</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--node-name </span><span class="si">#{</span><span class="no">NODE_NAME</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--run-list </span><span class="si">#{</span><span class="no">RUN_LIST</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;--verbose&quot;</span>
</span><span class='line'><span class="o">].</span><span class="n">join</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># Now we can bootstrap the instance with Chef and the configured run list.</span>
</span><span class='line'><span class="n">status</span> <span class="o">=</span> <span class="nb">system</span><span class="p">(</span><span class="n">bootstrap_cmd</span><span class="p">)</span> <span class="p">?</span> <span class="mi">0</span> <span class="p">:</span> <span class="o">-</span><span class="mi">1</span>
</span><span class='line'><span class="nb">exit</span> <span class="n">status</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The Joys of Working From Home]]></title>
    <link href="http://scottwb.com/blog/2012/08/15/the-joys-of-working-from-home/"/>
    <updated>2012-08-15T17:21:00-07:00</updated>
    <id>http://scottwb.com/blog/2012/08/15/the-joys-of-working-from-home</id>
    <content type="html"><![CDATA[<p><a href="http://www.flickr.com/photos/scottwb69/sets/72157630674264242/detail/"><img class="left" src="http://farm9.staticflickr.com/8022/7612882390_880e5abdc8.jpg"></a></p>

<p>I work in a team that is distributed around the country, where everyone works from their home offices. Sounds great, doesn&#8217;t it? Well, it&#8217;s not for everyone. First, you have to be seriously self-motivated. Personally, I get more done at home than I ever have at an office, because I get to choose when to be distracted and <em>I&#8217;m good at staying focused</em>. For some people, choosing when to be distracted is a curse. They choose to be distracted all the time. Facebook, twitter, bathroom, kitchen, twitter, kids, etc.</p>

<p>Still, some of those home distractions can plague even the most focused of us. One of the keys I&#8217;ve found to succesfully working from home is to have a dedicated work space. I am lucky enough to have an extra room to use as an office, and the family knows that when I&#8217;m in there, &#8220;Daddy&#8217;s at work&#8221;.</p>

<p>But sometimes that&#8217;s not enough&#8230;</p>

<!-- MORE -->


<h3>Get Out</h3>

<p>Even when the house is empty, no co-workers are interrupting you, and you have a clear path ahead of you, the familiarity of the home office can be a distraction in itself. <em>Who&#8217;s that driving down my street? I should really fix that door. I wonder if there&#8217;s anything in the fridge. What time are the wife and kids coming home?</em></p>

<p>In times like these, I like to just get out and go work from somewhere else.</p>

<p>Everyone has wifi. Go to a coffee shop. Go to a pub. Go to the beach (hurray for MiFi!). Even though there is more noise and more movement, I find that sometimes just varying my surroundings can actually help me to focus. I block out the unfamiliar sounds. I expect the noise so I don&#8217;t key in on every little movement. I&#8217;ve done some of my best out-of-the-box thinking in these kinds of environemnts.</p>

<p>This actually turns into a great perk of the distrbuted team environment. I can go work from the coffee-shop-with-a-view and it makes no difference to my co-workers. I&#8217;m still on Skype and email. I&#8217;m still checking in code and answering questions.</p>

<h3>Far Far Away</h3>

<p>So, if I can work from a coffee shop overlooking Lake Washington&#8230;what&#8217;s the difference if I make it Lake Chelan? Or Miami Beach for that matter?</p>

<p>In July, I worked for 4 days from a remote part of the Olympic Peninsula. In March, to get away from the terrible Seattle weather, I worked for almost a month from South Florida. Add to that the fact that my team likes to travel to get together, and &#8220;work from home&#8221; actually becomes &#8220;work from everywhere&#8221;.</p>

<p>I rather enjoy that lifestyle. The bulk of my time I&#8217;m cranking away in my home office where I am extremely productive. But every now and then, at just the right intervals, I go somewhere else to mix it up. I&#8217;ve even started collecting <a href="http://www.flickr.com/photos/scottwb69/sets/72157630674264242/detail/">photos of all these places</a>. It makes for a fun reminder of one of the reasons why I like this job and working in a distributed team.</p>

<h3>Join Me, Won&#8217;t You?</h3>

<p>And now for the shameless plug. My team at <a href="http://www.validas.com/">Validas</a> is ready to grow. The beauty of this is that we can recruit from anywhere - no moving expenses. Just a generous budget to deck out your home/mobile setup with the latest tech goodies, paid internet and mobile communications, and away we go.</p>

<p>So, if you&#8217;re a seasoned architect/engineer, who fits our job description and wants to thrive in this work/life style, <a href="http://www.validas.com/careers/">send us your résumé today</a>. We&#8217;d love to talk to you!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Merge Git Repositories and Preseve Commit History]]></title>
    <link href="http://scottwb.com/blog/2012/07/14/merge-git-repositories-and-preseve-commit-history/"/>
    <updated>2012-07-14T11:42:00-07:00</updated>
    <id>http://scottwb.com/blog/2012/07/14/merge-git-repositories-and-preseve-commit-history</id>
    <content type="html"><![CDATA[<p>Every time I want to combine two git repositories into a new high-level repository, I have to re-figure it out. I decided this time to write it down. Maybe someone else will find this useful too.</p>

<p>Here is my scenario: I have two repositories. I want to make a new empty repository and move the other two into it as subdirectories. I also want to preserve all the commit history of the original repositories.</p>

<p>Here are the steps involved for the first repository:</p>

<ol>
<li>Clone the first source repository</li>
<li>Remove its origin ref so you dont&#8217; accidentally push any screwups</li>
<li>Make a subdir in that source repo named the same as the repo</li>
<li>Move all the top-level files into that directory</li>
<li>Commit that move.</li>
<li>Create (or clone) your desination repository.</li>
<li>Add a remote ref to it that points to the file location of your source repo clone.</li>
<li>Pull from that ref</li>
<li>Remove that remote ref.</li>
</ol>


<p>You can now delete the clone of the source repository you, so you don&#8217;t really keep those file moves around in the original source if you don&#8217;t want to.</p>

<p>Here&#8217;s what those steps might look 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>
<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>
<span class='line-number'>33</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c"># Step 1</span>
</span><span class='line'><span class="nv">$ </span>git clone SOURCE_REPO_URL
</span><span class='line'><span class="nv">$ </span><span class="nb">cd </span>source_repo_name
</span><span class='line'>
</span><span class='line'><span class="c"># Step 2</span>
</span><span class='line'><span class="nv">$ </span>git remote rm origin
</span><span class='line'>
</span><span class='line'><span class="c"># Step 3</span>
</span><span class='line'><span class="nv">$ </span>mkdir source_repo_name
</span><span class='line'>
</span><span class='line'><span class="c"># Step 4</span>
</span><span class='line'><span class="c">#</span>
</span><span class='line'><span class="c"># NOTE: You can&#39;t actually do this command verbatim. You need to</span>
</span><span class='line'><span class="c">#       git mv each top-level file/dir individually and don&#39;t forget</span>
</span><span class='line'><span class="c">#       .* files like .gitignore, but DO NOT copy the .git directory.</span>
</span><span class='line'><span class="nv">$ </span>git mv * source_repo_name/.
</span><span class='line'>
</span><span class='line'><span class="c"># Step 5</span>
</span><span class='line'><span class="nv">$ </span>git commit -m <span class="s2">&quot;Prepare source_repo_name to merge into dest_repo_name&quot;</span>
</span><span class='line'><span class="nv">$ </span><span class="nb">cd</span> ..
</span><span class='line'>
</span><span class='line'><span class="c"># Step 6</span>
</span><span class='line'><span class="nv">$ </span>git clone DST_REPO_URL
</span><span class='line'><span class="nv">$ </span><span class="nb">cd </span>dest_repo_name
</span><span class='line'>
</span><span class='line'><span class="c"># Step 7</span>
</span><span class='line'><span class="nv">$ </span>git remote add source_repo_name SRC_REPO_FILEPATH
</span><span class='line'>
</span><span class='line'><span class="c"># Step 8</span>
</span><span class='line'><span class="nv">$ </span>git pull source_repo_name master
</span><span class='line'>
</span><span class='line'><span class="c"># Step 9</span>
</span><span class='line'><span class="nv">$ </span>git remote rm source_repo_name
</span></code></pre></td></tr></table></div></figure>


<p>Now you&#8217;re done and can delete the source repository clone, and push the destination repository clone upstream. Check the <code>git log</code> to be sure.</p>

<h3>A Concrete Example</h3>

<p>Say I have two repositories on github named <code>homer</code> and <code>bart</code> and I want to combine them into a new repository called <code>simpsons</code>. Here is how that looks:</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>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c"># ...First create a new empty repository on github named &#39;simpsons&#39;...</span>
</span><span class='line'>
</span><span class='line'><span class="c"># Clone all the repos to work with</span>
</span><span class='line'><span class="nv">$ </span><span class="nb">cd</span> ~/src
</span><span class='line'><span class="nv">$ </span>git clone git@github.com:scottwb/homer.git
</span><span class='line'><span class="nv">$ </span>git clone git@github.com:scottwb/bart.git
</span><span class='line'><span class="nv">$ </span>git clone git@github.com:scottwb/simpsons.git
</span><span class='line'>
</span><span class='line'><span class="c"># Copy over homer to simpsons</span>
</span><span class='line'><span class="nv">$ </span><span class="nb">cd </span>homer
</span><span class='line'><span class="nv">$ </span>git remote rm origin
</span><span class='line'><span class="nv">$ </span>mkdir homer
</span><span class='line'><span class="nv">$ </span>git mv *.* homer/.
</span><span class='line'><span class="nv">$ </span>git commit -m <span class="s2">&quot;Prepare homer to merge into simpsons&quot;</span>
</span><span class='line'><span class="nv">$ </span><span class="nb">cd</span> ../simpsons
</span><span class='line'><span class="nv">$ </span>git remote add homer ~/src/homer
</span><span class='line'><span class="nv">$ </span>git pull homer master
</span><span class='line'><span class="nv">$ </span>git remote rm homer
</span><span class='line'><span class="nv">$ </span><span class="nb">cd</span> ..
</span><span class='line'><span class="nv">$ </span>rm -rf homer
</span><span class='line'>
</span><span class='line'><span class="c"># Copy over bart to simpsons</span>
</span><span class='line'><span class="nv">$ </span><span class="nb">cd </span>bart
</span><span class='line'><span class="nv">$ </span>git remote rm origin
</span><span class='line'><span class="nv">$ </span>mkdir bart
</span><span class='line'><span class="nv">$ </span>git mv *.* bart/.
</span><span class='line'><span class="nv">$ </span>git commit -m <span class="s2">&quot;Prepare bart to merge into simpsons&quot;</span>
</span><span class='line'><span class="nv">$ </span><span class="nb">cd</span> ../simpsons
</span><span class='line'><span class="nv">$ </span>git remote add bart ~/src/bart
</span><span class='line'><span class="nv">$ </span>git pull bart master
</span><span class='line'><span class="nv">$ </span>git remote rm bart
</span><span class='line'><span class="nv">$ </span><span class="nb">cd</span> ..
</span><span class='line'><span class="nv">$ </span>rm -rf bart
</span><span class='line'>
</span><span class='line'><span class="c"># Push simpsons back upstream to github</span>
</span><span class='line'><span class="nv">$ </span><span class="nb">cd </span>simpsons
</span><span class='line'><span class="nv">$ </span>git push
</span></code></pre></td></tr></table></div></figure>


<h3>Bonus Points: Only Moving a Sub-Directory</h3>

<p>What if you only want to move a subdirectory of the original source repository into the destination repository? All you need to do is filter out what you copy into that new sub-directory you create in step 4, and make sure everything else gets removed from that source repository. (Don&#8217;t worry - remember, you&#8217;re just working with a local working copy of that source repo, that you&#8217;re going top discard after this operation. You won&#8217;t harm anything irreversibly here.)</p>

<p>One way to peform that filtering is by using the <code>git filter-branch</code> command. For example, to only copy the <code>pranks</code> subdir from the <code>bart</code> repo, just before step 4, you would do something 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>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>git filter-branch --subdirectory-filter pranks -- --all
</span></code></pre></td></tr></table></div></figure>


<p>That dumps all the contents of the <code>pranks</code> dir into top-level dir where you can proceed to move them into your new subdir that you created in step 3.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Customize Attribute Accessors on Ripple Models]]></title>
    <link href="http://scottwb.com/blog/2012/07/12/customize-attribute-accessors-on-ripple-models/"/>
    <updated>2012-07-12T09:18:00-07:00</updated>
    <id>http://scottwb.com/blog/2012/07/12/customize-attribute-accessors-on-ripple-models</id>
    <content type="html"><![CDATA[<p>Sometimes in your Ruby on Rails models, you want to override the attribute accessors to do additional processing on the values on their way in and out of the data storage layer. Custom serialization is a common use of this.</p>

<p>For example, when using ActiveRecord for your Rails models, you can provide custom attribute accessors, say to serialize a Hash to JSON, using the <code>read_attribute</code> and <code>write_attribute</code> methods 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>
<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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># Assume a text column named &#39;stuff&#39;</span>
</span><span class='line'><span class="k">class</span> <span class="nc">User</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="k">def</span> <span class="nf">stuff</span>
</span><span class='line'>    <span class="no">JSON</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">read_attribute</span><span class="p">(</span><span class="ss">:stuff</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="nf">stuff</span><span class="o">=</span><span class="p">(</span><span class="n">new_val</span><span class="p">)</span>
</span><span class='line'>    <span class="n">write_attribute</span><span class="p">(</span><span class="ss">:stuff</span><span class="p">,</span> <span class="n">new_val</span><span class="o">.</span><span class="n">json</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>


<p>With this, you can assign a Hash to the <code>stuff</code> attribute of <code>User</code>, and when you access it via <code>User#stuff</code>, you get a Hash back. All the while, it&#8217;s read and written to and from the database as a JSON string.</p>

<h3>Doing it with Ripple on top of Riak</h3>

<p><a href="https://github.com/seancribbs/ripple/">Ripple</a> is the Ruby modeling layer for the distributed NoSQL store, <a href="http://basho.com/products/riak-overview/">Riak</a>. It tries very hard to provide a lot of the same interfaces as ActiveRecord. However, this is one of the areas it diverges: <code>Ripple::Document</code> objects do not support the <code>read_attribute</code> and <code>write_attribute</code> methods.</p>

<p>Instead, they implement the <code>[]</code> and <code>[]=</code> methods. Translating the code above to work with Ripple is pretty easy:</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">User</span>
</span><span class='line'>  <span class="kp">include</span> <span class="no">Ripple</span><span class="o">::</span><span class="no">Document</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">property</span> <span class="ss">:stuff</span><span class="p">,</span> <span class="no">Hash</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">stuff</span>
</span><span class='line'>    <span class="no">JSON</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="nb">self</span><span class="o">[</span><span class="ss">:stuff</span><span class="o">]</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="nf">stuff</span><span class="o">=</span><span class="p">(</span><span class="n">new_val</span><span class="p">)</span>
</span><span class='line'>    <span class="nb">self</span><span class="o">[</span><span class="ss">:stuff</span><span class="o">]</span> <span class="o">=</span> <span class="n">new_val</span><span class="o">.</span><span class="n">to_json</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>


<h3>Bonus Points</h3>

<p>Using this tactic, you can easily add some memoization so that your getter doesn&#8217;t need to parse the JSON text on every access. To do this, we&#8217;ll use an instance variable as a cache that we&#8217;ll invalidate in the setter, like so:</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">User</span>
</span><span class='line'>  <span class="kp">include</span> <span class="no">Ripple</span><span class="o">::</span><span class="no">Document</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">property</span> <span class="ss">:stuff</span><span class="p">,</span> <span class="no">Hash</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">stuff</span>
</span><span class='line'>    <span class="vi">@cached_stuff</span> <span class="o">||=</span> <span class="no">JSON</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="nb">self</span><span class="o">[</span><span class="ss">:stuff</span><span class="o">]</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="nf">stuff</span><span class="o">=</span><span class="p">(</span><span class="n">new_val</span><span class="p">)</span>
</span><span class='line'>    <span class="vi">@cached_stuff</span> <span class="o">=</span> <span class="kp">nil</span>
</span><span class='line'>    <span class="nb">self</span><span class="o">[</span><span class="ss">:stuff</span><span class="o">]</span> <span class="o">=</span> <span class="n">new_val</span><span class="o">.</span><span class="n">to_json</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[Show jQuery Mobile Page Loading With Text And Spinner]]></title>
    <link href="http://scottwb.com/blog/2012/06/30/show-jquery-mobile-page-loading-with-text-and-spinner/"/>
    <updated>2012-06-30T21:47:00-07:00</updated>
    <id>http://scottwb.com/blog/2012/06/30/show-jquery-mobile-page-loading-with-text-and-spinner</id>
    <content type="html"><![CDATA[<p>I like jQuery Mobile&#8217;s page loading spinner. When you transition between pages it displays a nice subtle spinner while the page loads, only if the page load takes more than a configured number of milliseconds.</p>

<p>So, I wanted to use their page-loading spinner for in-page wait times in my app. The user clicks something, I do some AJAX stuff and show them a nice spinner with a message telling them what&#8217;s going on until the results come back. jQuery Mobile has javascript functions for this called <code>$.mobile.showPageLoadingMsg</code> and <code>$.hidePageLoadingMsg</code>. The problem is, you can either show a spinner only, or a text-message only. You can&#8217;t show both a spinner <em>and</em> a text message, even though they clearly support it. Well, you <em>can</em> do that if you have the global setting <code>$.mobile.loadingMessageTextVisible = true</code>, but then you get a text with your normal page-load spinners.</p>

<!-- MORE -->


<p>I&#8217;ve created a workaround for this and included it in my <a href="https://github.com/scottwb/jquery.mobile.utils">jquery.mobile.utils</a> library. Using this you can call:</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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="c1">// Params: jqm theme swatch, and message text</span>
</span><span class='line'><span class="nx">$</span><span class="p">.</span><span class="nx">mobile</span><span class="p">.</span><span class="nx">utils</span><span class="p">.</span><span class="nx">showWaitBox</span><span class="p">(</span><span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="s2">&quot;Hang on while I do work...&quot;</span><span class="p">);</span>
</span><span class='line'><span class="c1">// ... some time later...</span>
</span><span class='line'><span class="nx">$</span><span class="p">.</span><span class="nx">mobile</span><span class="p">.</span><span class="nx">utils</span><span class="p">.</span><span class="nx">hideWaitBox</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>


<p>That will let you leave the standard textless page-loading spinenrs alone, but be able to show a nice themed wait box with a spinner while your app is doing something the user needs to wait for, that looks like this:</p>

<p><img class="center" src="http://img.skitch.com/20120701-met428ntqsxrest228pxgrrxjs.png"></p>

<p>To use these methods, download the JavaScript or CoffeeScript version of the jquery.mobile.utils library from the <a href="https://github.com/scottwb/jquery.mobile.utils">project&#8217;s github page</a> and include it in your jQuery Mobile page after <code>jquery.mobile-1.1.0.js</code> is loaded.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Compile A Single CoffeeScript File From Your Rails Project]]></title>
    <link href="http://scottwb.com/blog/2012/06/30/compile-a-single-coffeescript-file-from-your-rails-project/"/>
    <updated>2012-06-30T20:56:00-07:00</updated>
    <id>http://scottwb.com/blog/2012/06/30/compile-a-single-coffeescript-file-from-your-rails-project</id>
    <content type="html"><![CDATA[<p>Using Rails 3.2 (or 3.1 for that matter), you typically have CoffeeScript (and a javascript engine like therubyracer/V8) installed as gems - probably via Bundler from your Gemfile, possibly in a per-project RVM gemset.</p>

<p>It doesn&#8217;t seem that this type of setup includes direct access to the <code>coffee</code> command-line compiler. Instead the <code>coffee-script</code> gem uses calls directly to the compiler. Sometimes I&#8217;d just like to compile one of the CoffeeScript files in my Rails app just to look at the javascript, or maybe to export it somewhere that I&#8217;m not using CoffeeScript.</p>

<p>To do this, I&#8217;ve made a simple Rake task. Just drop this into <code>lib/tasks/coffee.rake</code> in your Rails project:</p>

<figure class='code'><figcaption><span>coffee.rake </span><a href='https://gist.github.com/3026787'>View Gist</a></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">namespace</span> <span class="ss">:coffee</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">task</span> <span class="ss">:compile</span><span class="p">,</span> <span class="ss">:filename</span> <span class="k">do</span> <span class="o">|</span><span class="n">t</span><span class="p">,</span> <span class="n">args</span><span class="o">|</span>
</span><span class='line'>    <span class="n">filename</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">filename</span>
</span><span class='line'>    <span class="nb">puts</span> <span class="no">CoffeeScript</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="no">File</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">filename</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>


<p>Then, you can compile a single file from 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>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>rake coffee:compile<span class="o">[</span>app/assets/javascripts/mystuff.js.coffee<span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>This will output it to stdout so you can redirect that to a file, or the <code>gist</code> command, or whatever.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Reload The Same Page Without Blinking On jQuery Mobile]]></title>
    <link href="http://scottwb.com/blog/2012/06/29/reload-the-same-page-without-blinking-on-jquery-mobile/"/>
    <updated>2012-06-29T09:53:00-07:00</updated>
    <id>http://scottwb.com/blog/2012/06/29/reload-the-same-page-without-blinking-on-jquery-mobile</id>
    <content type="html"><![CDATA[<p>When using jQuery Mobile, if you try to reload the current page from javascript using the usual tactics of assigning <code>window.location.href</code>, you might notice some unsightly artifacts and page blinking. This is particularly noticeable on iOS Mobile Safari (mainly on iPhone), because the reload also causes the address bar to slide down and back up.</p>

<p>Below is function I made to refresh the current page successfully with no visible artifacts: no blinking, no address bar showing, transitions:</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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span> <span class="nx">refreshPage</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">$</span><span class="p">.</span><span class="nx">mobile</span><span class="p">.</span><span class="nx">changePage</span><span class="p">(</span>
</span><span class='line'>    <span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">href</span><span class="p">,</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>      <span class="nx">allowSamePageTransition</span> <span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
</span><span class='line'>      <span class="nx">transition</span>              <span class="o">:</span> <span class="s1">&#39;none&#39;</span><span class="p">,</span>
</span><span class='line'>      <span class="nx">showLoadMsg</span>             <span class="o">:</span> <span class="kc">false</span><span class="p">,</span>
</span><span class='line'>      <span class="nx">reloadPage</span>              <span class="o">:</span> <span class="kc">true</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p><em>This was done on jQuery Mobile 1.1.0.</em></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[jQuery Mobile Breaks Your HREFs on iOS Mobile Safari]]></title>
    <link href="http://scottwb.com/blog/2012/06/29/jquery-mobile-breaks-your-hrefs-on-ios-mobile-safari/"/>
    <updated>2012-06-29T00:00:00-07:00</updated>
    <id>http://scottwb.com/blog/2012/06/29/jquery-mobile-breaks-your-hrefs-on-ios-mobile-safari</id>
    <content type="html"><![CDATA[<p>Starting with verison 1.1.0RC1, jQuery Mobile changes the <code>href</code> of any link to be <code>#</code> when it is clicked, but <em>only on iOS Mobile Safari</em>. If you try to make a click handler that reads the <code>href</code> attribute, you will be surprised to find that it does not get the href you intended. 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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">).</span><span class="nx">click</span><span class="p">(</span><span class="kd">function</span><span class="p">()</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">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s1">&#39;href&#39;</span><span class="p">));</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>When using jQuery Mobile, this code will print out the href you expect on every browser except Mobile Safari on iOS (both real devices and simulators, but <em>not with other browsers faking their User-Agent</em>). On Mobile Safari, this will print out that the href is &#8220;#&#8221;.</p>

<h2>The Problem</h2>

<p>Obviously this is a problem if you have code that operates in a click handler that reads the clicked element&#8217;s <code>href</code> attribute. jQuery Mobile changes that href before you get a chance to read it.</p>

<p>One common place this is used is with the <code>link_to</code> helper in Ruby on Rails, for links that need to use other HTTP methods. For example, the standard RESTful way to delete a resource will be with a link that sets <code>:method</code> to <code>:delete</code>. Rails handles this with a library called jQuery UJS that needs to read the href from the link when it is clicked.</p>

<p>Unfortunately for Rails users, this means that Rails method links don&#8217;t work with jQuery Mobile on iOS Mobile Safari.</p>

<!-- MORE -->


<h2>The Reason</h2>

<p>The jQuery Mobile team is doing this on purpose, to make it so the address bar on iPhone does not drop down when changing pages (and we all love that feature!). In <a href="https://github.com/jquery/jquery-mobile/issues/3777">this discussion</a> and <a href="https://github.com/jquery/jquery-mobile/issues/3686">this discussion</a>, they seem to understand that this makes custom click handlers get the wrong href, but decide to go ahead with it anyway.</p>

<p>You can see it happen in this excerpt from the jQuery Mobile 1.1.0 source (reformatted for blog-friendlyness):</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='javascript'><span class='line'><span class="c1">// By caching the href value to data and switching the href to a #,</span>
</span><span class='line'><span class="c1">// we can avoid address bar showing in iOS. The click handler resets</span>
</span><span class='line'><span class="c1">// the href during its initial steps if this data is present</span>
</span><span class='line'><span class="nx">$</span><span class="p">(</span> <span class="nx">link</span> <span class="p">)</span>
</span><span class='line'>  <span class="p">.</span><span class="nx">jqmData</span><span class="p">(</span> <span class="s2">&quot;href&quot;</span><span class="p">,</span> <span class="nx">$</span><span class="p">(</span> <span class="nx">link</span>  <span class="p">).</span><span class="nx">attr</span><span class="p">(</span> <span class="s2">&quot;href&quot;</span> <span class="p">)</span>  <span class="p">)</span>
</span><span class='line'>  <span class="p">.</span><span class="nx">attr</span><span class="p">(</span> <span class="s2">&quot;href&quot;</span><span class="p">,</span> <span class="s2">&quot;#&quot;</span> <span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<h2>The Solution</h2>

<p>Here&#8217;s the part that seems to be undocumented other than in that comment: <em>they store the href in a data attribute!</em> This makes this problem easy to workaround. Simply change your click handler to check for the data attribute first and fall back to the href:</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='javascript'><span class='line'><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">).</span><span class="nx">click</span><span class="p">(</span><span class="kd">function</span><span class="p">()</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">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">data</span><span class="p">(</span><span class="s1">&#39;href&#39;</span><span class="p">)</span> <span class="o">||</span> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s1">&#39;href&#39;</span><span class="p">));</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>That would be easy enough to encapsulate in a nice helper function&#8230;</p>

<h2>Applying This To Rails jQuery UJS</h2>

<p>If you use the Rails <code>link_to</code> helper with the <code>:method</code> option set to anything other than <code>:get</code>, links are handled by the <code>jquery_ujs</code> library that comes with Rails (often referred to as Rails UJS, jQuery UJS, rails.js). You&#8217;ll notice, for example that none of your <code>:delete</code> links work with jQuery Mobile when you are on iOS Mobile Safari. You might see this as a missing route for your <code>show</code> or <code>edit</code> method depending on where you are linking from.</p>

<p>It turns out the fix is easy. The jquery_ujs code already has a method to encapsulate getting the href from an element, complete with a comment telling you how you can override it. All it does out of the box is read <code>element.attr('href')</code>. All we have to do is override it and make it try the data attribute first.</p>

<p>Somewhere after <code>jquery_ujs.js</code> is loaded, define 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='javascript'><span class='line'><span class="nx">$</span><span class="p">.</span><span class="nx">rails</span><span class="p">.</span><span class="nx">href</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">element</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">element</span><span class="p">.</span><span class="nx">data</span><span class="p">(</span><span class="s1">&#39;href&#39;</span><span class="p">)</span> <span class="o">||</span> <span class="nx">element</span><span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s1">&#39;href&#39;</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now your Rails <code>link_to</code> calls with custom <code>:method</code> options such as <code>:delete</code> will work just fine with jQuery Mobile 1.1.0 on iOS Mobile Safari, and you don&#8217;t have to lose that clever bit that keeps the address bar hidden.</p>

<hr />

<p><em><strong>UPDATED Jan. 3, 2013:</strong> I have confirmed that this problem has been solved in jQuery Mobile 1.2.0, and this work-around is no longer required. I did try to upgrade one of my jQuery Mobile 1.1.0 apps to 1.1.1 (but rolled back for other reasons), and in that process I believe that the above solution was still required.</em></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My New Home]]></title>
    <link href="http://scottwb.com/blog/2012/04/27/my-new-home/"/>
    <updated>2012-04-27T13:00:00-07:00</updated>
    <id>http://scottwb.com/blog/2012/04/27/my-new-home</id>
    <content type="html"><![CDATA[<p>I am pleased to announce that I have found my new home. As of next week, I&#8217;ll be starting my new position as the VP of Engineering at <a href="http://www.validas.com/">Validas</a>.</p>

<p><a href="http://www.validas.com/"><img class="left" src="http://scottwb.com/images/validas-logo.png" title="Validas" alt="Validas"></a></p>

<p>Over the last two months, I&#8217;ve networked with a lot of great people and have considered opportunties at many great companies. There are so many exciting things going on in our industry, that this was not an easy decision. After taking some time to reflect, the opportunity at Validas stood out above the rest for a number of reasons.</p>

<!-- MORE -->


<p>The biggest differentiating factors were the company culture and lifestyle, and the comaraderie and ethos of the team. Earlier this month, I spent 3 days in NYC with the core members of the team. This happened to be during a big crunch time for them, with an important ship date coming just around the corner. I got to see first-hand how the team works and plays together, and how they deal with last-minute hiccups and schedule pressure. I had the opportunity to be a fly on the wall while they worked and to be included in some very interesting architecture discussions where it was clear my expertise was immediately impactful. I even got to take part in some fun PR and marketing operations with the team. I like how this team carries itself and, of all the places I&#8217;ve searched, I believe this is where I will feel the most at home.</p>

<p>Our team is distributed around the country, and we collaborate using all the modern online tools. Even though it&#8217;s still before my official start date, I&#8217;ve had a chance to get my feet wet this week just for fun (and because I am too excited to hold back). I am looking forward to enjoying the work-at-home lifestyle where I already have plenty of experience and know that I can be super-productive while still being able to hit the gym and have lunch with my kids.</p>

<p>Another big attraction to Validas is that it presents the greatest opportunity to work with cutting-edge technologies to solve the kinds of problems I find interesting. The volume of data we handle at Validas is enormous and growing rapidly. The huge success of our enterprise product, <a href="http://www.validas.com/solutions/vera-for-enterprise/">VERA</a>, shows that we&#8217;re already the premier provider of big-data solutions in the mobile space ⎯ not to mention our forthcoming consumer-facing offerings. I see this as a great opportunity to lead the development of innovative solutions in big-data intelligence, to push the envelope in exicting areas such as machine learning and predictive analytics, and to build the next generation of consumer-facing products. The vision of this company and its founding team is <a href="http://en.wikipedia.org/wiki/Big_Hairy_Audacious_Goal">big, hairy, and audacious</a> - just the way I like it!</p>

<p>After being on the sideline for two months, I am chomping at the bit to get back into the game. I&#8217;m already ramping up. My first level of impact will involve applying my fresh perspective to existing systems to help find architectural solutions to the next wave of challenges. I&#8217;ll also be heavily involved in building and launching some new products that have yet to be announced, so stay tuned&#8230;</p>

<p>Many thanks go out to everyone that supported me while I found my next path - especially my wife, Laurie. I rely on your support and your faith in me.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Custom Error Handling With Express Resource Auto-Loading]]></title>
    <link href="http://scottwb.com/blog/2012/04/19/custom-error-handling-with-express-resource-auto-loading/"/>
    <updated>2012-04-19T14:05:00-07:00</updated>
    <id>http://scottwb.com/blog/2012/04/19/custom-error-handling-with-express-resource-auto-loading</id>
    <content type="html"><![CDATA[<p><span class='pullquote-right' data-pullquote='You get no control over how the errors are handled, and the default mechanism is ignorant of the requested format.'>
<a href="https://github.com/visionmedia/express-resource">Express Resource</a> adds RESTful resource routing on top of the <a href="http://expressjs.com/">Express</a> framework for <a href="http://nodejs.org/">Node.js</a>. It includes a nice feature called &#8220;Auto-Loading&#8221; that allows you to define a single <code>load</code> method that will automatically be used to load your resource object from a URL id parameter and set it in the request object to be accessed by your conventional RESTful methods that need it. Another great feature of Express Resource is what they call &#8220;Content-Negotiation&#8221;. URLs with format extensions like <code>.xml</code> or <code>.json</code> automatically set an indicator of the format in the request so your actions can respond accordingly.</p>

<p>However, a problem arises when you use the recommended method for handling and reporting errors from within your <code>load</code> method: You get no control over how the errors are handled, and the default mechanism is ignorant of the requested format. This makes it tough to return nice JSON errors, for example, from a REST API that uses this technique.
</span></p>

<!-- MORE -->


<h2>The Problem</h2>

<p>The canonical example in the Express Resource documentation describes implementing a <code>load</code> method on your resource that calls a callback with an error and/or the loaded object, like so:</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='javascript'><span class='line'><span class="nx">exports</span><span class="p">.</span><span class="nx">load</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">id</span><span class="p">,</span> <span class="nx">fn</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">user</span>  <span class="o">=</span> <span class="nx">yourLookupUserFunction</span><span class="p">(</span><span class="nx">id</span><span class="p">);</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">error</span> <span class="o">=</span> <span class="nx">user</span> <span class="o">?</span> <span class="kc">null</span> <span class="o">:</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s2">&quot;User Not Found&quot;</span><span class="p">);</span>
</span><span class='line'>  <span class="nx">fn</span><span class="p">(</span><span class="nx">error</span><span class="p">,</span> <span class="nx">user</span><span class="p">);</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is great, except there are a few problems:</p>

<ol>
<li><p><strong>You can&#8217;t override what Express Resource does with this error.</strong> The way Express Resource works here is to use a param callback. If you pass an error, its param callback simply passes the error to the next method in the chain via <code>next(err)</code>, which ends up invoking the default error handling before any of your methods or <code>app.error()</code> handlers are called. This means that a RESTful API call expecting a JSON response won&#8217;t get a JSON response. If you want your JSON API 404s to return JSON data in the payload, you are out of luck.</p></li>
<li><p><strong>There is no response object passed to your auto-load function.</strong> This makes it difficult to bypass Express Resource&#8217;s intended error handling and issue a response directly on your own.</p></li>
<li><p><strong>The content-negotiation hasn&#8217;t happened yet at this point in the chain.</strong> This means, even if you could respond directly from your <code>load</code> function, you wouldn&#8217;t be able to use the conventional Express Resource mechanism of testing <code>req.format</code> to know whether or not to response with JSON, HTML, XML, whatever.</p></li>
</ol>


<h2>The Solution</h2>

<p>It turns out there are some simple solutions to these problems. Let&#8217;s tackle them in reverse order.</p>

<ol>
<li><p>Even though Express Resource has not parsed the route&#8217;s format yet, and set <code>req.format</code> for you, the core routing framework has already done this as params matching. Instead of <code>req.format</code>, for this method you need to use <code>req.params.format</code>.</p></li>
<li><p>Even though Express Resource does not pass the <code>res</code> object into your <code>load</code> function, node.js has conveniently linked the two together for us. In this method, instead of using <code>res</code>, you need to use <code>req.res</code>. This may be a little dangerous since it relies on the internal structure of the request object, but this linkage seems relatively stable.</p></li>
<li><p>Don&#8217;t have your <code>load</code> function call the given callback if there is an error. Only call it in the success case. If there is an error, render the response yourself using the tactics described above for #1 and #2.</p></li>
</ol>


<h2>Putting it all together</h2>

<p>Here is an example of what all that might look like for your <code>load</code> function:</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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">exports</span><span class="p">.</span><span class="nx">load</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">id</span><span class="p">,</span> <span class="nx">fn</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">user</span> <span class="o">=</span> <span class="nx">yourLookupUserFunction</span><span class="p">(</span><span class="nx">id</span><span class="p">);</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="nx">user</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">fn</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">user</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">else</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">switch</span> <span class="p">(</span><span class="nx">req</span><span class="p">.</span><span class="nx">params</span><span class="p">.</span><span class="nx">format</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">case</span> <span class="s1">&#39;json&#39;</span><span class="o">:</span>
</span><span class='line'>      <span class="nx">req</span><span class="p">.</span><span class="nx">res</span><span class="p">.</span><span class="nx">json</span><span class="p">({</span><span class="s1">&#39;error&#39;</span> <span class="o">:</span> <span class="s1">&#39;User Not Found&#39;</span><span class="p">},</span> <span class="mi">404</span><span class="p">);</span>
</span><span class='line'>      <span class="k">break</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">case</span> <span class="s1">&#39;html&#39;</span><span class="o">:</span>
</span><span class='line'>    <span class="k">default</span><span class="o">:</span>
</span><span class='line'>      <span class="c1">// Regular 404...or render your own template if you like</span>
</span><span class='line'>      <span class="nx">req</span><span class="p">.</span><span class="nx">res</span><span class="p">.</span><span class="nx">send</span><span class="p">(</span><span class="mi">404</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Moving On]]></title>
    <link href="http://scottwb.com/blog/2012/03/06/moving-on/"/>
    <updated>2012-03-06T12:25:00-08:00</updated>
    <id>http://scottwb.com/blog/2012/03/06/moving-on</id>
    <content type="html"><![CDATA[<p>Last week I resigned my position as CTO at <a href="http://www.balassanianenterprises.com/">Balassanian Enterprises</a>.</p>

<p>Those of you who know me know that this is <em>huge</em>. I&#8217;ve been working closely with the founder, Edward Balassanian, for a <em>long</em> time. It&#8217;s been spread over multiple companies and an array of varying products, but it&#8217;s always been a shared ethos with the same core group of people that has defined that period of my career. Leaving that behind feels a bit like an emotional breakup. Such a big part of my professional and personal identity was wrapped up in how we worked together, what we built, where we&#8217;ve been, and our vision of the future. We put everything into it.</p>

<p>It&#8217;s also a bit like leaving home to explore new worlds.</p>

<!-- MORE -->


<p>I didn&#8217;t leave because of some falling out or because of &#8220;creative differences&#8221;. I think Edward and the team at Balasannian Enterprises have brilliant ideas and an exciting work environment. Some of the best minds in software development, product design, intellectual property, and creative strategy work there.</p>

<p>I left because it is time for me to do something different. My motiviation has always been driven by the pursuit of <a href="http://www.youtube.com/watch?v=u6XAPnuFjJc">autonomy, mastery, and purpose</a>. That hasn&#8217;t changed. It&#8217;s the way I&#8217;ve been going about it that needs to change. I feel the need for fresh inspiration, new relationships, and a different variety of opportunity.</p>

<p>How will I achieve that? I have no idea.</p>

<p>There are so many options; it&#8217;s a bit daunting. I have some ideas that I might like to pursue on my own. I have some great contacts in the industry that are working on some really cool and <strong><em>BIG</em></strong> problems. It would be awesome to align with them. There are some exciting companies in my own backyard working on building the future and there is a vibrant, growing community of tech startups here in the Seattle area. I have a skill set that is in high demand on the contracting/consulting scene, and there&#8217;s always the allure of contributing on a large scale to a meaningful open source project. That kind of networking can be fun and door-opening.</p>

<p>I&#8217;ve committed to taking at least a month off to let it all sink in. With an open mind, I am going to explore all avenues and opportunities before crafting the path into the next phase of my career. I&#8217;m open to all ideas! Please feel free to comment below or connect with me privately. I&#8217;d love to hear your feedback.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Middleman Deployment Rakefile]]></title>
    <link href="http://scottwb.com/blog/2012/02/24/middleman-deployment-rakefile/"/>
    <updated>2012-02-24T09:30:00-08:00</updated>
    <id>http://scottwb.com/blog/2012/02/24/middleman-deployment-rakefile</id>
    <content type="html"><![CDATA[<p>I started tinkering with <a href="http://middlemanapp.com/">Middleman</a> the other day to build a small static website.</p>

<p>In the past, I&#8217;ve used <a href="http://staticmatic.rubyforge.org/">StaticMatic</a>, <a href="http://jekyllrb.com/">Jekyll</a>, or <a href="http://octopress.org/">Octopress</a> for this sort of thing. Jekyll and Octopress are great for blogs. StaticMatic is great for prototyping a static site with Haml and Sass that you can easily migrate to your Rails app.</p>

<p><span class='pullquote-right' data-pullquote='any custom middleware you add to the stack gets invoked for all the pages it generates.'>
I&#8217;ve been using Middleman for less than 24 hours at this point, but so far it seems great. One of the things I like about it is that it&#8217;s actually serving your site dynamically with Sinatra, and comes out of the box with Haml, Sass, Compass, view helpers, lorem ipsum generators, CSS and Javascript minification, and more. Plus, you can throw in any other Rack middleware you like, for example, the Google Analytics middleware. While you&#8217;re developing the site, you run the preview server and your changes are immediately visible without restarting it. The cool part is that when you deploy, it actually generates all the static pages by executing the Rack server and running requests through it. This means any custom middleware you add to the stack gets invoked for all the pages it generates.
</span></p>

<p>Middleman imposes very little structure. This is refreshing if you&#8217;re just making a one- or two-page site. It lets you get started right away and build whatever organization you want for yourself. It doesn&#8217;t tell you how to deploy your site. That&#8217;s I&#8217;m here to do. :)</p>

<p>I took a little inspiration from Octopress&#8217;s Rakefile and put together a simple Rakefile for use with Middleman that helps you deploy your static site via rsync. It also adds a few little wrapper tasks for things like remembering to add the <code>--clean</code> flag, and an Octopress-like <code>rake gen_deploy</code> task that regenerates and deploys in one step.</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='bash'><span class='line'><span class="nv">$ </span>rake -T
</span><span class='line'>rake build       <span class="c"># Build the website from source</span>
</span><span class='line'>rake deploy      <span class="c"># Deploy website via rsync</span>
</span><span class='line'>rake gen_deploy  <span class="c"># Build and deploy website</span>
</span><span class='line'>rake preview     <span class="c"># Run the preview server at http://localhost:4567</span>
</span></code></pre></td></tr></table></div></figure>


<p>Simply drop this Rakefile into the root of your Middleman project and edit the SSH variables at the top.</p>

<figure class='code'><figcaption><span>Rakefile </span><a href='https://gist.github.com/1902178'>View Gist</a></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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">SSH_USER</span> <span class="o">=</span> <span class="s1">&#39;root&#39;</span>
</span><span class='line'><span class="no">SSH_HOST</span> <span class="o">=</span> <span class="s1">&#39;www.example.com&#39;</span>
</span><span class='line'><span class="no">SSH_DIR</span>  <span class="o">=</span> <span class="s1">&#39;/var/www/html/www.example.com&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="n">desc</span> <span class="s2">&quot;Build the website from source&quot;</span>
</span><span class='line'><span class="n">task</span> <span class="ss">:build</span> <span class="k">do</span>
</span><span class='line'>  <span class="nb">puts</span> <span class="s2">&quot;## Building website&quot;</span>
</span><span class='line'>  <span class="n">status</span> <span class="o">=</span> <span class="nb">system</span><span class="p">(</span><span class="s2">&quot;middleman build --clean&quot;</span><span class="p">)</span>
</span><span class='line'>  <span class="nb">puts</span> <span class="n">status</span> <span class="p">?</span> <span class="s2">&quot;OK&quot;</span> <span class="p">:</span> <span class="s2">&quot;FAILED&quot;</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">desc</span> <span class="s2">&quot;Run the preview server at http://localhost:4567&quot;</span>
</span><span class='line'><span class="n">task</span> <span class="ss">:preview</span> <span class="k">do</span>
</span><span class='line'>  <span class="nb">system</span><span class="p">(</span><span class="s2">&quot;middleman server&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="n">desc</span> <span class="s2">&quot;Deploy website via rsync&quot;</span>
</span><span class='line'><span class="n">task</span> <span class="ss">:deploy</span> <span class="k">do</span>
</span><span class='line'>  <span class="nb">puts</span> <span class="s2">&quot;## Deploying website via rsync to </span><span class="si">#{</span><span class="no">SSH_HOST</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class='line'>  <span class="n">status</span> <span class="o">=</span> <span class="nb">system</span><span class="p">(</span><span class="s2">&quot;rsync -avze &#39;ssh&#39; --delete build/ </span><span class="si">#{</span><span class="no">SSH_USER</span><span class="si">}</span><span class="s2">@</span><span class="si">#{</span><span class="no">SSH_HOST</span><span class="si">}</span><span class="s2">:</span><span class="si">#{</span><span class="no">SSH_DIR</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
</span><span class='line'>  <span class="nb">puts</span> <span class="n">status</span> <span class="p">?</span> <span class="s2">&quot;OK&quot;</span> <span class="p">:</span> <span class="s2">&quot;FAILED&quot;</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">desc</span> <span class="s2">&quot;Build and deploy website&quot;</span>
</span><span class='line'><span class="n">task</span> <span class="ss">:gen_deploy</span> <span class="o">=&gt;</span> <span class="o">[</span><span class="ss">:build</span><span class="p">,</span> <span class="ss">:deploy</span><span class="o">]</span> <span class="k">do</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[A Better Way To Add Mobile Pages To A Rails Site]]></title>
    <link href="http://scottwb.com/blog/2012/02/23/a-better-way-to-add-mobile-pages-to-a-rails-site/"/>
    <updated>2012-02-23T10:42:00-08:00</updated>
    <id>http://scottwb.com/blog/2012/02/23/a-better-way-to-add-mobile-pages-to-a-rails-site</id>
    <content type="html"><![CDATA[<p>Having a mobile version of your website is a pretty common thing these days. Doing it with Ruby on Rails seems pretty common as well. Yet there seems to be a lot of misguidance on the web when you search google for advice on making mobile sites in Rails. There are two prevailing suggestions for accomplishing this that I think are undesirable. I&#8217;ve come up with another variation on this that I think is more maintainable and better for the end user.</p>

<p>But first&#8230;some background&#8230;</p>

<p><em><strong>TL;DR:</strong> Don&#8217;t use custom MIME formats or domain redirects. Use custom view paths. Skip to &#8220;The Final Solution&#8221; at the bottom, if you don&#8217;t care why.</em></p>

<!-- MORE -->


<h2>Adding a Custom Format Sucks</h2>

<p>I don&#8217;t like everyone&#8217;s suggestion of adding a <code>:mobile</code> or <code>:iphone</code> MIME type 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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Mime</span><span class="o">::</span><span class="no">Type</span><span class="o">.</span><span class="n">register_alias</span> <span class="s2">&quot;text/html&quot;</span><span class="p">,</span> <span class="ss">:mobile</span>
</span></code></pre></td></tr></table></div></figure>


<p>These solutions detect the User-Agent in a <code>before_filter</code> and set the request format 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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">request</span><span class="o">.</span><span class="n">format</span> <span class="o">=</span> <span class="ss">:mobile</span>
</span></code></pre></td></tr></table></div></figure>


<p>This sucks if you want to use any of your partials for both mobile and desktop pages, because they are considered to be different formats. Say you have a template <code>show.html.haml</code> for the desktop version of the page, and a <code>show.mobile.haml</code> template for the mobile version. You can&#8217;t have them both render the same partial. Now imagine you have a common footer you want to use in both version. You&#8217;d like to just <code>render :partial =&gt; 'footer'</code> and have it work. But it doesn&#8217;t. If your partial is named <code>_footer.html.haml</code>, rendering the mobile template will complain that it can&#8217;t find <code>_footer.mobile.haml</code>. You&#8217;re stuck maintaining two identical copies of this partial. This <em>really</em> sucks if you have a lot of these kinds of partials.</p>

<p>Some people suggest removing the format in the filename, so you&#8217;d have <code>_footer.haml</code>. I am not fond of this solution.</p>

<h2>Adding a Custom Domain Sucks</h2>

<p>Don&#8217;t you hate it when you see a link to an article on Twitter and click it on your desktop, only to be taken to http://m.whatever.com/ because someone shared this link from their mobile device? Now you&#8217;re reading a mobile version of this article full screen on your desktop and it looks ridiculous. Or you hit a full version URL from your mobile device and have to suffer yet another redirect. As a user, I would prefer to see one page that looks mobile-friendly on a mobile device, and looks like a full version on a desktop. As a developer, redirecting seems like a cop-out. It also feels like it violates a good RESTful design. There should be one URL for this resource and its view should be tailored to the device on which I am viewing it.</p>

<h2>There Is A Better Way</h2>

<p>I like to leverage the same <code>before_filter</code> concept of the custom format solution to detect whether or not you are on a mobile device. You can even add a check for a query param that allows a request to set a flag in the session that overrides the mobile-or-not setting. The first step to this is to build some filters and helpers into your <code>ApplicationController</code>:</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">check_for_mobile</span>
</span><span class='line'>  <span class="n">session</span><span class="o">[</span><span class="ss">:mobile_override</span><span class="o">]</span> <span class="o">=</span> <span class="n">params</span><span class="o">[</span><span class="ss">:mobile</span><span class="o">]</span> <span class="k">if</span> <span class="n">params</span><span class="o">[</span><span class="ss">:mobile</span><span class="o">]</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="nf">mobile_device?</span>
</span><span class='line'>  <span class="k">if</span> <span class="n">session</span><span class="o">[</span><span class="ss">:mobile_override</span><span class="o">]</span>
</span><span class='line'>    <span class="n">session</span><span class="o">[</span><span class="ss">:mobile_override</span><span class="o">]</span> <span class="o">==</span> <span class="s2">&quot;1&quot;</span>
</span><span class='line'>  <span class="k">else</span>
</span><span class='line'>    <span class="c1"># Season this regexp to taste. I prefer to treat iPad as non-mobile.</span>
</span><span class='line'>    <span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">user_agent</span> <span class="o">=~</span> <span class="sr">/Mobile|webOS) &amp;&amp; (request.user_agent !~ /i</span><span class="no">Pad</span><span class="o">/</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="n">helper_method</span> <span class="ss">:mobile_device?</span>
</span></code></pre></td></tr></table></div></figure>


<p>With these in your <code>ApplicationControlelr</code>, you can add <code>before_filter :check_for_mobile</code> to any controller/action and have it detect whether or not a request is mobile, or is forced to be mobile (or not) with a query parameter <code>mobile=1</code> (or <code>mobile=0</code>). You also have a <code>mobile_device?</code> method that you can call from any controller or view to see if you are currently rendering a mobile-formatted page. (This is rarely needed, but can be handy in certain situations.)</p>

<p>The next step is to tell Rails to render mobile versions of the templates if the request is deemed to be from a mobile device. Rather than using a custom format, use a <strong><em>custom view path</em></strong>. This is the trick used by Rails engines and plugins to extend the app with its own view templates, while still allowing them to be overridden by the app. To make this work, you need to <strong><em>create a separate directory structure for mobile view templates</em></strong> and add it to the front of the view load path. This way, you can still use the <code>:html</code> format, and can still share templates between mobile and desktop templates. As an added bonus, you can still serve full versions of pages to mobile devices if you haven&#8217;t implemented the mobile version yet. This is huge if you&#8217;re trying to incrementally add mobile-friendly pages to an existing desktop-oriented site.</p>

<p>For purposes of this example, I&#8217;ll call this parallel mobile views directory <code>views_mobile</code>. The way you prepend that to the view load path is with the <code>prepend_view_path</code> method. I prefer to put this in a method in <code>ApplicationController</code> that I can call from the <code>check_for_mobile</code> filter if a mobile device is detected, or that I can use directly as it&#8217;s own filter:</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">prepare_for_mobile</span>
</span><span class='line'>  <span class="n">prepend_view_path</span> <span class="no">Rails</span><span class="o">.</span><span class="n">root</span> <span class="o">+</span> <span class="s1">&#39;app&#39;</span> <span class="o">+</span> <span class="s1">&#39;views_mobile&#39;</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now, if you use <code>before_filter :prepare_for_mobile</code> on any action, it will <em>always</em> be treated as mobile, rendering templates from your <code>app/views_mobile</code> directory tree if they exist, falling back to those in your <code>app/views</code> directory tree if they don&#8217;t. This is great if you have a <em>mobile-first responsive design</em> for your page that you want to always serve to mobile and desktop devices, but still have other pages (and layouts) that are fully designed for the desktop that you don&#8217;t want to mix with.</p>

<p>The last step is to augment the <code>check_for_mobile</code> filter method to call <code>prepare_for_mobile</code> if it detects a mobile device. That way you can use <code>before_filter :check_for_mobile</code> on methods that have two versions. Those actions will render the mobile version from <code>app/views_mobile</code> for mobile devices and from <code>app/views</code> for non-mobile devices &#8211; and they can both render the same shared partials living in <code>app/views</code>.</p>

<h2>The Final Solution</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>
<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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># app/controllers/application_controller.rb</span>
</span><span class='line'><span class="k">class</span> <span class="nc">ApplicationController</span> <span class="o">&lt;</span> <span class="no">ActionController</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">check_for_mobile</span>
</span><span class='line'>    <span class="n">session</span><span class="o">[</span><span class="ss">:mobile_override</span><span class="o">]</span> <span class="o">=</span> <span class="n">params</span><span class="o">[</span><span class="ss">:mobile</span><span class="o">]</span> <span class="k">if</span> <span class="n">params</span><span class="o">[</span><span class="ss">:mobile</span><span class="o">]</span>
</span><span class='line'>    <span class="n">prepare_for_mobile</span> <span class="k">if</span> <span class="n">mobile_device?</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="nf">prepare_for_mobile</span>
</span><span class='line'>    <span class="n">prepend_view_path</span> <span class="no">Rails</span><span class="o">.</span><span class="n">root</span> <span class="o">+</span> <span class="s1">&#39;app&#39;</span> <span class="o">+</span> <span class="s1">&#39;views_mobile&#39;</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="nf">mobile_device?</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">session</span><span class="o">[</span><span class="ss">:mobile_override</span><span class="o">]</span>
</span><span class='line'>      <span class="n">session</span><span class="o">[</span><span class="ss">:mobile_override</span><span class="o">]</span> <span class="o">==</span> <span class="s2">&quot;1&quot;</span>
</span><span class='line'>    <span class="k">else</span>
</span><span class='line'>      <span class="c1"># Season this regexp to taste. I prefer to treat iPad as non-mobile.</span>
</span><span class='line'>      <span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">user_agent</span> <span class="o">=~</span> <span class="sr">/Mobile|webOS) &amp;&amp; (request.user_agent !~ /i</span><span class="no">Pad</span><span class="o">/</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="n">helper_method</span> <span class="ss">:mobile_device?</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># app/controllers/posts_controller.rb</span>
</span><span class='line'><span class="k">class</span> <span class="nc">PostsController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
</span><span class='line'>  <span class="c1"># Render mobile or desktop depending on User-Agent for these actions.</span>
</span><span class='line'>  <span class="n">before_filter</span> <span class="ss">:check_for_mobile</span><span class="p">,</span> <span class="ss">:only</span> <span class="o">=&gt;</span> <span class="o">[</span><span class="ss">:new</span><span class="p">,</span> <span class="ss">:edit</span><span class="o">]</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># Always render mobile versions for these, regardless of User-Agent.</span>
</span><span class='line'>  <span class="n">before_filter</span> <span class="ss">:prepare_for_mobile</span><span class="p">,</span> <span class="ss">:only</span> <span class="o">=&gt;</span> <span class="ss">:show</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[HTML5 Placeholder Polyfill With jQuery Mobile]]></title>
    <link href="http://scottwb.com/blog/2012/02/22/html5-placeholder-polyfill-with-jquery-mobile/"/>
    <updated>2012-02-22T11:29:00-08:00</updated>
    <id>http://scottwb.com/blog/2012/02/22/html5-placeholder-polyfill-with-jquery-mobile</id>
    <content type="html"><![CDATA[<p>IE9 doesn&#8217;t support the <code>placeholder</code> attribute on <code>input</code> elements. Why does this matter for a jQuery Mobile site? If you want to build a single mobile-first page with jQuery Mobile, and use resposive design techiniques to display the same page on desktop browsers, you might appreciate the ability to use the HTML5 <code>placeholder</code> in your design, and still need to support at least IE9.</p>

<p>Using a <code>placeholder</code> on an input field is a nice way to save space on a mobile form. It lets you forgo the field labels while still telling the user what the fields are for. In many cases its utility goes beyond labeling; you might need it to express input requirements such as what unit of measurement to use in a &#8220;distance&#8221; field. This is just as useful in a desktop design as it is in a mobile design.</p>

<p>There is a growing movement of creating polyfills for this sort of thing - especially targeted at Internet Explorer. If you&#8217;re not already using something like <a href="http://www.modernizr.com/">Modernizr</a> to fulfill all your HTML5 polyfill needs, and just want a solution for the <code>placeholder</code> attribute, you&#8217;re in luck. The Modernizr wiki has a <a href="https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills">list of polyfills</a>. You&#8217;ll see that there are quite a few placeholder polyfills&#8230;but they are not all created equal.</p>

<!-- MORE -->


<p>I tried a few of these, and read through the code, and realized that a few of them have some problems. Problems like not supporting password fields, or incorrectly styling non-focused input placeholders. After reviewing these, I decided that the best one to go with was <a href="http://mths.be/placeholder">jquery-placeholder</a> by <a href="http://mathiasbynens.be/">Mathias Bynens</a>. This one is easy to use, no-ops on browsers that natively support input placeholders, and nicely handles all the corner cases I tested for.</p>

<p>To use jquery-placeholder, simply download <code>jquery.placeholder.js</code> from the <a href="http://mths.be/placeholder">project page</a> and make sure it gets included in your page after jQuery. Then you&#8217;ll need to apply it to every element you want with a jQuery statement 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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">$</span><span class="p">(</span><span class="nx">selector</span><span class="p">).</span><span class="nx">placeholder</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you want to use this with jQuery Mobile and its AJAX page-loading mode, you&#8217;ll need to make sure to execute this statement on every page that gets loaded. You can do that in your main javascript for the document with something 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>
<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="nb">document</span><span class="p">).</span><span class="nx">bind</span><span class="p">(</span><span class="s1">&#39;pageshow&#39;</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">activePage</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;.page.ui-page-active&#39;</span><span class="p">);</span>
</span><span class='line'>  <span class="nx">activePage</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="s1">&#39;input[placeholder], textarea[placeholder]&#39;</span><span class="p">).</span><span class="nx">placeholder</span><span class="p">();</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>From there, you may notice that the placeholder looks different on all the different browsers. You can style this to be consistent with CSS. You&#8217;ll need to be a bit repetitive here, because of the way putting the placeholder selectors on the same line breaks some of the browsers. If you use a CSS preprocessor this gets a little easier. Here is an example using Sass&#8217;s SCSS syntax:</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>
</pre></td><td class='code'><pre><code class='scss'><span class='line'><span class="nt">input</span><span class="o">,</span> <span class="nt">textarea</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">&amp;</span><span class="nc">.placeholder</span> <span class="p">{</span>
</span><span class='line'>    <span class="na">color</span><span class="o">:</span> <span class="mh">#A9A9A9</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="na">&amp;</span><span class="o">:-</span><span class="n">moz-placeholder</span> <span class="p">{</span>
</span><span class='line'>    <span class="na">color</span><span class="o">:</span> <span class="mh">#A9A9A9</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="na">&amp;</span><span class="o">::-</span><span class="n">webkit-input-placeholder</span> <span class="p">{</span>
</span><span class='line'>    <span class="na">color</span><span class="o">:</span> <span class="mh">#A9A9A9</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[jQuery Mobile and Rails UJS Method Links]]></title>
    <link href="http://scottwb.com/blog/2012/02/17/jquery-mobile-and-rails-ujs-method-links/"/>
    <updated>2012-02-17T19:59:00-08:00</updated>
    <id>http://scottwb.com/blog/2012/02/17/jquery-mobile-and-rails-ujs-method-links</id>
    <content type="html"><![CDATA[<p>Rails UJS and jQuery Mobile do not play nice together when it comes to combining Rails UJS&#8217;s handling of non-GET/POST links with jQuery Mobile&#8217;s data attributes such as <code>data-ajax</code>, <code>data-direction</code>, and <code>data-transition</code>. This post demonstrates a quick hack you can use to remedy this.</p>

<!-- MORE -->


<p>The <code>rails.js</code> file from <a href="https://github.com/rails/jquery-ujs">rails/jquery-ujs</a> does some very cool stuff to let you emulate HTTP methods other than GET and POST. It does this by looking for a <code>data-method</code> attribute on your links. When it finds one, e.g.: <code>data-method='delete'</code>, it creates an invisible form to submit a POST with all your link details and a special <code>_method=delete</code> parameter that Rails handles in the backend as if it were a <code>DELETE</code> method. Using the <code>link_to</code> helper, that normally looks 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>
</pre></td><td class='code'><pre><code class='erb'><span class='line'><span class="cp">&lt;%=</span> <span class="n">link_to</span> <span class="s2">&quot;Delete&quot;</span><span class="p">,</span> <span class="vi">@post</span><span class="p">,</span> <span class="ss">:confirm</span> <span class="o">=&gt;</span> <span class="s2">&quot;You sure?&quot;</span><span class="p">,</span> <span class="ss">:method</span> <span class="o">=&gt;</span> <span class="ss">:delete</span> <span class="cp">%&gt;</span><span class="x"></span>
</span></code></pre></td></tr></table></div></figure>


<p>jQuery Mobile loads same-domain links and form submissions via AJAX and provide sexy page transitions. You control how these work by adding data attributes to the <code>&lt;a&gt;</code> or <code>&lt;form&gt;</code> element. One important one, that can affect the correct operation of your page, is the <code>data-ajax='false'</code> attribute. That makes disables the AJAX behavior, and loads the next page from your link or form as a new page. That normally looks 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>
</pre></td><td class='code'><pre><code class='erb'><span class='line'><span class="cp">&lt;%=</span> <span class="n">link_to</span> <span class="s2">&quot;View&quot;</span><span class="p">,</span> <span class="vi">@post</span><span class="p">,</span> <span class="s2">&quot;data-ajax&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;false&quot;</span> <span class="cp">%&gt;</span><span class="x"></span>
</span></code></pre></td></tr></table></div></figure>


<p>One important time you may wish to exercise both tactics at the same time is in providing a delete link. You want to use a link to generate a DELETE request via Rails UJS, and you want to redirect to a new page without jQuery Mobile loading it via AJAX. This is how you would attempt that:</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='erb'><span class='line'><span class="cp">&lt;%=</span> <span class="n">link_to</span><span class="p">(</span><span class="s2">&quot;Delete&quot;</span><span class="p">,</span> <span class="vi">@post</span><span class="p">,</span> <span class="ss">:confirm</span> <span class="o">=&gt;</span> <span class="s2">&quot;You sure?&quot;</span><span class="p">,</span>
</span><span class='line'>            <span class="ss">:method</span> <span class="o">=&gt;</span> <span class="ss">:delete</span><span class="p">,</span> <span class="s2">&quot;data-ajax&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;false&quot;</span> <span class="cp">%&gt;</span><span class="x"></span>
</span></code></pre></td></tr></table></div></figure>


<p>Sorry. That won&#8217;t work.</p>

<p>This is because Rails UJS does it&#8217;s magic by creating a new <code>&lt;form&gt;</code> to submit when the link is clicked. It doesn&#8217;t know or care about the <code>data-ajax</code> attribute, and the form it creates does not have that attribute. Then, when the form is submitted, jQuery Mobile handles it using AJAX by default because the form didn&#8217;t specify otherwise.</p>

<p>We need Rails UJS to copy the <code>data-ajax</code> attribute from the link to the form it creates. This is actually a pretty simple fix (hack) to the <code>handleMethod</code> funciton in <code>rails.js</code>. To just one-off this particular attribute, you can do somethign like this:</p>

<figure class='code'><figcaption><span>rails.js.diff </span><a href='https://gist.github.com/1857315'>View Gist</a></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>
</pre></td><td class='code'><pre><code class='diff'><span class='line'><span class="gh">diff --git a/src/rails.js b/src/rails.js</span>
</span><span class='line'><span class="gh">index 06b4e0b..49ff0b2 100644</span>
</span><span class='line'><span class="gd">--- a/src/rails.js</span>
</span><span class='line'><span class="gi">+++ b/src/rails.js</span>
</span><span class='line'><span class="gu">@@ -174,6 +174,11 @@</span>
</span><span class='line'>
</span><span class='line'>       if (target) { form.attr(&#39;target&#39;, target); }
</span><span class='line'>
</span><span class='line'><span class="gi">+      var ajax = link.data(&#39;ajax&#39;);</span>
</span><span class='line'><span class="gi">+      if (ajax !== undefined) {</span>
</span><span class='line'><span class="gi">+        form.attr(&#39;data-ajax&#39;, ajax);</span>
</span><span class='line'><span class="gi">+      }</span>
</span><span class='line'><span class="gi">+</span>
</span><span class='line'>       form.hide().append(metadata_input).appendTo(&#39;body&#39;);
</span><span class='line'>       form.submit();
</span><span class='line'>     },
</span></code></pre></td></tr></table></div></figure>


<p>In fact, I&#8217;ve <a href="https://github.com/scottwb/jquery-ujs/commit/4d6bc50c4545ac2f492c1e584bef1e154cd61522">committed this patch</a> in a branch on my fork of rails/jquery-ujs.</p>

<p>With this addition, the above combination of <code>:method =&gt; :delete</code> and <code>"data-ajax" =&gt; "false"</code> work beautifully together. You can imagine doing this for other data attributes you care about such as <code>data-direction</code> and <code>data-transition</code>.</p>

<p>There&#8217;s currently an <a href="https://github.com/rails/jquery-ujs/issues/189">outstanding issue</a> to address this, with some discussion on how to go about this generically. If you would like to see this make it into the main distro, head over to that issue and voice your support.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Auto-Focus The First Form Element With jQuery Mobile]]></title>
    <link href="http://scottwb.com/blog/2012/02/17/auto-focus-the-first-form-element-with-jquery-mobile/"/>
    <updated>2012-02-17T10:08:00-08:00</updated>
    <id>http://scottwb.com/blog/2012/02/17/auto-focus-the-first-form-element-with-jquery-mobile</id>
    <content type="html"><![CDATA[<p>Imagine a series of pages with simple forms on each of them. With jQuery Mobile, you&#8217;ll usually end up transitioning between these pages using AJAX and nice slide transitions. Since all your forms will end up in the same document, you need to be careful to give them unique IDs if you want to access them from javascript.</p>

<p>One thing I like to do with forms, especially on mobile platforms, is to automatically highlight the first field in the form when it comes in to view. The trick with jQuery Mobile is that with AJAX loading, pre-loading, and BACK/FORWARD buttons, you need to figure out which form is currently in view, and when transitions between forms happen.</p>

<p>Fortunately jQuery Mobile provides us the two facilities we need:</p>

<ul>
<li>The <code>ui-page-active</code> CSS class gets applied to the <code>.page</code> element that is currently in view.</li>
<li>The <code>pageshow</code> event gets triggered on the document when a page transition has completed to change which page is in view.</li>
</ul>


<p>Combine these two, and you can do something like this in your main javascript once for the whole document:</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='javascript'><span class='line'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">bind</span><span class="p">(</span><span class="s1">&#39;pageshow&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">$</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;.page.ui-page-active form :input:visible&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]).</span><span class="nx">focus</span><span class="p">();</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now, after every page transition, if there is a form on the newly-visible page, the first visible input element automatically receives focus. This works across AJAX, non-AJAX, and BACK/FORWARD page transitions.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Revisited: Adding Filters to Stock Devise Controllers]]></title>
    <link href="http://scottwb.com/blog/2012/02/16/revisited-adding-filters-to-stock-devise-controllers/"/>
    <updated>2012-02-16T23:54:00-08:00</updated>
    <id>http://scottwb.com/blog/2012/02/16/revisited-adding-filters-to-stock-devise-controllers</id>
    <content type="html"><![CDATA[<p>Six months ago, I wrote a quick blog post about <a href="http://sleeplesscoding.blogspot.com/2011/08/adding-filters-to-stock-device.html">adding custom filters to stock Devise controllers</a> without subclassing all the Devise controllers. At the end of the post I noted that the prescribed method only worked in the production environment, and not in development.</p>

<p>Today I come to you with a solution that works in development as well as production. The basic mechanics are the same, but we&#8217;ll factor it slightly differently.</p>

<p>First, we&#8217;ll create a module in <code>config/initializers/devise_filters.rb</code> where we&#8217;ll implement a method to add all the filters to Devise that we want. We also need to make sure this module calls this method upon initialization so that our filters get added upon startup (important for the production environment).</p>

<figure class='code'><figcaption><span>devise_filters.rb </span><a href='https://gist.github.com/1851733'>View Gist</a></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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># Put this somewhere it will get auto-loaded, like config/initializers</span>
</span><span class='line'><span class="k">module</span> <span class="nn">DeviseFilters</span>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">add_filters</span>
</span><span class='line'>    <span class="c1"># Example of adding a before_filter to all the Devise controller</span>
</span><span class='line'>    <span class="c1"># actions we care about.</span>
</span><span class='line'>    <span class="o">[</span>
</span><span class='line'>      <span class="no">Devise</span><span class="o">::</span><span class="no">SessionsController</span><span class="p">,</span>
</span><span class='line'>      <span class="no">Devise</span><span class="o">::</span><span class="no">RegistrationsController</span><span class="p">,</span>
</span><span class='line'>      <span class="no">Devise</span><span class="o">::</span><span class="no">PasswordsController</span>
</span><span class='line'>    <span class="o">].</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">controller</span><span class="o">|</span>
</span><span class='line'>      <span class="n">controller</span><span class="o">.</span><span class="n">before_filter</span> <span class="ss">:prepare_for_mobile</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1"># Example of adding one selective before_filter.</span>
</span><span class='line'>    <span class="no">Devise</span><span class="o">::</span><span class="no">RegistrationsController</span><span class="o">.</span><span class="n">before_filter</span> <span class="ss">:check_invite_code</span><span class="p">,</span> <span class="ss">:only</span> <span class="o">=&gt;</span> <span class="ss">:new</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="nb">self</span><span class="o">.</span><span class="n">add_filters</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>At this point, we&#8217;re in the same place we left off back in August: we can add filters to Devise controller actions, but in development mode the second request reloads all the Devise classes and all our filters are gone.</p>

<p>To address this, we need to add a bit of code to the end of the initializer block in <code>config/environments/development.rb</code> like so:</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">MyApp</span><span class="o">::</span><span class="no">Application</span><span class="o">.</span><span class="n">configure</span> <span class="k">do</span>
</span><span class='line'>  <span class="c1"># ...the usual stuff ...</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># Stuff to do on each request</span>
</span><span class='line'>  <span class="n">config</span><span class="o">.</span><span class="n">to_prepare</span> <span class="k">do</span>
</span><span class='line'>    <span class="no">DeviseFilters</span><span class="o">.</span><span class="n">add_filters</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>This tells Rails to call your <code>DeviseFilters.add_filters</code> method before each new request in development mode, after the classes have been reloaded.</p>
]]></content>
  </entry>
  
</feed>
