<?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>Andy Lindeman</title>
 
 <link href="http://www.andylindeman.com/" />
 <updated>2013-05-07T19:42:11-07:00</updated>
 <id>http://www.andylindeman.com/</id>
 <author>
   <name>Andy Lindeman</name>
   <email>alindeman@gmail.com</email>
 </author>

 
 <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/alindeman" /><feedburner:info uri="alindeman" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
   <title>My Assets Are No Longer Minified in Rails 4: HALP!</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/otcp95MUyQM/my-assets-are-no-longer-compressed-in-rails-4-halp.html" />
   <updated>2013-05-08T00:00:00-07:00</updated>
   <id>http://www.andylindeman.com/2013/05/08/my-assets-are-no-longer-compressed-in-rails-4-halp</id>
   <content type="html">&lt;p&gt;Rails 4.0 and sprockets no longer heed the &lt;code&gt;config.assets.compress&lt;/code&gt; directive.
Instead, the JavaScript and CSS compressor must be specified explicitly with
&lt;code&gt;config.assets.js_compressor&lt;/code&gt; and &lt;code&gt;config.assets.css_compressor&lt;/code&gt;,
respectively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No error or deprecation warning is raised: assets will simply not be
minified if the Rails 3 &lt;code&gt;config.assets.compress = true&lt;/code&gt; directive is left
intact.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Unminified assets may cause pages to load more sluggishly.&lt;/p&gt;

&lt;p&gt;When upgrading a Rails 3 application to Rails 4, make sure to adjust
&lt;code&gt;config/environments/production.rb&lt;/code&gt; as shown:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="ss"&gt;Widgets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="ss"&gt;:Application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;configure&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;# ...&lt;/span&gt;

  &lt;span class="c1"&gt;# Compress JavaScripts and CSS&lt;/span&gt;
  &lt;span class="c1"&gt;# config.assets.compress = true &amp;lt;- remove the Rails 3 setting&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;assets&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;js_compressor&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ss"&gt;:uglifier&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;assets&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;css_compressor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ss"&gt;:sass&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Both Rails 3 and 4 use &lt;code&gt;uglifier&lt;/code&gt; and &lt;code&gt;sass-rails&lt;/code&gt; in the default &lt;code&gt;Gemfile&lt;/code&gt;, so
these compressors are available unless you have removed them.&lt;/p&gt;

&lt;p&gt;With the new settings added, assets will again minify when compiled in
production.&lt;/p&gt;

&lt;p&gt;If you are interested in more upgrading steps and checklists, check out the
handbook I am writing: &lt;a href="http://upgradingtorails4.com/"&gt;Upgrading to Rails 4&lt;/a&gt;.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/otcp95MUyQM" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2013/05/08/my-assets-are-no-longer-compressed-in-rails-4-halp.html</feedburner:origLink></entry>
 
 <entry>
   <title>Upgrading Rails: Gems Extracted in Rails 4</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/xCA7JD8j54o/gems-extracted-in-rails-4.html" />
   <updated>2013-03-05T00:00:00-08:00</updated>
   <id>http://www.andylindeman.com/2013/03/05/gems-extracted-in-rails-4</id>
   <content type="html">&lt;p&gt;During the development of Rails 4, many features that were present in earlier
versions of Rails were removed from Rails itself and extracted to gems.&lt;/p&gt;

&lt;p&gt;Extracting features to gems slims down Rails itself, and allows Rails to take a
different direction to solve certain problems. For example,
&lt;a href="https://github.com/rails/strong_paramters"&gt;&lt;strong&gt;strong_parameters&lt;/strong&gt;&lt;/a&gt; in Rails 4
is recommended over &lt;strong&gt;attr_accessible&lt;/strong&gt; and &lt;strong&gt;attr_protected&lt;/strong&gt; from Rails 3
when an application needs to protect itself from mass-assignment
vulnerabilities.&lt;/p&gt;

&lt;p&gt;Furthermore, some of the extracted gems have new maintainers. These fresh
maintainers might respond more quickly to bug and feature requests than the
Rails core team feasibly can.&lt;/p&gt;

&lt;p&gt;Unfortunately, existing applications that are upgraded to Rails 4 may need to
pull in several new gems in order to perform properly. I have compiled a list
of these extracted gems and the features they provide. If your application uses
any of the listed features, you'll want to pull them into &lt;strong&gt;Gemfile&lt;/strong&gt; while
upgrading to Rails 4.&lt;/p&gt;

&lt;p&gt;The book I'm writing, &lt;a href="http://www.upgradingtorails4.com/"&gt;Upgrading to Rails
4&lt;/a&gt;, goes into more detail about these
extractions as well as new features in Rails 4.&lt;/p&gt;

&lt;table class="table table-bordered table-condensed"&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Gem&lt;/th&gt;
      &lt;th&gt;Description&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th&gt;
        &lt;a href="https://github.com/rails/protected_attributes"&gt;protected_attributes&lt;/a&gt;
      &lt;/th&gt;
      &lt;td&gt;
        Because Rails 4 recommends &lt;strong&gt;strong_parameters&lt;/strong&gt; for
        mass-assignment protection, &lt;strong&gt;attr_accessible&lt;/strong&gt; and
        &lt;strong&gt;attr_protected&lt;/strong&gt; have been extracted. I expect that most
        upgraded applications will need this gem, as the transition to
        &lt;strong&gt;strong_parameters&lt;/strong&gt; can be tedious and error-prone.
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;
        &lt;a href="https://github.com/rails/activeresource"&gt;activeresource&lt;/a&gt;
      &lt;/th&gt;
      &lt;td&gt;
        While the ActiveRecord-like abstraction over a RESTful API has always
        shipped as a gem, it is no longer included with Rails by default.
        Include it explicitly if your application requires it.
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;
        &lt;a href="https://github.com/rails/actionpack-action_caching"&gt;actionpack-action_caching&lt;/a&gt;&lt;br/&gt;
        &lt;a href="https://github.com/rails/actionpack-page_caching"&gt;actionpack-page_caching&lt;/a&gt;
      &lt;/th&gt;
      &lt;td&gt;
        Rails 4 includes many improvements to fragment caching, but action
        and page caching have been extracted. If your application uses action
        or page caching, be sure to pull these gems in explicitly.
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;
        &lt;a href="https://github.com/rails/activerecord-session_store"&gt;activerecord-session_store&lt;/a&gt;
      &lt;/th&gt;
      &lt;td&gt;
        The ability to store session data in a database table has been
        extracted in Rails 4. If your application uses the ActiveRecord
        session store, include this gem.
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;
        &lt;a href="https://github.com/rails/rails-observers"&gt;rails-observers&lt;/a&gt;
      &lt;/th&gt;
      &lt;td&gt;
        Rails no longer encourages the use of observers, separate objects
        that can react to lifecycle events of ActiveRecord models. If your
        application uses observers, make sure to include this gem.
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;
        &lt;a href="https://github.com/rails/actionpack-xml_parser"&gt;actionpack-xml_parser&lt;/a&gt;
      &lt;/th&gt;
      &lt;td&gt;
        Following security vulnerabilities involving inbound XML, Rails
        extracts the ability to accept XML input to a gem. If your application
        accepts XML in a request body (note: this is distinct from
        &lt;em&gt;responding with&lt;/em&gt; XML), you will need to pull in this gem.
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;
        &lt;a href="https://github.com/rails/rails-perftest"&gt;rails-perftest&lt;/a&gt;
      &lt;/th&gt;
      &lt;td&gt;
        Rails 4 extracts &lt;strong&gt;ActionDispatch::PerformanceTest&lt;/strong&gt;. If
        your application includes performance tests (usually located in
        &lt;strong&gt;test/performance&lt;/strong&gt;), add this gem to your bundle.
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;
        &lt;a href="https://github.com/reed/actionview-encoded_mail_to"&gt;actionview-encoded_mail_to&lt;/a&gt;
      &lt;/th&gt;
      &lt;td&gt;
        Rails previously included a little-known feature to obfuscate email
        addresses in hyperlinks, either with HTML entities or JavaScript
        code. If your application uses the &lt;strong&gt;encode&lt;/strong&gt; option with
        &lt;strong&gt;mail_to&lt;/strong&gt;, include this gem.
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;


&lt;p&gt; If you're interested in more details about upgrading to Rails 4, please
 &lt;a href="http://www.upgradingtorails4.com/"&gt;consider buying my Upgrading to Rails 4
 handbook&lt;/a&gt;.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/xCA7JD8j54o" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2013/03/05/gems-extracted-in-rails-4.html</feedburner:origLink></entry>
 
 <entry>
   <title>Decoding Rails Session Cookies</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/r2FJY2r1aCU/decoding-rails-session-cookies.html" />
   <updated>2013-02-18T00:00:00-08:00</updated>
   <id>http://www.andylindeman.com/2013/02/18/decoding-rails-session-cookies</id>
   <content type="html">&lt;p&gt;An &lt;a href="http://www.upgradingtorails4.com/"&gt;Upgrading to Rails 4&lt;/a&gt; reader emailed me
about this quote from the book:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Rails 3 uses digitally signed cookies as the default store for sessions.
Digitally signed cookies cannot be easily tampered with, but users can read
the data that is being saved.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;He asked:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Would you say that's something trivial to do, or would it involve some decent
amount of work? Just by accessing the Rails 3 cookies from my browser's
inspector, I can't seem to find a way to read the content from them.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Great question! Let's try.&lt;/p&gt;

