<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  <title>Anti-pattern</title>
  <id>http://anti-pattern.com</id>
  <updated>2013-03-12</updated>
  <author>
    <name>Brandon Weiss</name>
  </author>
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/anti-pattern" /><feedburner:info uri="anti-pattern" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <title>Use Bundler.setup Instead of Bundler.require</title>
    <link rel="alternate" href="/use-bundler-setup-instead-of-bundler-require" />
    <id>/use-bundler-setup-instead-of-bundler-require</id>
    <published>2013-03-12</published>
    <updated>2013-03-12</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;About two years ago I &lt;a href="http://anti-pattern.com/bundler-setup-vs-bundler-require"&gt;recommended using &lt;code&gt;Bundler.require&lt;/code&gt; instead of &lt;code&gt;Bundler.setup&lt;/code&gt;&lt;/a&gt; to automatically require and load gem dependencies. And in the spirit of &lt;a href="http://37signals.com/svn/posts/3289-some-advice-from-jeff-bezos"&gt;changing my mind often&lt;/a&gt; I’m completely reversing my stance; using &lt;code&gt;Bundler.setup&lt;/code&gt; is significantly better than &lt;code&gt;Bundler.require&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The original reason I thought using &lt;code&gt;Bundler.require&lt;/code&gt; was better was just a variety of bad habits leftover from Rails. Over the years Rails has changed a lot in how it handles dependencies. Gems used to be manually required, then were automatically required via &lt;a href="http://gembundler.org"&gt;Bundler&lt;/a&gt;. Code in the &lt;code&gt;app&lt;/code&gt; folder has always been automatically required, and up until relatively recently so was code in the &lt;code&gt;lib&lt;/code&gt; folder. The general message was and still is pretty clear; Rails implicitly—if not explicitly—discourages manual requiring. Over time this led me to the erroneous conclusion that manually requiring dependencies was repetitious and unnecessary. Why do something manually that can be done automatically?&lt;/p&gt;

&lt;p&gt;But as I’ve gotten better my opinion has shifted the other way. There are some huge benefits to using &lt;code&gt;Bundler.setup&lt;/code&gt; to set up load paths for gems, adding application directories to the load paths, and then manually requiring dependencies wherever they are used in an application.&lt;/p&gt;

&lt;h2 id="it-provides-amazing-design-feedback-and-documentation"&gt;It provides amazing design feedback and documentation&lt;/h2&gt;

&lt;p&gt;Manually requiring dependencies at the top of every file very explicitly tells you and anyone else exactly what dependencies that file has. This makes code easier to read, understand, test, and refactor. It also makes it painfully obvious when a class is too complicated. If a class needs more than five require statements to work there’s probably too much going on in that class.&lt;/p&gt;

&lt;h2 id="it-forces-you-to-think-about-your-dependencies-and-the-cost-associated-with-using-them"&gt;It forces you to think about your dependencies and the cost associated with using them&lt;/h2&gt;

&lt;p&gt;Dependencies aren’t free. Every dependency you have adds complexity and increases coupling. Automatically loading all dependencies makes them seem like they’re free and can be used whenever and wherever in your application. Having to manually require a dependency to use it forces you to justify its use and keeps your dependency list lean.&lt;/p&gt;

&lt;h2 id="it-makes-removing-dependencies-easier"&gt;It makes removing dependencies easier&lt;/h2&gt;

&lt;p&gt;If you automatically load all your dependencies when you load your environment, you have no way of knowing what parts of your application are using any given dependency. You’d have to manually search for class names, method names, and other ways of identifying a dependency in order to update it or remove it, which is tedious and error-prone. If you manually require dependencies, you can just search for the &lt;code&gt;require&lt;/code&gt; statement and remove or replace the dependency.&lt;/p&gt;

&lt;hr&gt;
&lt;p&gt;The big question at the heart of this all is what &lt;em&gt;is&lt;/em&gt; your application? Is it one monolithic, tightly-coupled mess? Or is it a structured collection of small, loosely-coupled objects? Using &lt;code&gt;Bundler.require&lt;/code&gt; will lead you towards the former, but using &lt;code&gt;Bundler.setup&lt;/code&gt; will help ensure you make the latter.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/PRY1HKtempI" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>Ship Now, Not Later</title>
    <link rel="alternate" href="/ship-now-not-later" />
    <id>/ship-now-not-later</id>
    <published>2013-03-04</published>
    <updated>2013-03-04</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;A few days ago I shipped a small project I’ve been working on called &lt;a href="http://arrival.io"&gt;Arrival&lt;/a&gt;. It estimates arrival times for Apple products based on previous release cycles. Embarrassingly, this is something I’d been thinking about for the better part of &lt;em&gt;two years&lt;/em&gt;, but it took until a few days ago for me to make it happen.&lt;/p&gt;

&lt;p&gt;I first had the idea a few years back after I stumbled onto the &lt;a href="http://buyersguide.macrumors.com"&gt;MacRumors Buyer’s Guide&lt;/a&gt; and was shocked at how poorly it was designed and how terribly it visualized the data. I was sure I could do much better. So I spent a few days cranking out a rough version that was functional but lacked an amazing visual design. And that’s where I hit a wall. I think great design is imperative and there was no way I was going to ship a product that didn’t look amazing.&lt;/p&gt;

&lt;p&gt;I spent the next two years sitting on it while I emailed designers to try and find someone to create the perfect design. Many expressed interest but most were ultimately too busy. Twice someone said yes and started working on it but dropped off as other things came up. Eventually I just gave up and shelved it, figuring I’d just meet the right person to work on it with eventually.&lt;/p&gt;

&lt;p&gt;A month or two ago my friend &lt;a href="https://twitter.com/alwaysunday"&gt;Andy Keil&lt;/a&gt; reached out to me with an idea for an event called &lt;a href="http://fuweekend.com"&gt;FinishUp Weekend&lt;/a&gt;. It’s the antithesis of &lt;a href="http://startupweekend.org"&gt;Startup Weekend&lt;/a&gt;, the premise being that starting is easy, finishing is hard, so instead of starting something new let’s finish up the projects we already have. He asked if I’d be interested in going and I told him absolutely, and that I knew just what I was going to work on.&lt;/p&gt;

&lt;p&gt;Two months later I flew down to Austin, met &lt;a href="https://twitter.com/thejurgy"&gt;Andre Jurgensen&lt;/a&gt; (co-founder of &lt;a href="http://handsome.is"&gt;Handsome&lt;/a&gt;) at the event and jammed it out over the next two days. I would love to say that the moment I told Andy I was going I had an epiphany and realized waiting for the perfect design was ridiculous, but the truth is, all through the weeks leading up to the event and even for a little bit while I was down there I debated whether or not I should ship it. It wasn’t until I met &lt;a href="http://joshlong.me"&gt;Josh Long&lt;/a&gt;, who came down to participate in the event and gave a small talk about &lt;a href="http://executebook.com"&gt;Execute&lt;/a&gt;, a book he wrote—&lt;em&gt;in three fucking days&lt;/em&gt;—that I realized how asinine I had truly been. If I’d waited to ship it until it was as perfect as I wanted it to be, I would never have shipped it at all.&lt;/p&gt;

&lt;p&gt;The moment after I shipped &lt;a href="http://arrival.io"&gt;Arrival&lt;/a&gt; was the best I’ve felt in a long time, and I already usually feel pretty good. The high I got from shipping it is better than any food I’ve ever eaten, any drink I’ve ever tasted, or any drug I’ve ever done. The first day &lt;a href="http://arrival.io"&gt;Arrival&lt;/a&gt; received 30,000 hits, three days later it has received over 60,000 hits, and the feedback has been nothing but positive. If you have an unfinished project that hasn’t shipped yet because you’re still perfecting it and it doesn’t quite meet your expectations, remember that you’re the only thing standing in your own way. Stop fucking around and ship it.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/svK22kPzbwE" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>Litany Against Distraction</title>
    <link rel="alternate" href="/litany-against-distraction" />
    <id>/litany-against-distraction</id>
    <published>2012-11-02</published>
    <updated>2012-11-02</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;&lt;p&gt;&lt;em&gt;I must not get distracted.&lt;/em&gt;&lt;br&gt;
Distraction is the mind-killer.&lt;br&gt;
Distraction is the little-death that brings total obliteration.&lt;br&gt;
I will face my distraction.&lt;br&gt;
I will permit it to pass over me and through me.&lt;br&gt;
And when it has gone past I will turn the inner eye to see its path.&lt;br&gt;
Where the distraction has gone there will be nothing.&lt;br&gt;
Only I will remain.  &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/ayjF8eXrXak" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>Upgrading from PostgreSQL 9.1.x to 9.2.x with Homebrew</title>
    <link rel="alternate" href="/upgrading-from-postgresql-9-1-x-to-9-2-x-with-homebrew" />
    <id>/upgrading-from-postgresql-9-1-x-to-9-2-x-with-homebrew</id>
    <published>2012-10-13</published>
    <updated>2012-10-13</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;I use &lt;a href="http://mxcl.github.com/homebrew"&gt;Homebrew&lt;/a&gt; to manage my packages on OS X. It makes installing them dead-simple and keeping them up-to-date dead-easy. But the one package which I always try to upgrade and always winds up breaking my setup is PostgreSQL. It seems like every single minor version (according to &lt;a href="http://semver.org"&gt;Semantic Versioning&lt;/a&gt;) upgrades fine, but then fails to start, and because I start the processes on login and run them in the background using launch agents, it’s never immediately obvious that the upgrade didn’t work, why it didn’t work, or what I need to do next to get it to work.&lt;/p&gt;

&lt;p&gt;This most recently bit me again when upgrading from 9.1.x to 9.2.x because apparently the database format changed and needed to be upgraded. I know how to do it and have done it several times in the past, but I still forget every time. So I’m documenting it here for myself and everyone else who forgets.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NB&lt;/strong&gt;: The exact commands differ depending on which patch level of 9.1.x you’re upgrading from and which patch level of 9.2.x you’re upgrading to. So make sure to change the x’s to the proper patch level in the commands.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;brew update
brew upgrade postgresql
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
mv /usr/local/var/postgres /usr/local/var/postgres91
initdb /usr/local/var/postgres -E utf8
pg_upgrade &lt;span class="se"&gt;\&lt;/span&gt;
  -b /usr/local/Cellar/postgresql/9.1.x/bin &lt;span class="se"&gt;\&lt;/span&gt;
  -B /usr/local/Cellar/postgresql/9.2.x/bin &lt;span class="se"&gt;\&lt;/span&gt;
  -d /usr/local/var/postgres91 &lt;span class="se"&gt;\&lt;/span&gt;
  -D /usr/local/var/postgres
cp /usr/local/Cellar/postgresql/9.2.x/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This will upgrade to the latest version of postgres, stop the process, move your database to a new location, initialize a new database, port your data from the old database to the new database, create a new launch agent, and start the process.&lt;/p&gt;

&lt;p&gt;After you’ve confirmed that postgres has started, everything is working properly, and your old data has been properly converted you can remove the original database.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;rm -rf /usr/local/var/postgres91
&lt;/pre&gt;&lt;/div&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/I1xlmoRsvB0" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>The Repository Pattern and Proper HTTP Status Codes</title>
    <link rel="alternate" href="/the-repository-pattern-and-proper-http-status-codes" />
    <id>/the-repository-pattern-and-proper-http-status-codes</id>
    <published>2012-10-12</published>
    <updated>2012-10-12</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;Suppose you are building an API and you need one of the resources to be upsertable—that is, you need to be able to either insert or update data from the same endpoint. Assuming you’re using Rails, it might look something like this.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;upsert&lt;/span&gt;
  &lt;span class="vi"&gt;@pickle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Pickle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:pickle&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;existing_pickle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Pickle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find_by_id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@pickle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@pickle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;existing_pickle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assign_attributes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:pickle&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="vi"&gt;@pickle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;save&lt;/span&gt;

  &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:pickle&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This isn’t bad, but all the cool kids are using the Repository pattern (which is really just a &lt;a href="http://en.wikipedia.org/wiki/Facade_pattern"&gt;Facade&lt;/a&gt; for persistence layers) to abstract ActiveRecord away from the domain as much as possible. So something like this might be better.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;upsert&lt;/span&gt;
  &lt;span class="vi"&gt;@pickle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;PickleRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create_or_update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:pickle&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:pickle&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now &lt;em&gt;that&lt;/em&gt; is a delicious PicklesController. But it’s not quite finished. If the &lt;code&gt;@pickle&lt;/code&gt; has any errors and can’t be created or updated, we’re still going to be returning a &lt;code&gt;200 OK&lt;/code&gt; response. That’s bad form. We can check if it has any errors and change the status accordingly.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;upsert&lt;/span&gt;
  &lt;span class="vi"&gt;@pickle&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;PickleRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create_or_update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:pickle&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;http_status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@pickle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;any?&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="ss"&gt;:unprocessable_entity&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="ss"&gt;:ok&lt;/span&gt;

  &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:pickle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;http_status&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Excellent. Now we’re returning a &lt;code&gt;422 Unprocessable Entity&lt;/code&gt; if the &lt;code&gt;@pickle&lt;/code&gt; has validation errors. But it’s still not quite finished. We’re returning a &lt;code&gt;200 OK&lt;/code&gt; if the resource is created. Although technically incorrect, that’s probably fine for most cases. But what if the client needs to know whether the request resulted in a resource being created or a resource being updated? Well there’s actually a different HTTP status code for that, &lt;code&gt;201 Created&lt;/code&gt;. So let’s just implement that and… crap, in the process of isolating persistence we’ve also removed our ability to determine if the object was saved or updated. What to do?&lt;/p&gt;

&lt;p&gt;After poring over the Rails docs for an hour or two I found that you can kind of introspect the state of an object after saving using &lt;code&gt;ActiveModel::Dirty#previous_changes&lt;/code&gt;. If &lt;code&gt;previous_changes&lt;/code&gt; includes a key named &lt;code&gt;id&lt;/code&gt;, it’s (probably) a new object and should return a &lt;code&gt;201 Created&lt;/code&gt;, otherwise it’s an update and should return a &lt;code&gt;200 OK&lt;/code&gt;. All together now.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;upsert&lt;/span&gt;
  &lt;span class="vi"&gt;@pickle&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;PickleRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create_or_update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:pickle&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;http_status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;HTTPStatusCode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@pickle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;from_model&lt;/span&gt;

  &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:pickle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;http_status&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# lib/http_status_code.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HTTPStatusCode&lt;/span&gt;

  &lt;span class="kp"&gt;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:model&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;from_model&lt;/span&gt;
    &lt;span class="n"&gt;unprocessable_entity&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;created&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="kp"&gt;private&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;unprocessable_entity&lt;/span&gt;
    &lt;span class="ss"&gt;:unprocessable_entity&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;any?&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;created&lt;/span&gt;
    &lt;span class="ss"&gt;:created&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;previous_changes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;include?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ok&lt;/span&gt;
    &lt;span class="ss"&gt;:ok&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/KjF1Zw0yWso" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>GitHub Is Your Resume Now</title>
    <link rel="alternate" href="/github-is-your-resume-now" />
    <id>/github-is-your-resume-now</id>
    <published>2012-06-17</published>
    <updated>2012-06-17</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;If you’re a programmer, the days of sending a &lt;abbr title="Curriculum Vitae"&gt;CV&lt;/abbr&gt; when you apply for a job are quickly coming to an end, if they haven’t already. No one really wants to see your poorly formatted, badly designed Microsoft Word template filled with irrelevant information you’ve carefully curated to make you look like the best programmer ever, least of all me. I just want to see what you’ve built and how you built it, and the best way to see that is by looking at your GitHub account.&lt;/p&gt;