&lt;p&gt;For this example, I setup a Rails application that sets a session key to a
constant value:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApplicationController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="ss"&gt;ActionController&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;before_filter&lt;/span&gt; &lt;span class="ss"&gt;:add_value_to_session&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;add_value_to_session&lt;/span&gt;
    &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:message&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If I hit the Rails application with &lt;code&gt;curl -i&lt;/code&gt; (include headers), I see:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl -i http://localhost:3000/
HTTP/1.1 200 OK 
Set-Cookie: &lt;span class="nv"&gt;_railsapp_session&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWYxZGNlMmNmYjFmYjBkODQ0NTc1ZWE3OTBjZmJmNTZkBjsAVEkiDG1lc3NhZ2UGOwBGSSIRSGVsbG8gV29ybGQhBjsARg%3D%3D--decd8e233744e2ba5a80481426e41d72a6685986; &lt;span class="nv"&gt;path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/; HttpOnly
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The cookie data looks obfuscated, but in reality it's shrouded only by Base64
encoding.&lt;/p&gt;

&lt;p&gt;Rack's session cookie methods can easily decode it:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# decode.rb&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rack&amp;#39;&lt;/span&gt;

&lt;span class="nb"&gt;puts&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;:Session&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="ss"&gt;Cookie&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="ss"&gt;:Base64&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Marshal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&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="no"&gt;ARGV&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Running &lt;code&gt;decode.rb&lt;/code&gt; with the cookie's data results in:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ruby decode.rb BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWYxZGNlMmNmYjFmYjBkODQ0NTc1ZWE3OTBjZmJmNTZkBjsAVEkiDG1lc3NhZ2UGOwBGSSIRSGVsbG8gV29ybGQhBjsARg%3D%3D--decd8e233744e2ba5a80481426e41d72a6685986
&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;session_id&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;gt;&lt;span class="s2"&gt;&amp;quot;f1dce2cfb1fb0d844575ea790cfbf56d&amp;quot;&lt;/span&gt;, &lt;span class="s2"&gt;&amp;quot;message&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;gt;&lt;span class="s2"&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Our message, alongside a generated session identifier, is available &lt;em&gt;without&lt;/em&gt;
the Rails application's secret session token.&lt;/p&gt;

&lt;p&gt;Attempting to tamper with the data, repackage it, and send it back to the
server would fail because the digital signature would not check out, but
&lt;strong&gt;the data is at least readable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Rails 4 will feature an encrypted cookie store, where data is both digitally
verified and unreadable to end users. I write about this (and more, of course!)
in &lt;a href="http://www.upgradingtorails4.com/"&gt;Upgrading to Rails 4&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In closing, remember:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The default Rails 3 session store (cookie store) allows users to &lt;em&gt;read&lt;/em&gt;
the contents of the session. They may not, however, change or tamper with the
session data.&lt;/li&gt;
&lt;li&gt;Do not store data in the session that would be sensitive even if the data
were read by an end user or an attacker sniffing the traffic.&lt;/li&gt;
&lt;li&gt;Switch to the encrypted cookie store when Rails 4 ships.&lt;/li&gt;
&lt;/ul&gt;

&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/r2FJY2r1aCU" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2013/02/18/decoding-rails-session-cookies.html</feedburner:origLink></entry>
 
 <entry>
   <title>rspec-rails and capybara 2.0: what you need to know</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/4oxJpVt9hjA/rspec-rails-and-capybara-2.0-what-you-need-to-know.html" />
   <updated>2012-11-11T00:00:00-08:00</updated>
   <id>http://www.andylindeman.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know</id>
   <content type="html">&lt;h3&gt;Background&lt;/h3&gt;

&lt;p&gt;Writing full-stack tests is important. I go into detail about this in my
&lt;a href="http://alindeman.github.com/acceptance_testing/"&gt;Acceptance Testing&lt;/a&gt; slide
deck.&lt;/p&gt;

&lt;p&gt;In a Rails application, RSpec &lt;a href="https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec"&gt;request
specs&lt;/a&gt;
are often used for the task of writing full-stack tests. In capybara 1.x,
request specs both wrap Rails' &lt;code&gt;IntegrationTest&lt;/code&gt; framework &lt;em&gt;and&lt;/em&gt; mix in
&lt;a href="http://github.com/jnicklas/capybara"&gt;capybara&lt;/a&gt;, a tool for driving web
applications (often via headless web browsers).&lt;/p&gt;

&lt;h3&gt;Confusion&lt;/h3&gt;

&lt;p&gt;The fact that request specs both wrap a Rails &lt;code&gt;IntegrationTest&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; have
capybara methods mixed in is confusing. José Valim goes into depth about the
problems in &lt;a href="http://blog.plataformatec.com.br/2012/06/improving-the-integration-between-capybara-and-rspec/"&gt;his blog post entitled "Improving the integration between capybara
and
RSpec"&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you've ever confused &lt;code&gt;get&lt;/code&gt; (Rails) with &lt;code&gt;visit&lt;/code&gt; (capybara) or
&lt;code&gt;response&lt;/code&gt; with &lt;code&gt;page&lt;/code&gt;, or attempted to explain the difference to a newcomer,
you know it is odd and often frustrating.&lt;/p&gt;

&lt;p&gt;The RSpec and capybara teams worked together to alleviate the confusion
in capybara 2.x.&lt;/p&gt;

&lt;h3&gt;Changes&lt;/h3&gt;

&lt;p&gt;Upon upgrading to capybara 2.0, capybara will not be available by default in
RSpec request specs. Instead, a new type of spec--the &lt;strong&gt;&lt;a href="https://www.relishapp.com/rspec/rspec-rails/docs/feature-specs/feature-spec"&gt;feature
spec&lt;/a&gt;&lt;/strong&gt;--has
been created for use with capybara.&lt;/p&gt;

&lt;p&gt;To upgrade to capybara 2.0, you'll need to do a few things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Upgrade rspec-rails to 2.12.0 or greater&lt;/li&gt;
&lt;li&gt;Move any tests that use capybara from spec/requests to spec/features.
Capybara tests use the &lt;code&gt;visit&lt;/code&gt; method and usually assert against &lt;code&gt;page&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Alternatively, you &lt;em&gt;can&lt;/em&gt; keep using capybara in request specs, but &lt;a href="https://github.com/rspec/rspec-rails/blob/master/Capybara.md"&gt;you'll need
to manually mix in the
methods&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Examples and Recommendations&lt;/h3&gt;

&lt;p&gt;The rest of this piece has some of my opinions, but opinions I've had good
success with in the applications I develop.&lt;/p&gt;

&lt;p&gt;Use RSpec request specs to test interactions with your application as a HTTP
API. To do so, use methods like &lt;code&gt;get&lt;/code&gt;, &lt;code&gt;post&lt;/code&gt;, &lt;code&gt;put&lt;/code&gt;, &lt;code&gt;delete&lt;/code&gt; and assert
against &lt;code&gt;response&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# spec/requests/widget_api_spec.rb&lt;/span&gt;
&lt;span class="n"&gt;describe&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;widget API&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;allows API clients to create widgets&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="n"&gt;widgets_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;widget&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Awesome Widget&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;json&amp;quot;&lt;/span&gt;

    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# &amp;quot;Created&amp;quot;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Use RSpec feature specs (with capybara) to test your application as a user
might interact with it. To do so, use methods like &lt;code&gt;visit&lt;/code&gt; and assert against
&lt;code&gt;page&lt;/code&gt;. You may also use &lt;code&gt;feature&lt;/code&gt; instead of &lt;code&gt;describe&lt;/code&gt; and &lt;code&gt;scenario&lt;/code&gt;
instead of &lt;code&gt;it&lt;/code&gt; to increase readability:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# spec/features/widget_management_spec.rb&lt;/span&gt;
&lt;span class="n"&gt;feature&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;widget management&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;scenario&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;creating a new widget&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;visit&lt;/span&gt; &lt;span class="n"&gt;root_url&lt;/span&gt;
    &lt;span class="n"&gt;click_link&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;New Widget&amp;quot;&lt;/span&gt;

    &lt;span class="n"&gt;fill_in&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Awesome Widget&amp;quot;&lt;/span&gt;
    &lt;span class="n"&gt;click_button&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Create Widget&amp;quot;&lt;/span&gt;

    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;have_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Widget was successfully created.&amp;quot;&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;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;h3&gt;Further Reading&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/rspec/rspec-rails/blob/master/features/feature_specs/feature_spec.feature"&gt;cucumber feature describing feature specs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/rspec/rspec-rails/blob/master/Capybara.md"&gt;rspec-rails documentation for upgrading capybara&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/jnicklas/capybara/pull/809"&gt;capybara pull request setting :type =&gt; :feature&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;Shoutouts&lt;/h3&gt;

&lt;p&gt;The capybara team, especially &lt;a href="https://github.com/jnicklas"&gt;Jonas Nicklas&lt;/a&gt;,
were pivotal in making these changes happen. In fact, my own role was small
comparatively. Thank you Jonas!&lt;/p&gt;

&lt;p&gt;Thanks to &lt;a href="https://twitter.com/freakyDaz"&gt;Darren Coxall&lt;/a&gt;, &lt;a href="https://twitter.com/jimhodgson"&gt;Jim
Hodgson&lt;/a&gt;, and &lt;a href="http://twitter.com/dchelimsky"&gt;David
Chelimsky&lt;/a&gt; for reviewing this post.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/4oxJpVt9hjA" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html</feedburner:origLink></entry>
 
 <entry>
   <title>Introducing rake-rails</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/r3iQf46wyfk/introducing-rake-rails.html" />
   <updated>2012-05-23T00:00:00-07:00</updated>
   <id>http://www.andylindeman.com/2012/05/23/introducing-rake-rails</id>
   <content type="html">&lt;p&gt;&lt;a href="http://jrubyconf.com"&gt;JRubyConf&lt;/a&gt; 2012 was a blast. Thanks