&lt;p&gt;The problem is that while most programmers seem to understand and even embrace GitHub as the new resume, few if any seem to have actually put any thought or time into making their GitHub accounts look and work well as such.&lt;/p&gt;

&lt;p&gt;GitHub is more than just your social backup storage, it’s your &lt;em&gt;portfolio&lt;/em&gt; and it’s &lt;em&gt;public&lt;/em&gt;. You can’t just dump everything there willy-nilly or your account is going to look like shit, which it probably does.&lt;/p&gt;

&lt;p&gt;So here are some really obvious things that most people do wrong with their GitHub accounts. Hopefully it’ll help.&lt;/p&gt;

&lt;h2 id="too-many-forked-repositories"&gt;Too many forked repositories&lt;/h2&gt;

&lt;p&gt;There’s nothing more annoying than looking at someone’s GitHub account and seeing a bunch of forked repositories. If you have to maintain a fork for some reason then fine, sometimes that’s necessary. But most of the time you just make a fork to fix something, and then the changes get accepted upstream and you’re done. If that happens delete the repo! Why are you holding on to it? You can always re-fork it the next time you want to work on it. As a rule, if you have a forked repository its commit graph better have a ton of blue in it, otherwise it’s just noise in your account.&lt;/p&gt;

&lt;h2 id="sharing-anything-and-everything"&gt;Sharing anything and everything&lt;/h2&gt;

&lt;p&gt;GitHub is your &lt;em&gt;portfolio&lt;/em&gt;. A common mistake designers and artists make is adding anything and everything they’ve ever done to their portfolios, and then never getting rid of anything. They could do an entire spin-off of Hoarders based around creative peoples’ portfolios—it’s that bad. Don’t make the same mistake with your GitHub account. Be judicious about what you add; you have to curate your work, otherwise your signal-to-noise ratio drops.&lt;/p&gt;

&lt;h2 id="missing-or-unclear-descriptions"&gt;Missing or unclear descriptions&lt;/h2&gt;

&lt;p&gt;Every project needs a clear, concise description. I don’t want to have to read all your code in order to divine what your project does. If you can’t tell me in one sentence what your project does, then you probably don’t even understand the problem well-enough to be solving it.&lt;/p&gt;

&lt;h2 id="old-repositories"&gt;Old repositories&lt;/h2&gt;

&lt;p&gt;The work you did a year or more ago couldn’t possibly still be relevant today. If it is, you aren’t working hard enough or learning fast enough. Get rid of anything you haven’t worked on in over a year.&lt;/p&gt;

&lt;h2 id="mediocre-code"&gt;Mediocre code&lt;/h2&gt;

&lt;p&gt;Quantity never trumps quality. If I see a few projects with mediocre code I just extrapolate and assume &lt;em&gt;all&lt;/em&gt; the projects are mediocre and stop looking. I’d rather see two really good projects and nothing else than eight mediocre ones mixed in with three good ones. Only add projects to your account if you truly think it’s great or useful work.&lt;/p&gt;

&lt;hr&gt;
&lt;p&gt;Like it or not, GitHub is your resume now, and the projects you choose to show tell everything about you, especially where you are spending your time and how wisely. Don’t waste your time building unimportant things. Identify a serious problem and write some really good, well-tested code that makes it better. Then put it on GitHub so the world can see, keeping in mind the issues I just outlined. Take good care of your GitHub account and it will take good care of you.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/pEyPnmQtdAg" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>Static Sites on Heroku</title>
    <link rel="alternate" href="/static-sites-on-heroku" />
    <id>/static-sites-on-heroku</id>
    <published>2012-06-02</published>
    <updated>2012-06-02</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;I’ve been using &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt; to host my Ruby applications pretty much since day one. Not having to waste countless hours managing servers or dealing with crappy shared hosting control panels is fantastic and I can’t recommend it highly enough. But I’ve always had a number of static HTML sites, and I didn’t really know what to do with those, so I just kept them on old-school shared hosting. Then about a year ago I finally got fed up with having to FTP into my shared server to make changes—managing my static sites should be as easy as managing my web apps.&lt;/p&gt;

&lt;p&gt;My first thought was to use &lt;a href="http://sinatrarb.com"&gt;Sinatra&lt;/a&gt;, an awesome, lightweight Ruby application framework. But that’s way overkill to just serve some static HTML files. However, Sinatra is built on top of &lt;a href="https://github.com/rack/rack"&gt;Rack&lt;/a&gt;, a common interface for building Ruby applications. Ruby on Rails, Sinatra, and all other Ruby web frameworks use it. I realized I could just write a really simple Rack app to serve my various static sites.&lt;/p&gt;

&lt;p&gt;At its simplest, a Rack app is just an object that responds to a &lt;code&gt;call&lt;/code&gt; method, and returns an Array of three things. A status code, a Hash of HTTP headers, and an Array of Strings that make up the body. The Rack app that runs my &lt;a href="http://brandonweiss.me"&gt;personal site&lt;/a&gt; looks like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# Gemfile&lt;/span&gt;
&lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="s2"&gt;"http://rubygems.org"&lt;/span&gt;

&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s2"&gt;"rack"&lt;/span&gt;

&lt;span class="c1"&gt;# config.ru&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"rubygems"&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"bundler"&lt;/span&gt;
&lt;span class="no"&gt;Bundler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="s2"&gt;"/"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="ss"&gt;Rack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="ss"&gt;:Static&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"/assets"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Dir&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pwd&lt;/span&gt;

  &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="nb"&gt;lambda&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="s2"&gt;"Content-Type"&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"text/html"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s2"&gt;"Cache-Control"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"public, max-age=86400"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="no"&gt;Dir&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pwd&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/index.html"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;File&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="ss"&gt;:RDONLY&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;

    &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I put my images, stylesheets, and javascripts in the assets folder, toss an &lt;code&gt;index.html&lt;/code&gt; file in the root and that’s it.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/eBYE53i-7HI" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>The Fountain of Youth</title>
    <link rel="alternate" href="/the-fountain-of-youth" />
    <id>/the-fountain-of-youth</id>
    <published>2012-03-15</published>
    <updated>2012-03-15</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;I, and many smarter people before me have noted that time is the only truly limited resource. Everything else we can get more of, but time we can never get back. We’re using it literally all the time, and one day we’ve used it all up and we’re gone. As someone with a never-ending list of things to do that will surely extend beyond my alotted time, I find this rather unfortunate.&lt;/p&gt;

&lt;p&gt;Many would argue that this is the natural order of things and the way it should be. That it is time’s limited nature that makes life so great, and that death is a necessary, natural change agent. I’m inclined to agree, but still, no one wants to die, and given the option, most would surely stave it off for as long as possible. Hence humanity’s nearly-eternal search for the fountain of youth—in whatever form it might take—in order to gain more time.&lt;/p&gt;

&lt;p&gt;Personally I consider it a noble endeavor, and I’d like to join in the pursuit, but I’m no Spanish Conquistador, and despite being a programmer my science aptitude is tenuous at best, so those avenues are kind of out. But there is another way that seems to be largely unexplored, or at least I’ve never heard anyone else mention it. And that is using time’s relative nature to alter your own perception of it. It’s something we do every day, albeit unintentionally. The speed at which we perceive time ebbs and flows, and we notice it—we even point it out to other people after it’s happened—but we never really seem to pinpoint &lt;em&gt;why&lt;/em&gt; it happens. In order to do that, we have to look at when.&lt;/p&gt;

&lt;p&gt;The most obvious example is childhood. When you look back on it, you remember growing up seeming to take eons, but as you get older time seems to accelerate exponentially. This could of course just be something about the way children perceive time, which I think is the common wisdom. But there are times when adults perceive time the same way, most notably when on vacation. You can even just go away for the weekend and it can seem like you’ve been gone for two weeks. The only real common denominator between the two is new, or varied experiences. For children, everything is new all the time. As they get older, things become more familiar and repetetive, until finally they’re just doing the same thing day in and day out until they die. Which is why time seems to accelerate as you get older. The longer you do the same thing over and over again, the quicker time will fly by. Vacations are little pockets of varied experiences that interrupt the normal pattern of your life, which is why they seem to last way longer than they actually do.&lt;/p&gt;

&lt;p&gt;So what that means is that in order to slow down time, all you have to do is mix it up every single day; have lots of different experiences as often as you can. New restaurants, new clothes, new friends, new cities, new jobs. You don’t have to give up the old, but make sure you get a healthy dose of new. People will probably think you’re weird, but if they ask you about it, just tell them you’re trying to live forever.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/_G5j2-RJSho" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>Require Sprockets When Not Using ActiveRecord</title>
    <link rel="alternate" href="/require-sprockets-when-not-using-activerecord" />
    <id>/require-sprockets-when-not-using-activerecord</id>
    <published>2011-08-15</published>
    <updated>2011-08-15</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;If you use MongoMapper or Mongoid (or anything other than ActiveRecord) and recently upgraded to Rails &lt;code&gt;3.1.0.rc5&lt;/code&gt;, you may have noticed that the asset pipeline stopped working even thought it was working fine in &lt;code&gt;3.1.0.rc4&lt;/code&gt;. That’s because in &lt;code&gt;rc5&lt;/code&gt; the &lt;a href="https://github.com/rails/rails/commit/c690b7124d2f2206342d11aebb7aa3fc990046d2#railties/lib/rails/all.rb"&gt;&lt;code&gt;sprockets/railtie&lt;/code&gt; require is now explicit&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Just add &lt;code&gt;require 'sprockets/railtie'&lt;/code&gt; to your &lt;code&gt;config/application.rb&lt;/code&gt; file and you should be golden. Like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# config/application.rb&lt;/span&gt;
&lt;span class="c1"&gt;# require 'rails/all'&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'action_controller/railtie'&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'action_mailer/railtie'&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'active_resource/railtie'&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'sprockets/railtie'&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/zFGUl2K6Dq4" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>Suppressing Notice Messages with PostgreSQL on Rails</title>
    <link rel="alternate" href="/suppressing-notice-messages-with-postgresql-on-rails" />
    <id>/suppressing-notice-messages-with-postgresql-on-rails</id>
    <published>2011-08-04</published>
    <updated>2011-08-04</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;If you’re using SQL, then you’re probably using PostgreSQL. And if you’re using PostgreSQL, then you probably get those annoying as hell notice messages outputted everywhere when you do anything that alters the database schema. They look something like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;NOTICE:  CREATE TABLE will create implicit sequence "groups_id_seq" for serial column "groups.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "groups_pkey" for table "groups"
NOTICE:  CREATE TABLE will create implicit sequence "projects_id_seq" for serial column "projects.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "projects_pkey" for table "projects"
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;So annoying&lt;/em&gt;. And the more tables your project has the more annoying it gets. I first noticed it a year or so ago and have been half-assedly trying to fix it ever since. Google suggests putting various combinations of  &lt;code&gt;SET client_min_messages TO WARNING;&lt;/code&gt; in a &lt;code&gt;.psqlrc&lt;/code&gt; file, but no matter what I tried I could never get it to work, so I always gave up.&lt;/p&gt;

&lt;p&gt;Until today, when I finally got so fed up with it that I decided I wouldn’t do anything else until I figured it out. Three hours later I got it, kind of.&lt;/p&gt;

&lt;p&gt;What eventually worked was setting &lt;code&gt;min_messages&lt;/code&gt; in the &lt;code&gt;database.yml&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# config/database.yml&lt;/span&gt;
&lt;span class="l-Scalar-Plain"&gt;defaults&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="nl"&gt;&amp;amp;defaults&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;adapter&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="l-Scalar-Plain"&gt;postgresql&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;encoding&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="l-Scalar-Plain"&gt;unicode&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;min_messages&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="l-Scalar-Plain"&gt;warning&lt;/span&gt;

&lt;span class="l-Scalar-Plain"&gt;development&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;*defaults&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;database&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="l-Scalar-Plain"&gt;something_development&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;username&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;password&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;pool&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="l-Scalar-Plain"&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It’s not ideal, I would have rather it been something global that I could put in my dotfiles than have it per project, but this will do for now.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/FgvlcW8dGfE" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>Sinatra Gotcha</title>
    <link rel="alternate" href="/sinatra-gotcha" />
    <id>/sinatra-gotcha</id>
    <published>2011-06-19</published>
    <updated>2011-06-19</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;You know how they always say to not forget to include a &lt;code&gt;favicon&lt;/code&gt; and &lt;code&gt;robots.txt&lt;/code&gt; and whatnot in your website or app so browsers and bots don’t fill up your error logs with 404’s? Yeah, well, don’t forget that if you have a catch-all route in your app and those resources don’t exist they’re not going to 404, they’re going to mysteriously throw a 500 on nearly every request.&lt;/p&gt;

&lt;p&gt;I know you wouldn’t make a stupid, obvious mistake like that and spend twenty minutes staring at your error logs trying to figure out what the hell is going on. But I figured I should mention it anyways. You know, just in case.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/dE2PzJZA01o" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>Adjusting Your Privates in Public</title>
    <link rel="alternate" href="/adjusting-your-privates-in-public" />
    <id>/adjusting-your-privates-in-public</id>
    <published>2011-05-29</published>
    <updated>2011-05-29</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;The title is a stretch—I know—but I couldn’t resist.&lt;/p&gt;

&lt;hr&gt;
&lt;p&gt;Like most programmers I have a fairly strong opinion about how code should be indented and formatted. And while much of formatting is subjective and comes down to personal preference, there are many times when one could probably make a solid, logical argument as to why one particular method is better than the others.&lt;/p&gt;

&lt;p&gt;I think one such instance would be the handling of &lt;code&gt;private&lt;/code&gt; and &lt;code&gt;protected&lt;/code&gt; declarations in classes and modules. Here are the methods I’ve seen:&lt;/p&gt;

&lt;h2 id="method-1"&gt;Method 1&lt;/h2&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Stuff&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;something_public&lt;/span&gt;
    &lt;span class="c1"&gt;#&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="kp"&gt;private&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;something_private&lt;/span&gt;
      &lt;span class="c1"&gt;#&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is, in my opinion, the most awful. It’s easy to see where the private methods start, but something just feels very off. Having some methods indented once and others indented twice feels more inconsistent than helpful. And in a long file if nothing is currently visible except twice-indented methods, with no other points of reference it might not be evident that the methods are in fact twice-indented, in which case they may as well not be.&lt;/p&gt;