&lt;a href="http://blog.nicksieger.com/"&gt;Nick&lt;/a&gt; and all the others who made it happen.&lt;/p&gt;

&lt;p&gt;I enjoyed many of the talks, including &lt;a href="https://speakerdeck.com/u/j3/p/adventures-on-the-golden-path"&gt;Adventures on the Golden
Path&lt;/a&gt; by &lt;a href="http://twitter.com/#!/j3"&gt;Jeff
Casimir&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Admittedly, I do not agree with all of his ideas or his implementations, but
one that struck me as a good idea &lt;strong&gt;and low hanging fruit&lt;/strong&gt; was the confusion
among the &lt;code&gt;rails&lt;/code&gt; and &lt;code&gt;rake&lt;/code&gt; commands:&lt;/p&gt;

&lt;script async class="speakerdeck-embed" data-slide="39" data-id="4f710744a1d1bd002200abc1" data-ratio="1.299492385786802" src="//speakerdeck.com/assets/embed.js"&gt;&lt;/script&gt;


&lt;p&gt;&lt;strong&gt;Beginners and advanced users get tripped up by this one all the time.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, I tried whipping up a gem that delegates &lt;code&gt;rake generate&lt;/code&gt;, &lt;code&gt;rake console&lt;/code&gt;,
etc.. to their respective &lt;code&gt;rails&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;It's called &lt;strong&gt;rake-rails&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# Gemfile&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rake-rails&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Profit! You can now run ALL the things with &lt;code&gt;rake&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;rake generate migration add_name_to_users name:string
&lt;span class="nv"&gt;$ &lt;/span&gt;rake console staging
&lt;span class="c"&gt;# ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;For efficiency freaks: it's still pretty fast and does &lt;strong&gt;not&lt;/strong&gt; involve loading
the Rails environment multiple times; it simply delegates to &lt;code&gt;rails/cli&lt;/code&gt; as
quickly as possible, just like the &lt;code&gt;rails&lt;/code&gt; command does.&lt;/p&gt;

&lt;p&gt;Try it out and let me know what you think! Everything is up on
&lt;a href="https://github.com/alindeman/rake-rails"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/r3iQf46wyfk" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2012/05/23/introducing-rake-rails.html</feedburner:origLink></entry>
 
 <entry>
   <title>Callbacks and Background Jobs</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/FBBe8zkuRWE/after-save-and-background-jobs.html" />
   <updated>2011-10-02T00:00:00-07:00</updated>
   <id>http://www.andylindeman.com/2011/10/02/after-save-and-background-jobs</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;: Don't use &lt;code&gt;after_{create,update,save}&lt;/code&gt; to enqueue a background
job; instead use &lt;code&gt;after_commit&lt;/code&gt; because the database transaction will
be committed.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Rails' callbacks are popular way to hook into the lifecycle of a model, and use
these hooks to automatically perform tasks when a model changes.&lt;/p&gt;

&lt;p&gt;For instance:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Image&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="ss"&gt;ActiveRecord&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;after_create&lt;/span&gt; &lt;span class="ss"&gt;:generate_thumbnail&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_thumbnail&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;In this case, we are generating a thumbnail for an image after it is
created.&lt;/p&gt;

&lt;p&gt;Because it's likely that generating a thumbnail is an expensive
operation that could easily bog down the web server and web requests,
many folks would refactor it to enqueue a background job that generates
the thumbnail at some time in the future:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Image&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="ss"&gt;ActiveRecord&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;after_create&lt;/span&gt; &lt;span class="ss"&gt;:enqueue_thumbnail_job&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;enqueue_thumbnail_job&lt;/span&gt;
    &lt;span class="no"&gt;Resque&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ThumbnailJob&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&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;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;In this case, we are using a &lt;a href="https://github.com/defunkt/resque"&gt;Resque&lt;/a&gt;
job to do the heavy lifting. We avoid bogging down web requests with
thumbnail generation.&lt;/p&gt;

&lt;p&gt;Great, so imagine the Resque job looks something like:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ThumbnailJob&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Image&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;generate_thumbnail&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The problem is that &lt;strong&gt;after_create (and most other Rails callbacks) are
executed within the context of a transaction&lt;/strong&gt;: other database sessions
will not see the changes to the database until after the changes are
committed.&lt;/p&gt;

&lt;p&gt;If the background worker was incredibly fast at popping off a job, it is
possible that &lt;code&gt;Image.find(id)&lt;/code&gt; would fail &lt;strong&gt;because the database
transaction had not yet completed&lt;/strong&gt;. The row would not be visible to
other database sessions yet.&lt;/p&gt;

&lt;p&gt;Granted, it is unlikely that this will occur as there is a delay
between the time a job is enqueued and when it is started by a worker
process; however, it &lt;em&gt;could&lt;/em&gt; happen in a small number of cases, and
when it does, it would be very hard to debug and reproduce.&lt;/p&gt;

&lt;p&gt;So, what's the solution?&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;after_commit&lt;/strong&gt; callback, introduced in Rails 3 and &lt;a href="https://rubygems.org/gems/after_commit"&gt;available as a
plugin in Rails 2&lt;/a&gt;.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Image&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="ss"&gt;ActiveRecord&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;after_commit&lt;/span&gt; &lt;span class="ss"&gt;:enqueue_thumbnail_job&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;enqueue_thumbnail_job&lt;/span&gt;
    &lt;span class="no"&gt;Resque&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ThumbnailJob&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="n"&gt;destroyed?&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; We also check to make sure the object is not destroyed, as
&lt;code&gt;after_commit&lt;/code&gt; is called upon destruction as well (thanks &lt;strong&gt;zimbatm&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;Moral of the story: &lt;strong&gt;if you expect another process (e.g., background
job) to see changes to the database, wait until the database transaction
is completed (after_commit)&lt;/strong&gt;.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/FBBe8zkuRWE" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2011/10/02/after-save-and-background-jobs.html</feedburner:origLink></entry>
 
 <entry>
   <title>Testing Devise with RSpec Request Specs and Capybara</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/TxYcRl1_IQ0/testing-devise-with-rspec-request-specs-and-capybara.html" />
   <updated>2011-09-25T00:00:00-07:00</updated>
   <id>http://www.andylindeman.com/2011/09/25/testing-devise-with-rspec-request-specs-and-capybara</id>
   <content type="html">&lt;p&gt;Many questions about testing Devise come up regularly in the &lt;a href="irc://irc.freenode.net/rspec"&gt;freenode/#rspec&lt;/a&gt;
IRC channel. Often, folks ask how to use RSpec request specs to write
integration level ("full stack") tests against Devise.&lt;/p&gt;

&lt;p&gt;In integration tests, ideally nothing is mocked or stubbed. Using the
&lt;a href="https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-(and-rspec)"&gt;recommended setup for controller
tests&lt;/a&gt;,
for instance, is not a good idea in request specs. We want to click
links exactly as a user would, fill in forms exactly how a user would,
etc...&lt;/p&gt;

&lt;p&gt;This post assumes you have a Devise setup running already. If not, start
with the &lt;a href="https://github.com/plataformatec/devise"&gt;Devise README&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;First, RSpec request specs can use either Capybara or Webrat. I prefer
Capybara. To install, add this line to your &lt;code&gt;Gemfile&lt;/code&gt; and run &lt;code&gt;bundle
install&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;capybara&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:group&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;:test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Next, add &lt;code&gt;require 'capybara/rspec'&lt;/code&gt; to &lt;code&gt;spec/spec_helper.rb&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;RAILS_ENV&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;test&amp;#39;&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="s2"&gt;&amp;quot;../../config/environment&amp;quot;&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="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rspec/rails&amp;#39;&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;capybara/rspec&amp;#39;&lt;/span&gt; &lt;span class="c1"&gt;### ADD THIS LINE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Finally, RSpec request specs must be created in the &lt;code&gt;spec/requests&lt;/code&gt;
directory under your Rails root, so create that directory if it does not
already exist.&lt;/p&gt;

&lt;p&gt;Now you're ready to start writing specs! For example, here's a simple
one I named &lt;code&gt;spec/requests/user_registration_spec.rb&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;spec_helper&amp;quot;&lt;/span&gt;

&lt;span class="n"&gt;describe&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;user registration&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;allows new users to register with an email address and password&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;visit&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/users/sign_up&amp;quot;&lt;/span&gt;

    &lt;span class="n"&gt;fill_in&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Email&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                 &lt;span class="ss"&gt;:with&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;alindeman@example.com&amp;quot;&lt;/span&gt;
    &lt;span class="n"&gt;fill_in&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Password&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;              &lt;span class="ss"&gt;:with&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;ilovegrapes&amp;quot;&lt;/span&gt;
    &lt;span class="n"&gt;fill_in&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Password confirmation&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:with&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;ilovegrapes&amp;quot;&lt;/span&gt;

    &lt;span class="n"&gt;click_button&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Sign up&amp;quot;&lt;/span&gt;

    &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;have_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Welcome! You have signed up successfully.&amp;quot;&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;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;And here's one to test user sign in, named
&lt;code&gt;spec/requests/user_sign_in_spec.rb&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;spec_helper&amp;quot;&lt;/span&gt;

&lt;span class="n"&gt;describe&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;user sign in&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;allows users to sign in after they have registered&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:email&lt;/span&gt;    &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;alindeman@example.com&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                       &lt;span class="ss"&gt;:password&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;ilovegrapes&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;visit&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/users/sign_in&amp;quot;&lt;/span&gt;

    &lt;span class="n"&gt;fill_in&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Email&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="ss"&gt;:with&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;alindeman@example.com&amp;quot;&lt;/span&gt;
    &lt;span class="n"&gt;fill_in&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Password&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:with&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;ilovegrapes&amp;quot;&lt;/span&gt;

    &lt;span class="n"&gt;click_button&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Sign in&amp;quot;&lt;/span&gt;

    &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;have_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Signed in successfully.&amp;quot;&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;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Hopefully these serve as a good starting point for your tests.&lt;/p&gt;

&lt;p&gt;As you move beyond these basic specs, check out these resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec"&gt;rspec-rails Request Spec Cukes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/jnicklas/capybara/blob/master/README.rdoc"&gt;capybara README&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions"&gt;capybara Node::Actions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers"&gt;capybara Node::Matchers&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/TxYcRl1_IQ0" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2011/09/25/testing-devise-with-rspec-request-specs-and-capybara.html</feedburner:origLink></entry>
 
 <entry>
   <title>What's The First Release That Contains This Commit?</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/0T8plJUpOL8/whats-the-first-release-that-contains-this-commit.html" />
   <updated>2011-09-12T00:00:00-07:00</updated>
   <id>http://www.andylindeman.com/2011/09/12/whats-the-first-release-that-contains-this-commit</id>
   <content type="html">&lt;p&gt;If the project is using `git` and tags each release, it is really easy to ask `git` which tag is the first to contain a given commit.&lt;/p&gt;
&lt;p&gt;For instance, take &lt;a href="https://github.com/sunspot/sunspot/commit/58a46bf"&gt;58a46bf&lt;/a&gt; on &lt;a href="https://github.com/sunspot/sunspot"&gt;Sunspot&lt;/a&gt; which added the `Sunspot.optimize` method:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; /path/to/sunspot
&lt;span class="nv"&gt;$ &lt;/span&gt;git describe --contains 58a46bf
v1.2.0~22
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this case, `v1.2.0` was the first tag to contain the commit in its tree, and it was `22` commits away.&lt;/p&gt;
&lt;p&gt;To use `Sunspot.optimize`, you&amp;#8217;d need at least Sunspot v1.2.0.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/0T8plJUpOL8" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2011/09/12/whats-the-first-release-that-contains-this-commit.html</feedburner:origLink></entry>
 
 <entry>
   <title>ruby-debug and "Warning: saved frames may be incomplete"</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/WjOIUnafQS4/ruby-debugger-warning-saved-frames-may-be-incomplete.html" />
   <updated>2011-08-04T00:00:00-07:00</updated>
   <id>http://www.andylindeman.com/2011/08/04/ruby-debugger-warning-saved-frames-may-be-incomplete</id>
   <content type="html">&lt;p&gt;I like using &lt;a href="http://rubygems.org/gems/ruby-debug"&gt;ruby-debug&lt;/a&gt; (well, &lt;a href="http://rubygems.org/gems/ruby-debug19"&gt;ruby-debug19&lt;/a&gt; for Ruby 1.9), a command line debugger for Ruby.&lt;/p&gt;
&lt;p&gt;Occasionally, though, I would run into an issue where the &lt;b&gt;backtrace&lt;/b&gt; (&lt;b&gt;bt&lt;/b&gt;) command did not give a useful stack trace.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="o"&gt;(&lt;/span&gt;rdb:1&lt;span class="o"&gt;)&lt;/span&gt; bt
--&amp;gt; &lt;span class="c"&gt;#0 at line /.../config/initializers/omniauth.rb:11&lt;/span&gt;
Warning: saved frames may be incomplete; compare with &lt;span class="nb"&gt;caller&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;0&lt;span class="o"&gt;)&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Thanks to a &lt;a href="http://stackoverflow.com/questions/2763893/why-does-ruby-debug-say-saved-frames-may-be-incomplete"&gt;StackOverflow question&lt;/a&gt;, though, I&amp;#8217;ve found the solution: &lt;b&gt;require &amp;#8220;ruby-debug&amp;#8221;&lt;/b&gt; and call &lt;b&gt;Debugger.start&lt;/b&gt; as soon as possible in the application being debugged.&lt;/p&gt;
&lt;p&gt;For a Rails project, this may look like adding the following to &lt;b&gt;config/environments/development.rb&lt;/b&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;ruby-debug&amp;#39;&lt;/span&gt;
&lt;span class="no"&gt;Debugger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Sweet, sweet backtraces return!&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="o"&gt;(&lt;/span&gt;rdb:1&lt;span class="o"&gt;)&lt;/span&gt; bt
--&amp;gt; &lt;span class="c"&gt;#0 OmniAuth::Strategy.fail!(message_key#Symbol)&lt;/span&gt;
       at line /.../lib/omniauth/strategy.rb:11
    &lt;span class="c"&gt;#1 OmniAuth::Strategy.fail!(message_key#Symbol)&lt;/span&gt;
       at line /.../lib/omniauth/strategy.rb:224
    &lt;span class="c"&gt;#2 OmniAuth::Strategies::OpenID.start&lt;/span&gt;
       at line /.../lib/omniauth/strategies/open_id.rb:71
    &lt;span class="c"&gt;#3 OmniAuth::Strategies::OpenID.request_phase&lt;/span&gt;
       at line /.../lib/omniauth/strategies/open_id.rb:63
    ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/WjOIUnafQS4" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2011/08/04/ruby-debugger-warning-saved-frames-may-be-incomplete.html</feedburner:origLink></entry>
 
 <entry>
   <title>Optimistic Locking with MongoDB (and mongo_mapper)</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/anbH7jNb4J8/optimistic-locking-with-mongodb.html" />
   <updated>2011-07-06T00:00:00-07:00</updated>
   <id>http://www.andylindeman.com/2011/07/06/optimistic-locking-with-mongodb</id>
   <content type="html">&lt;p&gt;(cross posted on the &lt;a href="http://napkin.highgroove.com/articles/2011/07/06/optimistic-locking-with-mongo"&gt;Highgroove Blog&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;At &lt;a href="http://highgroove.com"&gt;Highgroove&lt;/a&gt; (&lt;a href="/about.html"&gt;where I work&lt;/a&gt;), we build database-backed web applications. These days, there are many options when choosing a database backend. We normally start with relational database systems (e.g., &lt;a href="http://www.postgresql.org/"&gt;PostgreSQL&lt;/a&gt; and &lt;a href="http://dev.mysql.com/"&gt;MySQL&lt;/a&gt;) because they are very mature and feature niceties like &lt;a href="http://en.wikipedia.org/wiki/ACID"&gt;&lt;span class="caps"&gt;ACID&lt;/span&gt; transactions&lt;/a&gt; and &lt;a href="http://www.postgresql.org/docs/9.0/interactive/explicit-locking.html"&gt;locks&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;On the other hand, we also work with newer NoSQL database systems that often trade features like transactions, locks, and joins for higher performance and scalability. One popular option is a document-based store called &lt;a href="http://www.mongodb.org/"&gt;MongoDB&lt;/a&gt;.  By design, MongoDB does not support &lt;span class="caps"&gt;ACID&lt;/span&gt; transactions (though many operations &lt;a href="http://www.mongodb.org/display/DOCS/Atomic+Operations"&gt;are atomic&lt;/a&gt;) or traditional locks.&lt;/p&gt;
&lt;p&gt;In many cases MongoDB, paired with an object-relational mapper like &lt;a href="http://mongomapper.com/"&gt;mongo_mapper&lt;/a&gt; is a great solution for Ruby and Rails applications. And after the break I explore a solution that allows a developer to &amp;#8220;lock&amp;#8221; a MongoDB document while still maintaining the high performance and other features MongoDB is known for.&lt;/p&gt;
&lt;p&gt;mongo_mapper loads documents from MongoDB into memory as a Ruby object. Typically, a program then changes a few values and saves the record. Notably, saving the record actually resets every field in the document to the values in the Ruby object.&lt;/p&gt;
&lt;p&gt;Unfortunately, data and work can be lost if multiple processes (e.g., different web server processes/threads or background workers) are interacting with the same documents at the same time.&lt;/p&gt;
&lt;p&gt;For instance, in the code below if two processes execute &lt;code&gt;post = BlogPost.find("4e13cda850b86112c9000001")&lt;/code&gt; at roughly the same time, one will end up overriding the other because the values in memory will be stale.&lt;/p&gt;
&lt;script src="https://gist.github.com/1066390.js"&gt; &lt;/script&gt;&lt;p&gt;This problem is not unique to MongoDB or mongo_mapper, but the potential solutions are different from relational databases because MongoDB documents cannot be exclusively locked.&lt;/p&gt;
&lt;p&gt;One way to solve the problem is to only interact with MongoDB through its &lt;a href="http://www.mongodb.org/display/DOCS/Atomic+Operations"&gt;atomic operations&lt;/a&gt;, but many of the conveniences of an object-relational mapper are lost for a problem that may only occur sporadically.&lt;/p&gt;
&lt;p&gt;Another way is to use &lt;strong&gt;optimistic locking&lt;/strong&gt;, a technique that reliably determines if a record has been updated by another process between the time it was initially loaded into memory and the time a save is attempted. If the record &lt;em&gt;has&lt;/em&gt; been modified by another process, an error is raised and the object must be reloaded. If objects are not in high contention and retrying the operations is easily accomplished, optimistic locking is a good solution that keeps performance high in the average case.&lt;/p&gt;
&lt;p&gt;If using optimistic locking with a background job, a reasonable response to a stale document error would be to reload the record, retry the operation, and attempt a save again. On the other hand, if two end users are in contention, the stale document could be presented to one of the users for manual conflict resolution.&lt;/p&gt;
&lt;p&gt;We have written a small gem that implements optimistic locking with mongo_mapper called &lt;a href="https://github.com/highgroove/mm-optimistic_locking"&gt;mm-optimistic_locking&lt;/a&gt;. It is very straightforward to use and we think it does its job well, but we definitely welcome &lt;a href="https://github.com/highgroove/mm-optimistic_locking/issues"&gt;feedback&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;An example usage is shown below (basic mongo_mapper familiarity assumed):&lt;/p&gt;
&lt;script src="https://gist.github.com/1066412.js"&gt; &lt;/script&gt;&lt;script src="https://gist.github.com/1066414.js"&gt; &lt;/script&gt;&lt;p&gt;More information is available in the &lt;a href="https://github.com/highgroove/mm-optimistic_locking/blob/master/README.md"&gt;&lt;span class="caps"&gt;README&lt;/span&gt;&lt;/a&gt; and its &lt;a href="https://github.com/highgroove/mm-optimistic_locking"&gt;GitHub homepage&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/anbH7jNb4J8" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2011/07/06/optimistic-locking-with-mongodb.html</feedburner:origLink></entry>
 
 <entry>
   <title>Introduction to Solr and Sunspot</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/NBB-oFEpPsk/introduction-to-solr-and-sunspot.html" />
   <updated>2011-04-14T00:00:00-07:00</updated>
   <id>http://www.andylindeman.com/2011/04/14/introduction-to-solr-and-sunspot</id>
   <content type="html">&lt;p&gt;For the &lt;a href="http://www.meetup.com/hsv-rb/events/17085402/"&gt;April meeting&lt;/a&gt; of the &lt;a href="http://www.meetup.com/hsv-rb/" title="hsv.rb"&gt;Huntsville Ruby Meetup&lt;/a&gt;, I recorded a 5 minute lightning talk screencast giving a high level overview of setting up &lt;strong&gt;Solr&lt;/strong&gt; and &lt;strong&gt;Sunspot&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solr&lt;/strong&gt; and &lt;strong&gt;Sunspot&lt;/strong&gt; give you powerful full-text search within Ruby applications.  The &lt;strong&gt;sunspot_rails&lt;/strong&gt; gem plays really nicely with Rails.&lt;/p&gt;
&lt;p&gt;More info:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Solr: &lt;a href="http://lucene.apache.org/solr/"&gt;http://lucene.apache.org/solr/&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;Sunspot: &lt;a href="http://outoftime.github.com/sunspot/"&gt;http://outoftime.github.com/sunspot/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Enjoy and give me feedback!&lt;/p&gt;
&lt;p&gt;&lt;iframe src="http://player.vimeo.com/video/22365115" width="600" height="375" frameborder="0"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/NBB-oFEpPsk" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2011/04/14/introduction-to-solr-and-sunspot.html</feedburner:origLink></entry>
 
 <entry>
   <title>Remote Pair Programming</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/lGG4Ri9CLFs/remote-pair-programming.html" />
   <updated>2011-03-05T00:00:00-08:00</updated>
   <id>http://www.andylindeman.com/2011/03/05/remote-pair-programming</id>
   <content type="html">&lt;p&gt;I don&amp;#8217;t think I&amp;#8217;ve ever &lt;em&gt;not&lt;/em&gt; learned something from talking to another developer.  The world of developing software is simply too big for one person to grok completely: &lt;em&gt;anyone&lt;/em&gt; can have insight or experiences that have value when shared.&lt;/p&gt;
&lt;p&gt;Along those lines, &lt;a href="http://en.wikipedia.org/wiki/Pair_programming"&gt;pair programming&lt;/a&gt; is a technique that champions the benefits of two developers working on the same problem at the same time.&lt;/p&gt;
&lt;p&gt;College was my first experience with pair programming; unfortunately the introductory lab for &amp;#8220;Computer Science 101&amp;#8221; was simply not the appropriate time to introduce it.  That poor execution put a foul taste in my mouth for the technique.&lt;/p&gt;
&lt;p&gt;However, as I moved into the software development industry, I heard more and more respected programmers tout the benefits.&lt;/p&gt;
&lt;p&gt;Finally, I gave it another shot with &lt;a href="http://twitter.com/tundal45"&gt;Ashish Dixit&lt;/a&gt; earlier today.  We even went a step further, doing &lt;a href="http://en.wikipedia.org/wiki/Pair_programming#Remote_pair_programming"&gt;remote pair programming&lt;/a&gt; as we were not physically located in the same place.&lt;/p&gt;
&lt;p&gt;We hacked on a &lt;a href="http://codekata.pragprog.com/2007/01/kata_thirteen_c.html"&gt;Kata&lt;/a&gt; that initially appeared simple, but ended up being complicated enough to spur some great discussion on design and testability.&lt;/p&gt;
&lt;p&gt;We started by writing a failing test, then taking turns writing the code that would make the test pass.  We discussed opportunities for refactoring along the way.  Each of us had the ability to dive in whenever we saw room for improvement.&lt;/p&gt;
&lt;p&gt;It was remarkably easy to get started.  We used &lt;strong&gt;&lt;a href="http://skype.com/"&gt;Skype&lt;/a&gt;&lt;/strong&gt; to talk and &lt;strong&gt;&lt;a href="http://www.teamviewer.com/en/index.aspx"&gt;TeamViewer&lt;/a&gt;&lt;/strong&gt; for sharing a screen and allowing remote control.&lt;/p&gt;
&lt;p&gt;I was really struck by how much I was invigorated by the experience.  Ashish has previously &lt;a href="http://tundal45.tumblr.com/post/3014278767/stand-remotely-on-the-shoulder-of-giants"&gt;blogged&lt;/a&gt; about his experiences as well.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;I want to do this again!&lt;/strong&gt;  Any takers?  Let&amp;#8217;s hack on a &lt;a href="http://codekata.pragprog.com/"&gt;Kata&lt;/a&gt;, &lt;a href="http://rubyquiz.com/"&gt;Ruby Quiz&lt;/a&gt;, or an open-source project.  &lt;a href="mailto:&amp;#097;&amp;#108;&amp;#105;&amp;#110;&amp;#100;&amp;#101;&amp;#109;&amp;#097;&amp;#110;&amp;#064;&amp;#103;&amp;#109;&amp;#097;&amp;#105;&amp;#108;&amp;#046;&amp;#099;&amp;#111;&amp;#109;"&gt;Get in touch with me!&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/lGG4Ri9CLFs" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2011/03/05/remote-pair-programming.html</feedburner:origLink></entry>
 
 <entry>
   <title>Beware of Ruby Sugar</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/I-BiaEg1QQo/beware-of-ruby-syntactic-sugar.html" />
   <updated>2011-01-22T00:00:00-08:00</updated>
   <id>http://www.andylindeman.com/2011/01/22/beware-of-ruby-syntactic-sugar</id>
   <content type="html">&lt;p&gt;Yesterday, I &lt;a href="/2011/01/21/iterate-through-time-with-rails.html"&gt;posted a tip&lt;/a&gt; about iterating through time with Ruby and Rails/ActiveSupport.&lt;/p&gt;
&lt;p&gt;Originally, I posted this code.  &lt;strong&gt;This code is actually not valid in Ruby 1.9 anymore.&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# Steps day-by-day from today until 5 days from now&lt;/span&gt;
&lt;span class="no"&gt;Range&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="no"&gt;Time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;days&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;from_now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&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;time&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This code looks amazingly compact and incredibly idiomatic &amp;#8230; right?&lt;/p&gt;
&lt;p&gt;Well, not so &amp;#8220;fast&amp;#8221;.  Take a look at the &lt;a href="http://rubydoc.info/stdlib/core/1.8.7/Range#step-instance_method"&gt;&lt;span class="caps"&gt;API&lt;/span&gt; documentation for Range#step&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Iterates over rng, passing each nth element to the block. If the range contains numbers, n is added for each iteration. &lt;strong&gt;Otherwise step invokes &lt;em&gt;succ&lt;/em&gt; to iterate through range elements.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;A &lt;strong&gt;Time&lt;/strong&gt; instance is not a number, so &lt;strong&gt;Range#step&lt;/strong&gt; is stuck calling &lt;strong&gt;Time#succ&lt;/strong&gt; 86400 times (the number of seconds in a day) between steps.  To add insult to injury, &lt;strong&gt;Time#succ&lt;/strong&gt; creates a new &lt;strong&gt;Time&lt;/strong&gt; instance: we create 86399 useless objects between steps!&lt;/p&gt;
&lt;p&gt;Yeouch.&lt;/p&gt;
&lt;p&gt;While it&amp;#8217;s often reasonable to sacrifice a bit of performance for idiomatic, readable code, in this case, the price is certainly too high.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lesson learned:&lt;/strong&gt; Peel back at least one level of abstraction when writing any code that looks a bit magical.&lt;/p&gt;
&lt;p&gt;In the end, I recommend this code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DateTime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;

&lt;span class="c1"&gt;# Second argument defaults to 1, but shown for clarity&lt;/span&gt;
&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&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;time&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;DateTime&lt;/strong&gt; has its own &lt;strong&gt;#step&lt;/strong&gt; instance method which can be much more specific about its implementation than &lt;strong&gt;Range#step&lt;/strong&gt; which must operate correctly on nearly every type of enumerable object.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Final thought:&lt;/strong&gt; Sometimes abstractions and duck typing are great things, but be especially careful with methods that accept any object that implements a very limited public &lt;span class="caps"&gt;API&lt;/span&gt; (in this case, &lt;strong&gt;Range&lt;/strong&gt; with objects that respond to &lt;strong&gt;#succ&lt;/strong&gt;).  In many cases, using only this limited &lt;span class="caps"&gt;API&lt;/span&gt; results in very bad inefficiencies.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/I-BiaEg1QQo" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2011/01/22/beware-of-ruby-syntactic-sugar.html</feedburner:origLink></entry>
 
 <entry>
   <title>Iterating Through Time With Rails</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/JxLSVkhhq5E/iterate-through-time-with-rails.html" />
   <updated>2011-01-21T00:00:00-08:00</updated>
   <id>http://www.andylindeman.com/2011/01/21/iterate-through-time-with-rails</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;Be sure to read the &lt;a href="/2011/01/22/beware-of-ruby-syntactic-sugar.html"&gt;follow up post&lt;/a&gt; where I analyze this a bit more and make better recommendations.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I just learned this neat trick from &lt;a href="http://twitter.com/#!/rjspotter"&gt;@rjspotter&lt;/a&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# Could also use Enumerable#map with Time.at&lt;/span&gt;
&lt;span class="no"&gt;Range&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="no"&gt;Time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;days&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;from_now&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&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;seconds_since_epoch&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seconds_since_epoch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Right now, for instance, that outputs:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;Fri Jan 21 15:25:32 -0600 2011
Sat Jan 22 15:25:32 -0600 2011
Sun Jan 23 15:25:32 -0600 2011
Mon Jan 24 15:25:32 -0600 2011
Tue Jan 25 15:25:32 -0600 2011
Wed Jan 26 15:25:32 -0600 2011
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Replace &lt;strong&gt;days&lt;/strong&gt; with &lt;strong&gt;hours&lt;/strong&gt;, &lt;strong&gt;minutes&lt;/strong&gt;, &lt;strong&gt;weeks&lt;/strong&gt;, etc&amp;#8230;&lt;/p&gt;
&lt;p&gt;Notably, this uses &lt;a href="http://ruby-doc.org/core/classes/Range.html#M000703"&gt;Range#step&lt;/a&gt; from Ruby core and &lt;a href="https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/numeric/time.rb"&gt;ActiveSupport&amp;#8217;s monkey patching of Numeric&lt;/a&gt; from Rails.&lt;/p&gt;
&lt;p&gt;While the &lt;strong&gt;#to_i&lt;/strong&gt; calls look a bit unappealing, it&amp;#8217;s important that &lt;strong&gt;Range#step&lt;/strong&gt; is called over numbers, or else it would have to call &lt;strong&gt;#succ&lt;/strong&gt; 86400 times between iterations!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/JxLSVkhhq5E" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2011/01/21/iterate-through-time-with-rails.html</feedburner:origLink></entry>
 
 <entry>
   <title>P2PU: Introduction to Ruby and Rails (Signup Closed)</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/qoJzFkbZQEM/p2pu-intro-to-ruby-and-rails-signup-closed.html" />
   <updated>2011-01-19T00:00:00-08:00</updated>
   <id>http://www.andylindeman.com/2011/01/19/p2pu-intro-to-ruby-and-rails-signup-closed</id>
   <content type="html">&lt;p&gt;As I &lt;a href="/2011/01/07/p2pu-intro-to-ruby-and-rails.html"&gt;mentioned previously&lt;/a&gt;, I&amp;#8217;m organizing and facilitating a free (as in cost and content licensing) Peer 2 Peer University (P2PU) course called &lt;strong&gt;&lt;a href="http://p2pu.org/webcraft/introduction-ruby-and-rails"&gt;Introduction to Ruby and Rails&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The signups for P2PU were scheduled to close on 19 January and because of high demand, I interpreted that to mean the beginning of the day.  I ended up having over &lt;strong&gt;120&lt;/strong&gt; complete applications, and probably closer to &lt;strong&gt;200&lt;/strong&gt; including people who expressed interest, but did not complete the sign up task.&lt;/p&gt;
&lt;p&gt;To those &lt;strong&gt;50&lt;/strong&gt; or so who I accepted to due obvious enthusiasm or otherwise interesting background, congratulations!  I&amp;#8217;m really looking forward to working with you to expand our knowledge of Ruby and Rails.&lt;/p&gt;
&lt;p&gt;To those who I could not accept, all of the coursework will be open and freely available for you to lurk or otherwise follow along.  We&amp;#8217;ll be using:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;A moderated &lt;a href="https://groups.google.com/group/p2pu-rubyrails-jan2011"&gt;Google Group&lt;/a&gt; for high latency mailing list communication&lt;/li&gt;
	&lt;li&gt;An unmoderated &lt;a href="irc://irc.freenode.net#p2pu-rubyrails"&gt;&lt;span class="caps"&gt;IRC&lt;/span&gt; channel&lt;/a&gt; for low latency, informal chat&lt;/li&gt;
	&lt;li&gt;Some sort of wiki/collaborative document editing platform (to be determined)&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://alindeman.github.com/p2pu-rubyrails/"&gt;Weekly coursework&lt;/a&gt; with reading assignments and exercises&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The course officially starts on 26 January, though I&amp;#8217;ll aim to have the week 1 coursework finished and available by the beginning of that week.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/qoJzFkbZQEM" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2011/01/19/p2pu-intro-to-ruby-and-rails-signup-closed.html</feedburner:origLink></entry>
 
 <entry>
   <title>Defining Methods Using Blocks in Ruby</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/kEH5PIC-R2c/defining-methods-using-blocks-in-ruby.html" />
   <updated>2011-01-08T00:00:00-08:00</updated>
   <id>http://www.andylindeman.com/2011/01/08/defining-methods-using-blocks-in-ruby</id>
   <content type="html">&lt;p&gt;While chatting on &lt;a href="irc://irc.freenode.net#ruby"&gt;irc.freenode.net#ruby&lt;/a&gt; this evening, I ran across someone attempting to &lt;strong&gt;define a singleton method using a block&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Quick reminder: singleton methods are those defined on a specific object.  More information available from the &lt;a href="http://www.rubyist.net/~slagell/ruby/singletonmethods.html"&gt;Ruby User&amp;#8217;s Guide&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;While there are multiple ways to define a singleton method, he &lt;strong&gt;needed to use a block&lt;/strong&gt; that made use of a local variable.  His first try was using &lt;strong&gt;instance_eval&lt;/strong&gt;.  A simplified version of his code is shown below:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;123&amp;quot;&lt;/span&gt;

&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Object&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;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;instance_eval&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;return_val&lt;/span&gt;
    &lt;span class="n"&gt;val&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;return_val&lt;/span&gt; &lt;span class="c1"&gt;# expecting &amp;quot;123&amp;quot; but instead raises error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Unfortunately, this code raises an error:&lt;/p&gt;
&lt;pre&gt;NameError: undefined local variable or method `val'&lt;/pre&gt;
&lt;p&gt;I was also initially puzzled as to why &lt;strong&gt;val&lt;/strong&gt; in the block (a.k.a. closure) was not bound to the local variable &lt;strong&gt;val&lt;/strong&gt; at the time of block creation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Mon_Ouie&lt;/strong&gt; from the &lt;span class="caps"&gt;IRC&lt;/span&gt; channel explains: &amp;#8220;Once you use the def keyword, it&amp;#8217;s not a closure anymore.&amp;#8221;&lt;/p&gt;
&lt;p&gt;To achieve the desired behavior while still using a block, something like this is required:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;123&amp;quot;&lt;/span&gt;

&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Object&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;o_singleton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;# retrieve singleton class&lt;/span&gt;

&lt;span class="n"&gt;o_singleton&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt; &lt;span class="ss"&gt;:define_method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:return_val&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="c1"&gt;# must use send because define_method is not public&lt;/span&gt;
  &lt;span class="n"&gt;val&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;return_val&lt;/span&gt; &lt;span class="c1"&gt;# prints &amp;quot;123&amp;quot; as expected&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In Ruby 1.9.x, the &lt;strong&gt;define_singleton_method&lt;/strong&gt; method can be used:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;123&amp;quot;&lt;/span&gt;

&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Object&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;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;define_singleton_method&lt;/span&gt; &lt;span class="ss"&gt;:return_val&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;val&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;return_val&lt;/span&gt; &lt;span class="c1"&gt;# prints &amp;quot;123&amp;quot; as expected&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Lesson learned: to define a singleton method using a block able to access local variables, use &lt;strong&gt;Object#define_singleton_method&lt;/strong&gt; (1.9 only) or &lt;strong&gt;Module#define_method&lt;/strong&gt; (on the singleton class).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Object#define_singleton_method&lt;/strong&gt; and &lt;strong&gt;Module#define_method&lt;/strong&gt; do have other uses in Ruby metaprogramming beyond defining these sorts of singleton methods, but more on that in a later post.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/kEH5PIC-R2c" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2011/01/08/defining-methods-using-blocks-in-ruby.html</feedburner:origLink></entry>
 
 <entry>
   <title>P2PU: Introduction to Ruby and Rails</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/KDJIXyTXy_I/p2pu-intro-to-ruby-and-rails.html" />
   <updated>2011-01-07T00:00:00-08:00</updated>
   <id>http://www.andylindeman.com/2011/01/07/p2pu-intro-to-ruby-and-rails</id>
   <content type="html">&lt;p&gt;&lt;span style="float: right;padding-left: 5px; padding-bottom: 5px;"&gt;&lt;img src="http://andylindeman.com/images/posts/rubyfun.png" title="ruby==fun" alt="ruby==fun" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;A few months ago, as I was diving into learning Ruby on Rails, I saw a &lt;a href="http://twitter.com/#!/johndbritton/status/9677343568171008"&gt;tweet by @johndbritton&lt;/a&gt; about a need for organizing an &lt;strong&gt;Introduction to Ruby and Rails&lt;/strong&gt; course for &lt;a href="http://p2pu.org/"&gt;Peer 2 Peer University&lt;/a&gt; (P2PU).  I volunteered to organize and facilitate it.&lt;/p&gt;
&lt;p&gt;A little background on P2PU:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The Peer 2 Peer University (P2PU) is an online community of open study groups for short university-level courses. Think of it as online book clubs for open educational resources. The P2PU helps you navigate the wealth of open education materials that are out there, creates small groups of motivated learners, and supports the design and facilitation of courses. Students and tutors get recognition for their work, and we are building pathways to formal credit as well.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;P2PU courses do have a facilitator, but the learning mostly happens via interactions among those taking the course, as opposed to one professor-like figure doling out knowledge to students who do not necessarily learn much from one other.&lt;/p&gt;
&lt;p&gt;Those who take the Introduction to Ruby and Rails course can expect to complete weekly reading assignments from freely available material, practice with programming assignments, and collaborate with others to explore Ruby, Rails, and the surrounding ecosystem .. as well work through any problems that will inevitably arise! I will also mentor and nudge as required.&lt;/p&gt;
&lt;p&gt;The course is targeted at those who already have &lt;strong&gt;basic web design experience&lt;/strong&gt; and &lt;strong&gt;basic programming knowledge&lt;/strong&gt; (in any other language), but applicants certainly do not need to be an expert at either though.&lt;/p&gt;
&lt;p&gt;If you are interested .. or know someone who may be interested, please &lt;a href="http://p2pu.org/webcraft/introduction-ruby-and-rails"&gt;see the course webpage on p2pu.org&lt;/a&gt;.  Course applications will be accepted starting &lt;strong&gt;12 January 2011&lt;/strong&gt;, but motivated folks can feel free to &lt;a href="http://alindeman.github.com/p2pu-rubyrails/signup-task.html"&gt;complete the required sign-up task&lt;/a&gt; now.&lt;/p&gt;
&lt;p&gt;Questions?  Post a comment here or &lt;a href="http://p2pu.org/webcraft/introduction-ruby-and-rails#comment-form"&gt;on the course page&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/KDJIXyTXy_I" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2011/01/07/p2pu-intro-to-ruby-and-rails.html</feedburner:origLink></entry>
 
 <entry>
   <title>Weekend Project: GitHub Timeline Widget</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/auJLBkg20oQ/github-timeline-widget.html" />
   <updated>2011-01-03T00:00:00-08:00</updated>
   <id>http://www.andylindeman.com/2011/01/03/github-timeline-widget</id>
   <content type="html">&lt;p&gt;&lt;span style="float: left;"&gt;&lt;img src="https://img.skitch.com/20110102-ftt3nhikwxibr2a9i6a4gff6xu.png" title="GitHub Timeline Widget" alt="GitHub Timeline Widget" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Over last weekend, I decided to embark on yet another &amp;#8220;learn new technologies while creating something non-contrived along the way&amp;#8221; project.&lt;/p&gt;
&lt;p&gt;I came up with &lt;strong&gt;&lt;a href="https://github.com/alindeman/github-timeline-widget"&gt;GitHub Timeline Widget&lt;/a&gt;&lt;/strong&gt;, a jQuery plugin that places a widget/badge/sidebar item with your latest actions on GitHub on your website.&lt;/p&gt;
&lt;p&gt;It is similar to &lt;a href="http://drnicjavascript.rubyforge.org/github_badge/"&gt;Dr Nic&amp;#8217;s GitHub Badge&lt;/a&gt;, but instead of displaying your projects, it shows a compact version of what you would see on the Your Actions tab on &lt;a href="https://github.com/dashboard/yours"&gt;your GitHub dashboard&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s &lt;a href="https://github.com/alindeman/github-timeline-widget/blob/master/README.textile"&gt;pretty easy to setup&lt;/a&gt; and you can &lt;a href="http://alindeman.github.com/github-timeline-widget/"&gt;try a live demo first&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For those interested, it&amp;#8217;s written in &lt;a href="http://www.coffeescript.com/"&gt;CoffeeScript&lt;/a&gt; as a &lt;a href="http://www.jquery.com/"&gt;jQuery Plugin&lt;/a&gt; using &lt;a href="https://github.com/pivotal/jasmine"&gt;Jasmine&lt;/a&gt; as a unit testing framework.&lt;/p&gt;
&lt;p&gt;Try it out and &lt;a href="https://github.com/alindeman/github-timeline-widget/issues"&gt;give me feedback&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/auJLBkg20oQ" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2011/01/03/github-timeline-widget.html</feedburner:origLink></entry>
 
 <entry>
   <title>Access to Modified Closure in Ruby</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/r4BK_PAN9wo/access-to-modified-closure.html" />
   <updated>2010-12-22T00:00:00-08:00</updated>
   <id>http://www.andylindeman.com/2010/12/22/access-to-modified-closure</id>
   <content type="html">&lt;p&gt;Ruby&amp;#8217;s &lt;a href="http://ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html#S2"&gt;blocks&lt;/a&gt; are incredibly powerful language constructs and are used throughout idiomatic Ruby code.&lt;/p&gt;
&lt;p&gt;More generally, Ruby&amp;#8217;s blocks are (for the most part) &lt;a href="http://en.wikipedia.org/wiki/Closure_(computer_science)"&gt;&lt;strong&gt;closures&lt;/strong&gt;&lt;/a&gt;.  For instance, they take on the environment in which they were created (often called a &lt;strong&gt;binding&lt;/strong&gt;).&lt;/p&gt;
&lt;p&gt;For example, consider this snippet:&lt;/p&gt;
&lt;script src="https://gist.github.com/751669.js"&gt; &lt;/script&gt;&lt;p&gt;The output of this code is &amp;#8220;1&amp;#8221;&lt;/p&gt;
&lt;p&gt;The key here is that while named the same, &lt;strong&gt;x&lt;/strong&gt; in the &lt;strong&gt;execute&lt;/strong&gt; method is different than &lt;strong&gt;x&lt;/strong&gt; in the top level scope.&lt;/p&gt;
&lt;p&gt;Since blocks are bound to the environment in which they were created, the &lt;strong&gt;x&lt;/strong&gt; within the block always refers to the &lt;strong&gt;x&lt;/strong&gt; in the top level scope.  Setting &lt;strong&gt;x&lt;/strong&gt; within the local scope of the &lt;strong&gt;execute&lt;/strong&gt; method has no effect on the block.&lt;/p&gt;
&lt;p&gt;Now, that doesn&amp;#8217;t mean that the block is bound to the specific &lt;em&gt;value&lt;/em&gt; of &lt;strong&gt;x&lt;/strong&gt; at the time of creation.  Consider:&lt;/p&gt;
&lt;script src="https://gist.github.com/751672.js"&gt; &lt;/script&gt;&lt;p&gt;The output of this code is &amp;#8220;1&amp;#8221; followed by &amp;#8220;3&amp;#8221;&lt;/p&gt;
&lt;p&gt;Contrary to the first snippet, when &lt;strong&gt;x&lt;/strong&gt; is reassigned to 3, it is reassigning the same &lt;strong&gt;x&lt;/strong&gt; as in the environment that the block is bound to.  Reassigning &lt;strong&gt;x&lt;/strong&gt; in this case &lt;em&gt;does&lt;/em&gt; affect the block.&lt;/p&gt;
&lt;p&gt;I think this is enough background to get to the meat of my post: &lt;strong&gt;using blocks within looping constructs can (sometimes) produce odd/unintentional results.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Consider this code (that is intended) to produce an array of actions to print &amp;#8220;1 2 3 4 5&amp;#8221;&lt;/p&gt;
&lt;script src="https://gist.github.com/751719.js"&gt; &lt;/script&gt;&lt;p&gt;If run, though, it outputs &amp;#8220;5 5 5 5 5&amp;#8221;  Huh?&lt;/p&gt;
&lt;p&gt;The issue is that each block is bound to the same local variable &lt;strong&gt;i&lt;/strong&gt;, &lt;em&gt;which is reassigned as the loop progresses&lt;/em&gt;.  When the loop ends, &lt;strong&gt;i&lt;/strong&gt; is set to 5, so every block prints &amp;#8220;5&amp;#8221;&lt;/p&gt;
&lt;p&gt;The reason this issue does not surface very often is that most Rubyists would have written the loop this way:&lt;/p&gt;
&lt;script src="https://gist.github.com/751726.js"&gt; &lt;/script&gt;&lt;p&gt;In this case, &lt;strong&gt;i&lt;/strong&gt; is different variable each time the block passed to &lt;a href="http://ruby-doc.org/core-1.9/classes/Array.html#M000430"&gt;Array#each&lt;/a&gt; is invoked.  Each block is bound to a different environment with a different &lt;strong&gt;i&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Again, this issue does not appear very often because the language looping constructs in Ruby (&lt;strong&gt;for&lt;/strong&gt;, &lt;strong&gt;while&lt;/strong&gt;, &lt;strong&gt;until&lt;/strong&gt;) are not used nearly as often as in many other languages.&lt;/p&gt;
&lt;p&gt;However, even if you never run a situation where this occurs, understanding why it can happen will increase your understanding of blocks and closures &amp;#8230; in Ruby and in other languages.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; While in my examples I use the syntaxes interchangeably, there is some variation in the operation of blocks depending on how they are defined (&lt;strong&gt;Proc.new&lt;/strong&gt; vs. &lt;strong&gt;lambda&lt;/strong&gt; vs. &lt;strong&gt;unnamed block passed to method&lt;/strong&gt;).  I encourage you to read &lt;a href="http://innig.net/software/ruby/closures-in-ruby.rb"&gt;this paper&lt;/a&gt; for more details.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/r4BK_PAN9wo" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2010/12/22/access-to-modified-closure.html</feedburner:origLink></entry>
 
 <entry>
   <title>Recursive Reverse Array in Ruby</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/VnkoKhecmSE/recursive-reverse.html" />
   <updated>2010-12-15T00:00:00-08:00</updated>
   <id>http://www.andylindeman.com/2010/12/15/recursive-reverse</id>
   <content type="html">&lt;p&gt;Someone in &lt;a href="irc://chat.freenode.net/%23rubyonrails"&gt;irc.freenode.net#rubyonrails&lt;/a&gt; wanted help &lt;strong&gt;writing a function to reverse an array using recursion&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The assignment was obviously contrived simply to teach recursion, since &lt;a href="http://ruby-doc.org/core/classes/Array.html#M002183"&gt;Array#reverse&lt;/a&gt; exists in the core library.&lt;/p&gt;
&lt;p&gt;Nevertheless, I thought the solution we came up with was pretty elegant and idiomatic Ruby.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty?&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inspect&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The base case is when the array is empty.  If it is, stop the recursion and return that empty array.&lt;/p&gt;
&lt;p&gt;Otherwise, pop off the last item, placing it at the first position in the new array.  The rest of the resulting array comes from a recursive call, passing in &lt;em&gt;array&lt;/em&gt; (which is now without its last element due to the &lt;a href="http://ruby-doc.org/core/classes/Array.html#M002169"&gt;Array#pop&lt;/a&gt; call).&lt;/p&gt;
&lt;p&gt;The splat (*) argument produces a flattened array (without needing to explicitly call &lt;a href="http://ruby-doc.org/core/classes/Array.html#M002218"&gt;Array#flatten&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Thoughts on it?  Any ways to make it even more elegant or idiomatic?&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;b&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt; 16 December 2010&lt;/b&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Thanks to &lt;a href="http://ckdake.com/"&gt;Chris Kelly&lt;/a&gt; and &lt;a href="http://maraby.org/"&gt;Matt Todd&lt;/a&gt; for commenting.  Matt&amp;#8217;s proposed solution is definitely more idiomatic and elegant than I proposed above.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Array&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;recursive_reverse&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;empty?&lt;/span&gt;
    &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;recursive_reverse&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;recursive_reverse&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inspect&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As &lt;a href="http://www.andylindeman.com/2010/12/15/recursive-reverse.html#comment-112716929"&gt;Matt points out&lt;/a&gt;, this solution takes advantage of Ruby&amp;#8217;s open classes.  It also returns early to avoid the somewhat ugly ternary operator.  Finally, since &lt;em&gt;recursive_reverse&lt;/em&gt; is an instance method, the solution eliminates the need for passing the array as an argument.&lt;/p&gt;
&lt;p&gt;This was fun!  I may try it again in the future with other algorithms.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/VnkoKhecmSE" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2010/12/15/recursive-reverse.html</feedburner:origLink></entry>
 
 <entry>
   <title>Rails Dilemma: HATEOAS in XML/JSON Responses</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/FxGCfcMeQnc/hateoas-in-rails.html" />
   <updated>2010-11-13T00:00:00-08:00</updated>
   <id>http://www.andylindeman.com/2010/11/13/hateoas-in-rails</id>
   <content type="html">&lt;p&gt;One of the tenants of &lt;span class="caps"&gt;REST&lt;/span&gt; is &lt;a href="http://en.wikipedia.org/wiki/HATEOAS"&gt;&lt;span class="caps"&gt;HATEOAS&lt;/span&gt;&lt;/a&gt;.  I understand this to mean that a RESTful response should, if applicable, contain links to the other actions that a client can perform.&lt;/p&gt;
&lt;p&gt;Imagine that my Rails application has a &lt;em&gt;Book&lt;/em&gt; resource.  For the benefit of other applications that use my &lt;span class="caps"&gt;API&lt;/span&gt;, I want to include a &lt;em&gt;purchase_url&lt;/em&gt; attribute, which is the link to the &lt;span class="caps"&gt;URL&lt;/span&gt; where the book can be purchased.&lt;/p&gt;
&lt;p&gt;For instance (in &lt;span class="caps"&gt;JSON&lt;/span&gt;):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="javascript"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;&amp;quot;book&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Ender&amp;#39;s Game&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;&amp;quot;isbn&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;0812550706&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;&amp;quot;purchase_url&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;http://www.example.com/books/1/purchase&amp;quot;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;The problem is that generating an &lt;span class="caps"&gt;XML&lt;/span&gt; or &lt;span class="caps"&gt;JSON&lt;/span&gt; representation of the model (by default) resides within the model itself, which does not have access to the functions needed to generate URLs (which are only accessible in the controller and view).&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;My options, as my current research has uncovered, seem to be:&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;Instead of using the default rendering method (to_xml, to_json), render a view/partial that builds the &lt;span class="caps"&gt;XML&lt;/span&gt; and &lt;span class="caps"&gt;JSON&lt;/span&gt; exactly as I need.  However, this pretty drastically increases the complexity and size of my code and requires constant maintenance as attributes are added or removed.&lt;/li&gt;
	&lt;li&gt;Mix in an ActionController module into my models to generate the URLs, as described in &lt;a href="http://stackoverflow.com/questions/1255426/rails-restful-to-xml-how-to-achieve-connectedness"&gt;this Stack Overflow thread&lt;/a&gt;  However, even Ryan Bates, who answered the question, notes how messy his proposed solution is.&lt;/li&gt;
	&lt;li&gt;Add a new attribute for the &lt;span class="caps"&gt;URL&lt;/span&gt; which is set by the controller (also mentioned in Ryan&amp;#8217;s answer on Stack Overflow).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For now, I&amp;#8217;m using the last option.  For instance, here is the create method of my controller:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# POST /books&lt;/span&gt;
&lt;span class="c1"&gt;# POST /books.xml&lt;/span&gt;
&lt;span class="c1"&gt;# POST /books.json&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;
  &lt;span class="vi"&gt;@book&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Book&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="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:book&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="vi"&gt;@book&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;save&lt;/span&gt;

  &lt;span class="vi"&gt;@book&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:purchase_url&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;purchase_book_url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@book&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;respond_with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@book&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I&amp;#8217;m really not happy with it, though.  First of all, I&amp;#8217;m adding an attribute to a model simply to get it to serialize in a certain way.  Secondly, this approach is not very &lt;span class="caps"&gt;DRY&lt;/span&gt;: every action of every controller that interacts with a Book model needs the code to set purchase_url.  This might be somewhat manageable for one controller, but I imagine it quickly spiraling out of control if the model is ever nested in another resource.&lt;/p&gt;
&lt;p&gt;Any thoughts on a better way?&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/FxGCfcMeQnc" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2010/11/13/hateoas-in-rails.html</feedburner:origLink></entry>
 
 <entry>
   <title>Ignite-Style Talk: Ruby Version Manager</title>
   <link href="http://feedproxy.google.com/~r/alindeman/~3/ezp_gks2kqI/ignite-style-talk-ruby-version-manager.html" />
   <updated>2010-11-09T00:00:00-08:00</updated>
   <id>http://www.andylindeman.com/2010/11/09/ignite-style-talk-ruby-version-manager</id>
   <content type="html">&lt;p&gt;I will be presenting at the &lt;a href="http://www.meetup.com/web-99/"&gt;North Alabama Web Developers Meetup&lt;/a&gt; tomorrow (10 Nov 2010).&lt;/p&gt;
&lt;p&gt;The plan is to do &lt;a href="http://ignite.oreilly.com/"&gt;Ignite&lt;/a&gt; style talks: 5 minutes, 20 slides that auto-advance every 15 seconds.&lt;/p&gt;
&lt;p&gt;While many Ignite talks are non-technical, I chose to present about &lt;a href="http://rvm.beginrescueend.com/"&gt;rvm&lt;/a&gt; (Ruby Version Manager).&lt;/p&gt;
&lt;p&gt;If you&amp;#8217;d like to follow along, comment, or found yourself here simply looking for an introduction to the tool, I&amp;#8217;ve posted my slides below:&lt;br /&gt;
&lt;iframe src="https://docs.google.com/present/embed?id=dfnqrft2_45d36vhdhp&amp;amp;size=m" width="555" height="451"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/alindeman/~4/ezp_gks2kqI" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://www.andylindeman.com/2010/11/09/ignite-style-talk-ruby-version-manager.html</feedburner:origLink></entry>
 
 
</feed>