&lt;h2 id="method-2"&gt;Method 2&lt;/h2&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Stuff&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;something_public&lt;/span&gt;
    &lt;span class="c1"&gt;#&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="kp"&gt;private&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;something_private&lt;/span&gt;
    &lt;span class="c1"&gt;#&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is by far the most consistent. Everything is only indented once, which makes it fairly easy to parse. But when scrolling through a long file the &lt;code&gt;private&lt;/code&gt; declaration blends right in.&lt;/p&gt;

&lt;h2 id="method-3"&gt;Method 3&lt;/h2&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Stuff&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;something_public&lt;/span&gt;
    &lt;span class="c1"&gt;#&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="kp"&gt;private&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;something_private&lt;/span&gt;
    &lt;span class="c1"&gt;#&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is the one I use. I believe it combines the best of both other methods. Everything is indented once for easy visual parsing, but the &lt;code&gt;private&lt;/code&gt; declaration is left flush with the edge of the document to provide a nice visual break when scrolling through a long file.&lt;/p&gt;

&lt;p&gt;If you’re of a different mind than I on the method to use and/or the reasoning behind it I’d love to &lt;a href="mailto:brandon@anti-pattern.com"&gt;hear&lt;/a&gt; from you.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/MJWEQcxJad0" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>Case-insensitive Keys with Devise</title>
    <link rel="alternate" href="/case-insensitive-keys-with-devise" />
    <id>/case-insensitive-keys-with-devise</id>
    <published>2011-05-16</published>
    <updated>2011-05-16</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;If you use &lt;a href="https://github.com/plataformatec/devise"&gt;Devise&lt;/a&gt; for authentication and deploy on &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt;, or really just use any database that is case-sensitive by default (like &lt;a href="https://postgresql.org"&gt;PostgreSQL&lt;/a&gt;), you’ve probably run into the problem that some users like to type certain letters in their logins (email and/or username) in uppercase, but expect it to be case-insensitive when they try to sign in. A not unreasonable request considering that’s pretty much the universally default behavior that most applications exhibit, but that’s not the default with Devise.&lt;/p&gt;

&lt;p&gt;Unfortunately, if you google the problem, the most common solution given is to &lt;code&gt;downcase&lt;/code&gt; the login in a &lt;code&gt;before_save&lt;/code&gt; callback so that it’s always lowercase in the database, and then overriding like eight different methods on several different controllers to &lt;code&gt;downcase&lt;/code&gt; the login in the &lt;code&gt;params&lt;/code&gt;. Thus always comparing a lowercase submitted login to a lowercase login in the database. Besides being annoyingly overcomplicated, it strips users of their ability to capitalize their login as they so choose, which is the whole reason this problem exists in the first place.&lt;/p&gt;

&lt;p&gt;Devise should really have a way to do this automatically, and it does as of a few months ago in version &lt;code&gt;1.2&lt;/code&gt; and later, although the way their wiki is set up makes it difficult to find. Here it is in all its one-line glory.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# config/initializers/devise.rb&lt;/span&gt;
&lt;span class="no"&gt;Devise&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setup&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;case_insensitive_keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:username&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Maybe this will push the old/incorrect answers out of the search results.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/tikCMfSkzS8" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>Adding a New Strategy to OmniAuth</title>
    <link rel="alternate" href="/adding-a-new-strategy-to-omniauth" />
    <id>/adding-a-new-strategy-to-omniauth</id>
    <published>2011-04-24</published>
    <updated>2011-04-24</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;Recently I had to authenticate with a service (&lt;a href="http://rdio.com"&gt;Rdio&lt;/a&gt;) that &lt;a href="https://github.com/intridea/omniauth"&gt;OmniAuth&lt;/a&gt; didn’t support yet. I’d never created an authentication strategy before, so I looked up how to do it in the &lt;a href="https://github.com/intridea/omniauth/wiki"&gt;wiki&lt;/a&gt;. The supposed &lt;a href="https://github.com/intridea/omniauth/wiki/Strategy-Contribution-Guide"&gt;Strategy Contribution Guide&lt;/a&gt; was rather… sparse, so I had to roll up my sleeves and figure it out.&lt;/p&gt;

&lt;p&gt;First, you need to set up an environment to work on the gem. So fork the &lt;a href="https://github.com/intridea/omniauth"&gt;official repo&lt;/a&gt; and then clone your fork. If you don’t have &lt;a href="http://gembundler.com"&gt;bundler&lt;/a&gt;, install it, then &lt;code&gt;bundle install&lt;/code&gt; from inside your cloned repo.&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" rel="footnote"&gt;1&lt;/a&gt;&lt;/sup&gt; Run the tests with &lt;code&gt;bundle exec rake spec&lt;/code&gt; and you should see all green.&lt;/p&gt;

&lt;p&gt;OmniAuth supports multiple different authentication schemes, but &lt;a href="http://rdio.com"&gt;Rdio&lt;/a&gt; thankfully uses &lt;a href="http://oauth.net"&gt;OAuth&lt;/a&gt;. Each authentication scheme has a separate gem/directory, so I looked in &lt;code&gt;oa-oauth/lib/omniauth/strategies&lt;/code&gt; to see how other OAuth strategies had been implemented. OAuth has a few different versions now, so make sure you’re looking at examples that use the same OAuth version as the API you’re trying to connect to supports. Rdio uses OAuth 1.0, so those are the strategies I looked at.&lt;/p&gt;

&lt;p&gt;I started by copying the module/class structure from another strategy into a new file and renaming the class where appropriate.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# oa-oauth/lib/omniauth/strategies/rdio.rb&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'omniauth/oauth'&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'multi_json'&lt;/span&gt;

&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;OmniAuth&lt;/span&gt;
  &lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Strategies&lt;/span&gt;
    &lt;span class="c1"&gt;#&lt;/span&gt;
    &lt;span class="c1"&gt;# Authenticate to Rdio via OAuth and retrieve basic user information.&lt;/span&gt;
    &lt;span class="c1"&gt;# Usage:&lt;/span&gt;
    &lt;span class="c1"&gt;#    use OmniAuth::Strategies::Rdio, 'consumerkey', 'consumersecret'&lt;/span&gt;
    &lt;span class="c1"&gt;#&lt;/span&gt;
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rdio&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="ss"&gt;OmniAuth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="ss"&gt;:Strategies&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;OAuth&lt;/span&gt;

    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then I started looking for other similarities among the various strategies. There’s a fair bit of variance in which methods are implemented and the naming of variables, but the first method that all strategies had in common was &lt;code&gt;initialize&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# oa-oauth/lib/omniauth/strategies/rdio.rb&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumer_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumer_secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;opts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="ss"&gt;:site&lt;/span&gt;               &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"http://api.rdio.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;:request_token_path&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"/oauth/request_token"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;:access_token_path&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"/oauth/access_token"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;:authorize_url&lt;/span&gt;      &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"https://www.rdio.com/oauth/authorize"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:rdio&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumer_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumer_secret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I was lucky and the paths were very clearly listed in the API docs. All I did here was switch out the paths and change the second argument passed to &lt;code&gt;super&lt;/code&gt; to &lt;code&gt;:rdio&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The next common method was &lt;code&gt;auth_hash&lt;/code&gt;. The &lt;a href="https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema"&gt;auth_hash&lt;/a&gt; is the data that is returned after you successfully authenticate with a service. It has info like the provider, the provider-specific id, name, username, etc.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# oa-oauth/lib/omniauth/strategies/rdio.rb&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;auth_hash&lt;/span&gt;
  &lt;span class="ss"&gt;OmniAuth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="ss"&gt;:Utils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deep_merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s1"&gt;'uid'&lt;/span&gt;       &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;user_hash&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'key'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'user_info'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;user_info&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'extra'&lt;/span&gt;     &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'user_hash'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;user_hash&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;user_info&lt;/span&gt;
  &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user_hash&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s1"&gt;'nickname'&lt;/span&gt;   &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'username'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'first_name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'firstName'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'last_name'&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'lastName'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'name'&lt;/span&gt;       &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'firstName'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'lastName'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;user_hash&lt;/span&gt;
  &lt;span class="vi"&gt;@user_hash&lt;/span&gt; &lt;span class="o"&gt;||=&lt;/span&gt; &lt;span class="no"&gt;MultiJson&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@access_token&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"http://api.rdio.com/1/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="ss"&gt;:method&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'currentUser'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;:extras&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'username'&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'result'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This part required a bit of trial and error, because although the docs indicate what fields are required, they don’t say which are returned automatically and which have to be retrieved from the provider’s API. Eventually I figured out that &lt;code&gt;provider&lt;/code&gt; and &lt;code&gt;credentials&lt;/code&gt; are automatically set, everything else has to be retrieved from the API. Look in the API docs for the specific request that will return information about the current user. Hopefully the docs are good and/or they have an API explorer so you can play around with it and see exactly what data is returned and in what format it’s in.&lt;/p&gt;

&lt;p&gt;Once you’ve got that figured out, map the fields returned to their appropriate values in the &lt;code&gt;auth_hash&lt;/code&gt;. A fairly common pattern is to also just stick the whole hash returned under &lt;code&gt;extra&lt;/code&gt;, in case there were some extra fields that couldn’t be mapped.&lt;/p&gt;

&lt;p&gt;There are several other methods that some strategies implement, but after more trial and error it seems like the only two methods that absolutely have to be implemented are &lt;code&gt;initialize&lt;/code&gt; and &lt;code&gt;auth_hash&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now make sure your new strategy is loaded.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# oa-oauth/lib/omniauth/oauth.rb&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'omniauth/core'&lt;/span&gt;

&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;OmniAuth&lt;/span&gt;
  &lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Strategies&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;

    &lt;span class="nb"&gt;autoload&lt;/span&gt; &lt;span class="ss"&gt;:Rdio&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'omniauth/strategies/rdio'&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Create a test for it.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# oa-oauth/spec/omniauth/strategies/rdio_spec.rb&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expand_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'../../../spec_helper'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;__FILE__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;describe&lt;/span&gt; &lt;span class="ss"&gt;OmniAuth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="ss"&gt;:Strategies&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Rdio&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;it_should_behave_like&lt;/span&gt; &lt;span class="s2"&gt;"an oauth strategy"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Update the readme.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# README.markdown&lt;/span&gt;
&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;Rdio&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;via&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;brandonweiss&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="sr"&gt;//&lt;/span&gt;&lt;span class="n"&gt;github&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;brandonweiss&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And you’re done. Now you’ll probably want to try it out to make sure it works. The easiest way is to just open up the project that’s going to use the new strategy and edit the &lt;code&gt;Gemfile&lt;/code&gt; to look for &lt;code&gt;omniauth&lt;/code&gt; locally.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# Gemfile&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'omniauth'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:path&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'~/Code/omniauth'&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;bundle install&lt;/code&gt; then start the server and you should be good to go. If you messed something up, fix it, then restart the server to pick up the changes.&lt;/p&gt;

&lt;p&gt;OmniAuth is a really amazing gem. Hopefully this will make it easier for people to add more strategies.&lt;/p&gt;

&lt;div class="footnotes"&gt;
  &lt;ol&gt;
&lt;li id="fn:1"&gt;
      &lt;p&gt;Make sure you have the latest version of Bundler; I was on 1.0.7 and ran into gem version conflict errors. The fix was to upgrade to 1.0.12.&lt;a href="#fnref:1" rel="reference"&gt;↩&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/wR4hyfIDXvE" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>rack-pagespeed on Heroku</title>
    <link rel="alternate" href="/rack-pagespeed-on-heroku" />
    <id>/rack-pagespeed-on-heroku</id>
    <published>2011-04-16</published>
    <updated>2011-04-16</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt; Use &lt;code&gt;memcached&lt;/code&gt;, not disk storage&lt;/p&gt;

&lt;p&gt;A few weeks ago I heard about &lt;a href="http://rack-pagespeed.heroku.com"&gt;rack-pagespeed&lt;/a&gt; and decided to use it on this blog. A week later I was randomly checking out the site and found it completely unstyled. The CSS file that &lt;code&gt;rack-pagespeed&lt;/code&gt; had generated was throwing a 404. I scratched my head a bit, asked on GitHub and realized that I’d stupidly been using the &lt;code&gt;tmp&lt;/code&gt; directory for storage. It’s called a temp directory for a reason, and &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt; was flushing it out periodically, hence why it looked fine after a deploy but broke a little while later.&lt;/p&gt;

&lt;p&gt;The solution is to use a persistent cache like &lt;code&gt;memcached&lt;/code&gt;. Depending on the size of your site and the amount of CSS/JS you have, this might not really be worth it, but assuming it is, here’s how you do it.&lt;/p&gt;

&lt;p&gt;First install &lt;code&gt;memcached&lt;/code&gt; locally using &lt;a href="https://github.com/mxcl/homebrew"&gt;Homebrew&lt;/a&gt;&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" rel="footnote"&gt;1&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ brew install memcached
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then just specify the store as &lt;code&gt;memcached&lt;/code&gt;. Heroku can sometimes be finicky about relative paths, so I like to be really explicit and set the root of the app to the current directory, then build off that. &lt;code&gt;settings&lt;/code&gt; is just a class method on the rack app, so you can access it from outside the app&lt;sup id="fnref:2"&gt;&lt;a href="#fn:2" rel="footnote"&gt;2&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# minimalog.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Minimalog&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="ss"&gt;Sinatra&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="ss"&gt;:Base&lt;/span&gt;
  &lt;span class="n"&gt;set&lt;/span&gt; &lt;span class="ss"&gt;:root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;__FILE__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# config.ru&lt;/span&gt;
&lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="ss"&gt;Rack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="ss"&gt;:PageSpeed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:public&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="no"&gt;Minimalog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/public"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="ss"&gt;:memcached&lt;/span&gt;
  &lt;span class="n"&gt;combine_css&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Redeploy to Heroku and everything should be good.&lt;/p&gt;

&lt;div class="footnotes"&gt;
  &lt;ol&gt;
&lt;li id="fn:1"&gt;
      &lt;p&gt;If you don’t use Homebrew I feel bad for you son, I’ve got 99 problems but manually building packages myself isn’t one.&lt;a href="#fnref:1" rel="reference"&gt;↩&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id="fn:2"&gt;
      &lt;p&gt;This specific example is for &lt;a href="http://www.sinatrarb.com"&gt;Sinatra&lt;/a&gt;, but it’s very similar for Rails.&lt;a href="#fnref:2" rel="reference"&gt;↩&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/T7rbjDrTq7U" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>Maintaining Complex Systems</title>
    <link rel="alternate" href="/maintaining-complex-systems" />
    <id>/maintaining-complex-systems</id>
    <published>2011-04-04</published>
    <updated>2011-04-04</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;&lt;p&gt;I’ll never understand why more nerds don’t go to the gym and exercise regularly. It’s all about keeping the ultimate system performant. Isn’t that the nerdiest possible endeavor?&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/48O407KhVWc" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>Dirty Associations with ActiveRecord</title>
    <link rel="alternate" href="/dirty-associations-with-activerecord" />
    <id>/dirty-associations-with-activerecord</id>
    <published>2011-03-22</published>
    <updated>2011-03-22</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;Imagine you have a system in which it’s advantageous to know when an object was last updated. Perhaps it’s just useful for users to know how fresh the data is, or maybe some operations need to run only on recently updated objects.&lt;/p&gt;

&lt;p&gt;With ActiveRecord this is really simple, because it automatically looks for &lt;code&gt;DateTime&lt;/code&gt; fields called &lt;code&gt;created_at&lt;/code&gt; and &lt;code&gt;updated_at&lt;/code&gt;, and if they exist will update them when an object is created and updated, respectively. But what about associations?&lt;/p&gt;

&lt;p&gt;For example, if a &lt;code&gt;lolrus&lt;/code&gt; has and belongs to many &lt;code&gt;buckets&lt;/code&gt;, and you add a new &lt;code&gt;bucket&lt;/code&gt; to that &lt;code&gt;lolrus&lt;/code&gt;, has that &lt;code&gt;lolrus&lt;/code&gt; then been updated? From a framework perspective, no. The &lt;code&gt;lolrus&lt;/code&gt; object never changed, nor did the &lt;code&gt;bucket&lt;/code&gt; object, just a new relation was created between them. No objects were updated. But from a user’s perspective, yes, they updated information on the &lt;code&gt;lolrus&lt;/code&gt;, so the &lt;code&gt;updated_at&lt;/code&gt; field on the &lt;code&gt;lolrus&lt;/code&gt; &lt;em&gt;should&lt;/em&gt; reflect the current data and time. But it won’t.&lt;/p&gt;

&lt;p&gt;When you update fields on a model, those changes are tracked in a hash. And when a model is saved, it calls a method called &lt;code&gt;changed?&lt;/code&gt;, which checks to see whether or not the hash is empty. If it’s empty, ostensibly nothing changed, and so the &lt;code&gt;updated_at&lt;/code&gt; timestamp is left alone. If it’s not empty, then one or more attributes changed and the &lt;code&gt;updated_at&lt;/code&gt; is set to the current time. But associations are &lt;strong&gt;not&lt;/strong&gt; tracked in this hash, so modifying them will not flag a model as being dirty, and thus the timestamp will never be updated. This isn’t a bug—it’s working as intended—but in certain situations that behavior is not going to be what the user expects.&lt;/p&gt;

&lt;p&gt;So if you want to update the timestamp of an object when the associations change as well, here’s how you do it:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# app/models/dirty_associations.rb&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;DirtyAssociations&lt;/span&gt;
  &lt;span class="kp"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:dirty&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_dirty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dirty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;changed?&lt;/span&gt;
    &lt;span class="n"&gt;dirty&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;super&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# app/models/lolrus.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Lolrus&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;DirtyAssociations&lt;/span&gt;

  &lt;span class="n"&gt;has_and_belongs_to_many&lt;/span&gt; &lt;span class="ss"&gt;:buckets&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                          &lt;span class="ss"&gt;:after_add&lt;/span&gt;    &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;:make_dirty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                          &lt;span class="ss"&gt;:after_remove&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;:make_dirty&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This works because callbacks can be defined on ActiveRecord associations. When an object is added or removed from the collection, the &lt;code&gt;make_dirty&lt;/code&gt; callback will be triggered and a virtual attribute representing whether or not the object is dirty will get set to &lt;code&gt;true&lt;/code&gt;. Then when &lt;code&gt;changed?&lt;/code&gt; is called to check whether an object is dirty, it will check the virtual attribute first before deferring to &lt;code&gt;super&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Mad props to &lt;a href="http://twitter.com/timgaleckas"&gt;Tim Galeckas&lt;/a&gt; for helping me sort this one out. You don’t even want to know the janky shit I had half-written to solve this.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/cJeX034T-XM" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>You Had Better Be Able to Answer These Questions in an Interview</title>
    <link rel="alternate" href="/you-had-better-be-able-to-answer-these-questions-in-an-interview" />
    <id>/you-had-better-be-able-to-answer-these-questions-in-an-interview</id>
    <published>2011-01-21</published>
    <updated>2011-01-21</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;Interviewing isn’t something I have to do often, but I’ve had to do it enough times that I’ve been able to formulate a good set of questions to ask a potential candidate. The questions are not particularly crazy or complex—they’re actually a lot simpler than most of the questions I’ve been asked in interviews—but together they easily weed out people who aren’t amazing. A truly skilled person should have no problem answering them, but they’ll be impossible for anyone else.&lt;/p&gt;

&lt;p&gt;The reason these questions work is there’s no way to study for them. They can’t be memorized or planned for because they don’t really have ‘right’ answers. You either have &lt;em&gt;a&lt;/em&gt; cogent answer or you don’t. And the only way you could come up with a good answer that makes sense is if you are the right type of person with the right type of knowledge to begin with. It’s almost always immediately evident whether I’m talking to the right person or not.&lt;/p&gt;

&lt;p&gt;If you’re looking to hire someone, try asking candidates these questions and see what they say. Even if you aren’t necessarily knowledgeable about the position you’re hiring for, you should be able to tell if they’re a good fit by how much they have to say about each one. If the person is worth their salt you won’t be able to get them to shut up.&lt;/p&gt;

&lt;p&gt;And if you’re a person looking to be hired, this is an excellent way of finding out if you’re in the right field or not. If you can’t answer all of these questions and answer them well, you either need to step up your game or find something else to do, because I may have written these up but they aren’t just &lt;em&gt;my&lt;/em&gt; questions. Any good person or company will probably ask some variant of these when interviewing you, and if you can’t answer them it’s going to be dead obvious that you’re either not passionate about your field or just don’t have what it takes to compete in it.&lt;/p&gt;

&lt;p&gt;So without further ado, the questions&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" rel="footnote"&gt;1&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;

&lt;hr&gt;
&lt;dl&gt;
&lt;dt&gt;Who are your heroes?&lt;/dt&gt;
  &lt;dd&gt;Everyone has someone they look up to in their field. Leaders, luminaries, people who are pushing their craft forward. If you can’t name at least five people and be able to talk at length about what you’re learning from them then that tells me that you’re not really &lt;em&gt;in&lt;/em&gt; your field. It would be like an electrical engineer in the 19th century not knowing who &lt;a href="http://en.wikipedia.org/wiki/Nikola_Tesla"&gt;Nikola Tesla&lt;/a&gt; was.&lt;/dd&gt;
  &lt;dt&gt;Who do you follow? What do you read?&lt;/dt&gt;
  &lt;dd&gt;This is somewhat related to the last question, but it’s not enough to just know of some famous people in your field. You need to be constantly reading and learning from them, but also others, too. If you tell me you read the top five blogs in your field, that’s great, but that tells me you only know exactly &lt;em&gt;what&lt;/em&gt; everyone else knows, &lt;em&gt;when&lt;/em&gt; everyone else knows it. Someone who is really entrenched in their field will have spent the time to find good, obscure sources that most people don’t know about.&lt;/dd&gt;
  &lt;dt&gt;Who have you collaborated with?&lt;/dt&gt;
  &lt;dd&gt;This demonstrates three things. One, that you’re part of the community and know your peers. Two, that you’re skilled enough for your peers to recognize it and want to work with you. And three, that you’re passionate enough about your field to want to do it outside of work.&lt;/dd&gt;
  &lt;dt&gt;What are you working on personally?&lt;/dt&gt;
  &lt;dd&gt;This is commonly a taboo topic. It’s considered a big faux pas to speak of plans beyond a company, lest you be perceived as disinterested or uncommitted. But a good employer will want employees that have dreams and aspirations beyond working for them. Logically you would think a company would want to hire people that will stay forever, but usually the qualities these people possess are the exact opposite of what a good employer should want. People who have the drive to be something more than just another employee &lt;em&gt;will&lt;/em&gt; inevitably leave, but during their stay they will probably do better work and be better for the company than your average lifer. Having one or more side projects underway is a good sign you’re that type of person.&lt;/dd&gt;
  &lt;dt&gt;What are you currently learning?&lt;/dt&gt;
  &lt;dd&gt;You should always, always, always be learning. Both in your field and outside of it. You should be perpetually overwhelmed with the amount of stuff you still have yet to learn. There should never be a time in which you’ve learned everything there is to learn about anything. If you’ve stopped learning things in your field and are content to keep doing the same things you’ve been doing, then you’ve already been left behind and just aren’t aware of it yet.&lt;/dd&gt;
  &lt;dt&gt;What do you do outside of work?&lt;/dt&gt;
  &lt;dd&gt;This is basically a gimme; everyone does &lt;em&gt;something&lt;/em&gt; outside of work. But what they answer tells me how interesting the person is, whether or not I’d get along with them and most importantly whether or not they challenge themselves, as that’s going to be a good indicator of if they’re going to challenge themselves at work. So if you collect stamps or scrapbook that’s cool, but I’m looking for someone who’s learning to arc weld and trying to become a master surfer.&lt;/dd&gt;
&lt;/dl&gt;
&lt;div class="footnotes"&gt;
  &lt;ol&gt;
&lt;li id="fn:1"&gt;
      &lt;p&gt;I’m a web developer, so I came up with these questions with my field in mind, but none of them are really field-specific. They’re all agnostic enough that they should apply to any job in any field that requires creative thought.&lt;a href="#fnref:1" rel="reference"&gt;↩&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/FhortMJetBg" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>Bundler.setup vs. Bundler.require</title>
    <link rel="alternate" href="/bundler-setup-vs-bundler-require" />
    <id>/bundler-setup-vs-bundler-require</id>
    <published>2010-12-20</published>
    <updated>2010-12-20</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; I’ve changed my opinion. &lt;a href="http://anti-pattern.com/use-bundler-setup-instead-of-bundler-require"&gt;Use Bundler.setup instead of Bundler.require&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt; Use &lt;code&gt;Bundler.require&lt;/code&gt; instead of &lt;code&gt;Bundler.setup&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I think pretty much everyone knows that when you use Bundler with Rails 3, you don’t have to require your gems manually; it’s just all taken care of when you define them in the &lt;code&gt;Gemfile&lt;/code&gt;. But I think a lot of people don’t understand exactly how this works, because when I see Bundler used &lt;em&gt;outside&lt;/em&gt; of Rails, I consistently see gems being defined in the &lt;code&gt;Gemfile&lt;/code&gt; and then manually required in the app.&lt;/p&gt;

&lt;p&gt;I’m guessing this is because in the &lt;a href="http://gembundler.com"&gt;Bundler docs&lt;/a&gt; the most &lt;a href="http://gembundler.com/bundler_setup.html"&gt;prominent example&lt;/a&gt; tells you to use &lt;code&gt;Bundler.setup&lt;/code&gt;, and so people assume the auto-requiring is part of Rails and do this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# Gemfile&lt;/span&gt;
&lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="s1"&gt;'http://rubygems.org'&lt;/span&gt;

&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'sinatra'&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'haml'&lt;/span&gt;

&lt;span class="c1"&gt;# config.ru&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'rubygems'&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'bundler'&lt;/span&gt;
&lt;span class="no"&gt;Bundler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'sinatra'&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'sinatra/base'&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'haml'&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But actually it’s all Bundler. Instead of &lt;code&gt;Bundler.setup&lt;/code&gt; you can call &lt;code&gt;Bundler.require&lt;/code&gt;, which will not only set up the load paths but also require the gems as well. This is how Rails automatically requires the gems you specify. So the best way to do it (in most situations) is like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# Gemfile&lt;/span&gt;
&lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="s1"&gt;'http://rubygems.org'&lt;/span&gt;

&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'sinatra'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:require&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'sinatra/base'&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'haml'&lt;/span&gt;

&lt;span class="c1"&gt;# config.ru&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'rubygems'&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'bundler'&lt;/span&gt;
&lt;span class="no"&gt;Bundler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that you can specify a different name to require a gem by if the file name happens to be different from the gem name. And, as an added bonus, you can pass in multiple groups to &lt;code&gt;Bundler.require&lt;/code&gt;, so you could also include gems specific to the current environment like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="no"&gt;Bundler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:default&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'RACK_ENV'&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;to_sym&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/IGdHwEQJmI8" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>Being a Good Jedi</title>
    <link rel="alternate" href="/being-a-good-jedi" />
    <id>/being-a-good-jedi</id>
    <published>2010-12-15</published>
    <updated>2010-12-15</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;
&lt;p&gt;A few weeks ago I read a blog post by Rob Conery called &lt;a href="http://blog.wekeroad.com/blog/be-a-good-jedi-build-your-own-blog/"&gt;Be a Good Jedi: Build Your Own Blog&lt;/a&gt;. If you’ve been trying and failing to start blogging I suggest you read it. I’d been trying to get a blog up and running for the past several years, but I hadn’t been able to do it because I’ve never been happy with any of the blogging engines I’ve tried. And it wasn’t for lack of options; I’ve tried &lt;a href="http://wordpress.org"&gt;WordPress&lt;/a&gt;, &lt;a href="http://drupal.org"&gt;Drupal&lt;/a&gt;, &lt;a href="http://mephistoblog.com"&gt;Mephisto&lt;/a&gt;, &lt;a href="http://github.com/fdv/typo"&gt;Typo&lt;/a&gt;, &lt;a href="http://symphony-cms.com"&gt;Symphony&lt;/a&gt;, &lt;a href="http://enkiblog.com"&gt;Enki&lt;/a&gt;, &lt;a href="http://jekyllrb.com"&gt;Jekyll&lt;/a&gt; and &lt;a href="http://cloudhead.io/toto"&gt;toto&lt;/a&gt;. But every single one of those fell short in some way. There was a steady progression towards minimalism, eventually moving away from database-backed engines altogether with Jekyll and &lt;a href="http://cloudhead.io/toto"&gt;toto&lt;/a&gt;, but even the most minimalist engines didn’t quite work how I wanted, or weren’t extensible in the way I needed. Eventually I realized I’d just need to build my own, but my time was in extra short supply those days and the whole thing got shelved.&lt;/p&gt;

&lt;p&gt;Until reading Rob Conery’s blog post jump-started it again. And so after a couple nights of hacking I have this. I’m calling it minimalog (the engine, not the blog itself). It’s a git-based &lt;a href="http://www.sinatrarb.com"&gt;Sinatra&lt;/a&gt; app that parses &lt;a href="http://daringfireball.net/projects/markdown/"&gt;markdown&lt;/a&gt; files. That’s it. It’s a lot like &lt;a href="http://cloudhead.io/toto"&gt;toto&lt;/a&gt;, except it does all the things that I couldn’t get toto to do, and it’s a lot less complex. It’s not open-source currently, but at some point I plan to abstract it into a gem, at which point I’ll release it.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/lzqS8YGIDKQ" height="1" width="1"/&gt;</content>
  </entry>
  <entry>
    <title>First Post!!!1</title>
    <link rel="alternate" href="/first-post" />
    <id>/first-post</id>
    <published>2010-12-14</published>
    <updated>2010-12-14</updated>
    <author>
      <name>Brandon Weiss</name>
    </author>
    <content type="html">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;&lt;p&gt;Have to start somewhere.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;
&lt;img src="http://feeds.feedburner.com/~r/anti-pattern/~4/C9V1_dVkwPE" height="1" width="1"/&gt;</content>
  </entry>
</feed>
