<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    <title>Adventures In Coding</title>
    <link>http://adventuresincoding.com</link>
    <description>Kevin Faustino's technical blog about Ruby, Ruby on Rails, and JavaScript</description>
    <lastBuildDate>2013-05-25 20:21:12 +0000</lastBuildDate>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/AdventuresInCoding" /><feedburner:info uri="adventuresincoding" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
      <title>Now writing at The Remarkable Labs Blog</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/XRVc_NauDw8/now-writing-at-the-remarkable-labs-blog</link>
      <pubDate>Mon, 29 Oct 2012 19:35:50 +0000</pubDate>
      <author />
      <guid isPermaLink="false">http://adventuresincoding.com/2012/10/now-writing-at-the-remarkable-labs-blog</guid>
      <description>&lt;p&gt;As some of you have noticed, Adventures in Coding has been missing in action for a while. This is because over the last couple of months, I have been starting a new Ruby on Rails consultancy based in Toronto, Ontario. The company is called &lt;a href="http://www.remarkablelabs.com"&gt;Remarkable Labs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This past week, I launched the company blog at &lt;a href="http://blog.remarkablelabs.com"&gt;http://blog.remarkablelabs.com&lt;/a&gt;. This is where I will continue sharing knowledge and tips about Ruby and various other technologies.&lt;/p&gt;

&lt;p&gt;Adventures in Coding will remain online as a reference, but I will no longer be actively posting here. You can subscribe to the Remarkable Labs blog &lt;a href="http://feeds.feedburner.com/remarkablelabs"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks, 
Kevin Faustino&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/XRVc_NauDw8" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2012/10/now-writing-at-the-remarkable-labs-blog</feedburner:origLink></item>
    <item>
      <title>A review of "The Rails View" by Bruce Williams and John Athayde</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/QJQ-ywyCw2U/a-review-of-the-rails-view-by-bruce-williams-and-john-athayde</link>
      <pubDate>Tue, 05 Jun 2012 10:44:17 +0000</pubDate>
      <author />
      <guid isPermaLink="false">http://adventuresincoding.com/2012/06/a-review-of-the-rails-view-by-bruce-williams-and-john-athayde</guid>
      <description>&lt;p&gt;&lt;a href="http://pragprog.com/book/warv/the-rails-view"&gt;&lt;em&gt;The Rails View: Create a Beautiful and Maintainable User Experience&lt;/em&gt;&lt;/a&gt; is a book published by the Pragmatic Bookshelf by &lt;a href="https://twitter.com/wbruce"&gt;Bruce Williams&lt;/a&gt; and &lt;a href="http://www.boboroshi.com"&gt;John Athayde&lt;/a&gt;. It is the most comprehensive resource on the view layer in Ruby on Rails available, clocking in at 264 pages and 9 chapters.&lt;/p&gt;

&lt;p&gt;The book explains each concept to the reader by building an application &lt;em&gt;ArtFlow&lt;/em&gt;. Each chapter builds upon the content in the previous, and provides a good sense of the order in which things should be done while developing.&lt;/p&gt;

&lt;p&gt;Despite the fact that I have been working with Rails for years, I found the book to be an excellent reference and filled with things I wasn&amp;#39;t aware of. For example, having read multiple HTML5 books, I was surprised to discover &lt;code&gt;&amp;lt;aside&amp;gt;&lt;/code&gt; tags are not to be used for layout sidebars. While some of the chapters were introductions to technologies such as Sass, the authors always provided enough tips and links to further explore each topic. &lt;/p&gt;

&lt;h2&gt;Who Should Read It?&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Anybody working with Ruby and maintain views for web applications. While some of the chapters are Ruby on Rails specific, many of these concepts can be applied to any Ruby web framework such as Sinatra. &lt;/li&gt;
&lt;li&gt;Rails developers looking for a great reference for building a maintainable view layer.&lt;/li&gt;
&lt;li&gt;Experienced Rails developers looking to learn a couple new tips and tricks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;The Chapters&lt;/h2&gt;

&lt;p&gt;Here is a list of what is covered in &lt;em&gt;The Rails View&lt;/em&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creating an Application Layout&lt;/li&gt;
&lt;li&gt;Improving Readability&lt;/li&gt;
&lt;li&gt;Adding Cascading Style Sheets&lt;/li&gt;
&lt;li&gt;Adding JavaScript&lt;/li&gt;
&lt;li&gt;Building Maintainable Forms&lt;/li&gt;
&lt;li&gt;Using Presenters&lt;/li&gt;
&lt;li&gt;Handling Mobile Views&lt;/li&gt;
&lt;li&gt;Working with Email&lt;/li&gt;
&lt;li&gt;Optimizing Performance &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Beautiful Markup&lt;/h2&gt;

&lt;p&gt;When reading the book, I was constantly reminded of a presentation I watched on the Envy Labs Blog named &lt;a href="http://blog.envylabs.com/2010/06/beautiful-markup"&gt;&amp;quot;Beautiful Markup&amp;quot;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Only after taking a look at the video again did I realaize that it was done by co-author of &lt;em&gt;The Rails View&lt;/em&gt;, John Athayde.&lt;/p&gt;

&lt;p&gt;While the presentation is from 2010, it is a great compliment to the book. Many of the topics are still relevent today and are expanded upon in the book.&lt;/p&gt;

&lt;div class="video-container"&gt;
&lt;iframe id="viddler-a7681a7b" src="//www.viddler.com/embed/a7681a7b/?f=1&amp;autoplay=0&amp;player=full&amp;loop=false&amp;nologo=false&amp;hd=false" width="545" height="451" frameborder="0" mozallowfullscreen="true" webkitallowfullscreen="true"&gt;&lt;/iframe&gt;
&lt;/div&gt;

&lt;h2&gt;Verdict&lt;/h2&gt;

&lt;p&gt;If you are developing Ruby on Rails applications for a living, you should pick up &lt;em&gt;The Rails View&lt;/em&gt;. While some areas may be to introductory for some developers, there will always be enough new knowledge to be discovered that more than pays for the book.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/QJQ-ywyCw2U" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2012/06/a-review-of-the-rails-view-by-bruce-williams-and-john-athayde</feedburner:origLink></item>
    <item>
      <title>Using elasticsearch with Heroku</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/3PCejuylZbY/using-elasticsearch-with-heroku</link>
      <pubDate>Thu, 31 May 2012 18:18:30 +0000</pubDate>
      <author />
      <guid isPermaLink="false">http://adventuresincoding.com/2012/05/using-elasticsearch-with-heroku</guid>
      <description>&lt;p&gt;In my previous post, I wrote about &lt;a href="http://adventuresincoding.com/2012/05/full-text-search-in-rails-using-elasticsearch"&gt;how to integrate elasticsearch in a Ruby on Rails application&lt;/a&gt;. The next step after you have everything up and running is to deploy your app to production. I will detail how to do this via Heroku, since &lt;a href="http://www.elasticsearch.org/tutorials/2012/03/21/deploying-elasticsearch-with-chef-solo.html"&gt;other deployment strategies&lt;/a&gt; have been well documented.&lt;/p&gt;

&lt;h2&gt;Bonsai ElasticSearch&lt;/h2&gt;

&lt;p&gt;Heroku has a wide variety of addons including Bonsai ElasticSearch. Bonsai allows you to have an elasticsearch server without having to write a single line of configuration.  Add the addon to your application via the command-line:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;heroku addons:add bonsai:test
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Configuring Tire to work with Bonsai&lt;/h2&gt;

&lt;p&gt;Nick Zadrozny wrote a &lt;a href="https://gist.github.com/2041121"&gt;gist&lt;/a&gt; detailing a technique he used to use Bonsai in production and a local elasticsearch server for development. &lt;/p&gt;

&lt;p&gt;To begin, we are going to create an initializer that sets a constant &lt;code&gt;BONSAI_INDEX_NAME&lt;/code&gt; that will be used by our indexed model. First we determine if an environment variable is set for &lt;code&gt;BONSAI_INDEX_URL&lt;/code&gt;. The &lt;code&gt;BONSAI_INDEX_URL&lt;/code&gt; is the address of your Bonasi elasticsearch server. If present, we configure &lt;code&gt;Tire&lt;/code&gt; to use &lt;a href="http://index.bonsai.io"&gt;http://index.bonsai.io&lt;/a&gt; as the base url for our elasticsearch calls and set our index name to the unique bonasi id found in the address. Otherwise, we use the technique described in my &lt;a href="http://adventuresincoding.com/2012/05/full-text-search-in-rails-using-elasticsearch"&gt;previous post&lt;/a&gt; to ensure a unique index per environment.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# config/initializers/bonsai.rb&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;BONSAI_INDEX_URL&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
  &lt;span class="no"&gt;Tire&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="n"&gt;url&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;http://index.bonsai.io&amp;quot;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="no"&gt;BONSAI_INDEX_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;BONSAI_INDEX_URL&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="sr"&gt;/[^\/]+$/&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="n"&gt;app_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parent_name&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;underscore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dasherize&lt;/span&gt;
  &lt;span class="no"&gt;BONSAI_INDEX_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;app_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;-&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Next update the &lt;code&gt;index_name&lt;/code&gt; set on your indexed model to use &lt;code&gt;BONSAI_INDEX_NAME&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt;
  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
  &lt;span class="n"&gt;index_name&lt;/span&gt; &lt;span class="no"&gt;BONSAI_INDEX_NAME&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;At this point, your application is ready for deployment to Heroku.&lt;/p&gt;

&lt;h2&gt;Indexing&lt;/h2&gt;

&lt;p&gt;Once you deploy your Heroku application, you must create and populate the elasticsearch index. We do this by running the Heroku console and entering the following code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create_elasticsearch_index&lt;/span&gt; &lt;span class="c1"&gt;# Post is our example model&lt;/span&gt;
&lt;span class="no"&gt;Tire&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="no"&gt;BONSAI_INDEX_NAME&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; 
   &lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;all&lt;/span&gt;
   &lt;span class="n"&gt;refresh&lt;/span&gt;
 &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Once your index is in place, test your application on production to see if you are receiving search results. &lt;/p&gt;

&lt;h2&gt;Final Note&lt;/h2&gt;

&lt;p&gt;The Bonasi addon is in public beta at the time of writing. This is just a word of warning if you have a mission critical application and you wanted to try elasticsearch out.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/3PCejuylZbY" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2012/05/using-elasticsearch-with-heroku</feedburner:origLink></item>
    <item>
      <title>Full-Text Search in Rails using elasticsearch</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/5BTlJwJxbnE/full-text-search-in-rails-using-elasticsearch</link>
      <pubDate>Wed, 30 May 2012 20:06:26 +0000</pubDate>
      <author />
      <guid isPermaLink="false">http://adventuresincoding.com/2012/05/full-text-search-in-rails-using-elasticsearch</guid>
      <description>&lt;p&gt;Searching on websites is important from a content discovery and user usability perspective. It allows readers to control the way they look for content instead of navigating through menus. Given this blog was missing full-text search, I decided to implement it using elasticsearch. Here is a detailed step-by-step guide on how I did that using outside-in development.&lt;/p&gt;

&lt;h2&gt;elasticsearch&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://www.elasticsearch.org/"&gt;elasticsearch&lt;/a&gt; is a distributed open source search server based on Apache Lucene. It allows for real-time searching and the ability to scale easily through replicas. Getting started is easy as elasticsearch is schema less. You only have to pass it a typed JSON document and it will automatically be indexed for you. Types are automatically determined by the server. It also allows you to define your own mappings to set boost levels, analyzers, and types.&lt;/p&gt;

&lt;h2&gt;Blog Engine Background&lt;/h2&gt;

&lt;p&gt;The code examples are in the post are extracted from the Adventures in Coding blog engine source code. It is a Ruby on Rails 3.1 application with a MongoDB database. All testing is done via Cucumber and RSpec. Views and CSS are generated with Haml and Sass respectively.&lt;/p&gt;

&lt;h2&gt;Getting Started&lt;/h2&gt;

&lt;p&gt;The first step in getting started is running an elasticsearch server locally for development. I do all my development on a Mac, so I easily installed it via &lt;code&gt;homebrew&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;brew install elasticsearch
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If you are running another platform, you can build elasticsearch from &lt;a href="http://www.elasticsearch.org/download/"&gt;source&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once installed, I launched the server running:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;elasticsearch -f -D es.config&lt;span class="o"&gt;=&lt;/span&gt;/usr/local/Cellar/elasticsearch/0.19.3/config/elasticsearch.yml
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Tire&lt;/h2&gt;

&lt;p&gt;Knowing that I am going to need a elasticsearch client, I selected the most popular Ruby client &lt;a href="https://github.com/karmi/tire"&gt;Tire&lt;/a&gt;. It is actively maintained, has a large amount of users, and also integrates nicely with Rails and ActiveModel.&lt;/p&gt;

&lt;p&gt;To add it to the project, I added it to my &lt;em&gt;Gemfile&lt;/em&gt;, and ran &lt;code&gt;bundle&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# Gemfile&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;tire&amp;#39;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Search Feature&lt;/h2&gt;

&lt;p&gt;Now that everything is setup, I defined my Cucumber &amp;quot;Search&amp;quot; feature and scenarios which will drive development.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c"&gt;# features/search.feature&lt;/span&gt;&lt;span class="nf"&gt;&lt;/span&gt;
&lt;span class="k"&gt;Feature:&lt;/span&gt;&lt;span class="nf"&gt; Search&lt;/span&gt;
&lt;span class="nf"&gt;  As a User&lt;/span&gt;
&lt;span class="nf"&gt;  I want to search for content&lt;/span&gt;
&lt;span class="nf"&gt;  In order to find content easily over paginating&lt;/span&gt;

&lt;span class="nf"&gt;  &lt;/span&gt;&lt;span class="k"&gt;Background:&lt;/span&gt;&lt;span class="nf"&gt;&lt;/span&gt;
&lt;span class="k"&gt;   Given &lt;/span&gt;&lt;span class="nf"&gt;I am on the home page&lt;/span&gt;

&lt;span class="nf"&gt;  &lt;/span&gt;&lt;span class="k"&gt;Scenario:&lt;/span&gt;&lt;span class="nf"&gt; Find posts by content&lt;/span&gt;
&lt;span class="k"&gt;    Given &lt;/span&gt;&lt;span class="nf"&gt;the following posts:&lt;/span&gt;
&lt;span class="k"&gt;      |&lt;/span&gt;&lt;span class="s"&gt; title&lt;/span&gt;&lt;span class="k"&gt;             |&lt;/span&gt;&lt;span class="nf"&gt;&lt;/span&gt;
&lt;span class="k"&gt;      |&lt;/span&gt;&lt;span class="s"&gt; Demon in a Bottle&lt;/span&gt;&lt;span class="k"&gt; |&lt;/span&gt;&lt;span class="nf"&gt;&lt;/span&gt;
&lt;span class="k"&gt;      |&lt;/span&gt;&lt;span class="s"&gt; Extremis&lt;/span&gt;&lt;span class="k"&gt;          |&lt;/span&gt;&lt;span class="nf"&gt;&lt;/span&gt;
&lt;span class="nf"&gt;    &lt;/span&gt;&lt;span class="k"&gt;When &lt;/span&gt;&lt;span class="nf"&gt;I search for &amp;quot;&lt;/span&gt;&lt;span class="s"&gt;Demon&lt;/span&gt;&lt;span class="nf"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="nf"&gt;    &lt;/span&gt;&lt;span class="k"&gt;Then &lt;/span&gt;&lt;span class="nf"&gt;I should be on the search page&lt;/span&gt;
&lt;span class="nf"&gt;    &lt;/span&gt;&lt;span class="k"&gt;And &lt;/span&gt;&lt;span class="nf"&gt;I should see the post called &amp;quot;&lt;/span&gt;&lt;span class="s"&gt;Demon in a Bottle&lt;/span&gt;&lt;span class="nf"&gt;&amp;quot; in the post list&lt;/span&gt;
&lt;span class="nf"&gt;    &lt;/span&gt;&lt;span class="k"&gt;But &lt;/span&gt;&lt;span class="nf"&gt;I should not see the post called &amp;quot;&lt;/span&gt;&lt;span class="s"&gt;Extremis&lt;/span&gt;&lt;span class="nf"&gt;&amp;quot; in the post list&lt;/span&gt;

&lt;span class="nf"&gt;  &lt;/span&gt;&lt;span class="k"&gt;Scenario:&lt;/span&gt;&lt;span class="nf"&gt; No posts found&lt;/span&gt;
&lt;span class="k"&gt;    When &lt;/span&gt;&lt;span class="nf"&gt;I search for &amp;quot;&lt;/span&gt;&lt;span class="s"&gt;Armor Wars&lt;/span&gt;&lt;span class="nf"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="nf"&gt;    &lt;/span&gt;&lt;span class="k"&gt;Then &lt;/span&gt;&lt;span class="nf"&gt;I should be on the search page&lt;/span&gt;
&lt;span class="nf"&gt;    &lt;/span&gt;&lt;span class="k"&gt;And &lt;/span&gt;&lt;span class="nf"&gt;I should see a message indicating no posts were found&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;When I ran &lt;code&gt;cucumber&lt;/code&gt;, my search scenarios failed because I haven&amp;#39;t defined two step definitions. Cucumber informs me of this with the following output:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nf"&gt;You can implement step definitions for undefined steps with these snippets:&lt;/span&gt;

&lt;span class="k"&gt;When &lt;/span&gt;&lt;span class="nf"&gt;/^I search for &amp;quot;&lt;/span&gt;&lt;span class="s"&gt;([^&lt;/span&gt;&lt;span class="nf"&gt;&amp;quot;]*)&amp;quot;&lt;/span&gt;&lt;span class="s"&gt;$/ do |arg1|&lt;/span&gt;
&lt;span class="s"&gt;  pending # express the regexp above with the code you wish you had&lt;/span&gt;
&lt;span class="s"&gt;end&lt;/span&gt;

&lt;span class="s"&gt;Then /^I should see a message indicating no posts were found$/ do&lt;/span&gt;
&lt;span class="s"&gt;  pending # express the regexp above with the code you wish you had&lt;/span&gt;
&lt;span class="s"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Wanting to keep all search related step definitions together to keep things organized, I created the file &lt;em&gt;features/step_definitions/search_steps.rb&lt;/em&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c"&gt;# features/step_definitions/search_steps.rb&lt;/span&gt;&lt;span class="nf"&gt;&lt;/span&gt;
&lt;span class="k"&gt;When &lt;/span&gt;&lt;span class="nf"&gt;/^I search for &amp;quot;&lt;/span&gt;&lt;span class="s"&gt;([^&lt;/span&gt;&lt;span class="nf"&gt;&amp;quot;]*)&amp;quot;&lt;/span&gt;&lt;span class="s"&gt;$/ do |query|&lt;/span&gt;
&lt;span class="s"&gt;  fill_in &amp;#39;query&amp;#39;, with: query&lt;/span&gt;
&lt;span class="s"&gt;  click_button &amp;#39;Search&amp;#39;&lt;/span&gt;
&lt;span class="s"&gt;end&lt;/span&gt;

&lt;span class="s"&gt;Then /^I should see a message indicating no posts were found$/ do&lt;/span&gt;
&lt;span class="s"&gt;  within(&amp;#39;section.posts&amp;#39;) do&lt;/span&gt;
&lt;span class="s"&gt;    page.should have_content(&amp;#39;No posts were found&amp;#39;)&lt;/span&gt;
&lt;span class="s"&gt;  end&lt;/span&gt;
&lt;span class="s"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Running &lt;code&gt;cucumber&lt;/code&gt; again indicated that it couldn&amp;#39;t find a query text field in the markup.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;When &lt;/span&gt;&lt;span class="nf"&gt;I search for &amp;quot;&lt;/span&gt;&lt;span class="s"&gt;Demon&lt;/span&gt;&lt;span class="nf"&gt;&amp;quot; &lt;/span&gt;&lt;span class="c"&gt;# features/step_definitions/search_steps.rb:1&lt;/span&gt;&lt;span class="nf"&gt;&lt;/span&gt;
&lt;span class="nf"&gt;  cannot fill in, no text field, text area or password field with id, name, or label &amp;#39;query&amp;#39; found (Capybara::ElementNotFound)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To solve this, I needed to create a new search controller, a form to submit the search query, and a search index view. 
Once implemented, Cucumber would be able to successfully interact with the search form and endpoints. Since I hadn&amp;#39;t wired up the &lt;code&gt;Post&lt;/code&gt; model to &lt;code&gt;Tire&lt;/code&gt; yet, the scenarios were still failing.&lt;/p&gt;

&lt;h3&gt;Search Form&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nt"&gt;%section&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="s1"&gt;&amp;#39;search&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;form_tag&lt;/span&gt; &lt;span class="n"&gt;search_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="ss"&gt;:get&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text_field_tag&lt;/span&gt; &lt;span class="ss"&gt;:query&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;:query&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;autocomplete&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="ss"&gt;:off&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;e.g. Ruby&amp;#39;&lt;/span&gt;
    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;submit_tag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Search&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;Controller&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# app/controllers/search_controller.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SearchController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
    &lt;span class="vi"&gt;@posts&lt;/span&gt; &lt;span class="o"&gt;=&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;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;Route&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# config/routes.rb:&lt;/span&gt;
&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;search&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;search#index&amp;#39;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;Helper&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# app/helpers/posts_helper.rb:&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;PostsHelper&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;render_posts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;posts&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
      &lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="n"&gt;content_tag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:div&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;No posts were found&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;message&amp;#39;&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;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;Search index view&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nt"&gt;%section&lt;/span&gt;&lt;span class="nc"&gt;.posts&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;render_posts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@posts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Adding Tire to ActiveModel&lt;/h2&gt;

&lt;p&gt;To make my &lt;code&gt;Post&lt;/code&gt; model searchable, I included two modules:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Tire&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Search&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Tire&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Callbacks&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now when I save a document, it will automatically create/update the elasticsearch index.&lt;/p&gt;

&lt;p&gt;Since I am running elasticsearch in both my test and development environments, I needed to create a unique elasticsearch index name per environment. This ensured when I ran my Cucumber features, that my development indexes were not wiped out. I accomplished this by setting &lt;code&gt;index_name&lt;/code&gt; to &lt;code&gt;&amp;quot;blog-engine-#{Rails.env}&amp;quot;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I also wanted control of what attributes are sent to Tire for indexing. If Tire finds a &lt;code&gt;to_indexed_json&lt;/code&gt; method on your model, it will use that over &lt;code&gt;to_json&lt;/code&gt;. I decided to index the model _id, title, content, and published_at date.&lt;/p&gt;

&lt;p&gt;Finally instead of relying on a dynamic schema, I defined a mapping which is explicit in how posts are analyzed and scored. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fields such as _id and published_at are indexed, but not analyzed. &lt;/li&gt;
&lt;li&gt;Post title&amp;#39;s are given a boost rating of 100 over the default 1.0.&lt;/li&gt;
&lt;li&gt;Blog post text fields are analyzed with a &lt;a href="http://www.elasticsearch.org/guide/reference/index-modules/analysis/snowball-analyzer.html"&gt;snowball analyzer&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the final &lt;em&gt;app/models/post.rb&lt;/em&gt; code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Tire&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Search&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Tire&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Callbacks&lt;/span&gt;

  &lt;span class="n"&gt;index_name&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;blog-engine-&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;

  &lt;span class="n"&gt;mapping&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;indexes&lt;/span&gt; &lt;span class="ss"&gt;:_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="ss"&gt;:not_analyzed&lt;/span&gt;
    &lt;span class="n"&gt;indexes&lt;/span&gt; &lt;span class="ss"&gt;:title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;analyzer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;snowball&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;boost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
    &lt;span class="n"&gt;indexes&lt;/span&gt; &lt;span class="ss"&gt;:content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;analyzer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;snowball&amp;#39;&lt;/span&gt;
    &lt;span class="n"&gt;indexes&lt;/span&gt; &lt;span class="ss"&gt;:published_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;date&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="ss"&gt;:not_analyzed&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_indexed_json&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;published_at&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;published_at&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_json&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Handling requests to elasticsearch from RSpec&lt;/h2&gt;

&lt;p&gt;To ensure no outside HTTP requests to elasticsearch are made when running specs, I added a dependency to &lt;a href="https://github.com/bblimke/webmock"&gt;webmock&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To install &lt;code&gt;webmock&lt;/code&gt; add it to your &lt;em&gt;Gemfile&lt;/em&gt; under the test group:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# Gemfile&lt;/span&gt;
&lt;span class="n"&gt;group&lt;/span&gt; &lt;span class="ss"&gt;:test&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;webmock&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In my &lt;em&gt;spec/spec_helper.rb&lt;/em&gt; file, I required the webmock library and stubbed out any request to the elasticsearch server (http://localhost:9200) in a before filter.&lt;/p&gt;

&lt;p&gt;Here is an abbreviated version of my &lt;em&gt;specs/spec_helper.rb&lt;/em&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&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;webmock/rspec&amp;#39;&lt;/span&gt;
&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;span class="no"&gt;RSpec&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="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;before&lt;/span&gt; &lt;span class="ss"&gt;:each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;stub_request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sr"&gt;/.*localhost:9200\/.*/&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_return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;{}&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="no"&gt;Mongoid&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;purge!&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If you are interested in finding out more about &lt;code&gt;webmock&lt;/code&gt;, be sure to read the &lt;a href="https://github.com/bblimke/webmock"&gt;README&lt;/a&gt; on GitHub.&lt;/p&gt;

&lt;h2&gt;Handling search calls to elasticsearch&lt;/h2&gt;

&lt;p&gt;Finally, I need to implement a method that actually used elasticsearch to search for posts. I didn&amp;#39;t want the method to live in my Post model, so I created a class &lt;code&gt;PostRepository&lt;/code&gt;. I did this to keep view specific logic, such as pagination, away from my domain models.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;PostRepository&lt;/code&gt; takes a hash of params from a controller. Using those params, it will extract the query and current page of results. &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# app/models/post_repository.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PostRepository&lt;/span&gt;
  &lt;span class="no"&gt;POSTS_PER_PAGE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;

  &lt;span class="kp"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:params&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;search&lt;/span&gt;
    &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:query&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tire&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;load&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kp"&gt;true&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="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:page&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;per_page&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;POSTS_PER_PAGE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default_operator&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;AND&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;present?&lt;/span&gt;
      &lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="ss"&gt;:range&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;published_at&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;lte&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;zone&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="n"&gt;sort&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="ss"&gt;:published_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;desc&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blank?&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="kp"&gt;protected&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;model&lt;/span&gt;
    &lt;span class="no"&gt;Post&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Here is a breakdown of what is occurs in the search method:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I accessed the Tire search method from the &lt;code&gt;Post.tire&lt;/code&gt; accessor.&lt;/li&gt;
&lt;li&gt;Passing in option &lt;code&gt;load: true&lt;/code&gt; to &lt;code&gt;search&lt;/code&gt; queries the database for the models. If you store all your attributes in elasticsearch then this isn&amp;#39;t needed. &lt;/li&gt;
&lt;li&gt; &lt;code&gt;query { string query, default_operator: &amp;quot;AND&amp;quot; } if query.present?&lt;/code&gt;: I query elasticsearch for the query string passed by the user. This string is analyzed using a AND operator.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;filter :range, published_at: { lte: Time.zone.now }&lt;/code&gt;: I ensure that only published articles are returned in the results by filtering for dates less than or equal to the current time.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sort { by :published_at, &amp;quot;desc&amp;quot; } if query.blank?&lt;/code&gt;: If a user enters no query string, I return the results in descending order of their published date.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I also updated the search_controller to use the new search method:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c"&gt;# app/controllers/search_controller.rb&lt;/span&gt;
&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="n"&gt;SearchController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="nb"&gt;index&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="n"&gt;posts&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PostRepository&lt;/span&gt;&lt;span class="p"&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="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Returning to Cucumber&lt;/h2&gt;

&lt;p&gt;Running the Cucumber search scenarios still resulted in failure. One of the main reasons is that I was not creating the Post index on each scenario run. To fix this, I added a &lt;code&gt;Before&lt;/code&gt; hook to delete all existing indexes and recreate them based on my mappings.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# features/support/hooks.rb&lt;/span&gt;
&lt;span class="no"&gt;Before&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tire&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delete&lt;/span&gt;
  &lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create_elasticsearch_index&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The features ran again, but still ended up in failure. Calls to elasticsearch were being done asynchronously, whereas my scenarios were being executed synchronously. The solution was to add a synchronization point in the code somewhere. The best place I thought was before any search execution. I updated the step definition &lt;code&gt;/^I search for &amp;quot;([^&amp;quot;]*)&amp;quot;$/&lt;/code&gt; to include an index refresh call &lt;code&gt;Post.tire.index.refresh&lt;/code&gt;. This step definition would be executed after any data has been setup in a &lt;code&gt;Given&lt;/code&gt; step.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# features/step_definitions/search_steps.rb&lt;/span&gt;
&lt;span class="no"&gt;When&lt;/span&gt; &lt;span class="sr"&gt;/^I search for &amp;quot;([^&amp;quot;]*)&amp;quot;$/&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;query&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tire&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refresh&lt;/span&gt;
  &lt;span class="n"&gt;fill_in&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;query&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;
  &lt;span class="n"&gt;click_button&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Search&amp;#39;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Running &lt;code&gt;cucumber&lt;/code&gt; again resulted in success:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;Feature:&lt;/span&gt;&lt;span class="nf"&gt; Search&lt;/span&gt;
&lt;span class="nf"&gt;  As a User&lt;/span&gt;
&lt;span class="nf"&gt;  I want to search for content&lt;/span&gt;
&lt;span class="nf"&gt;  In order to find content easily over paginating&lt;/span&gt;

&lt;span class="nf"&gt;  &lt;/span&gt;&lt;span class="k"&gt;Background:&lt;/span&gt;&lt;span class="nf"&gt;&lt;/span&gt;
&lt;span class="k"&gt;    Given &lt;/span&gt;&lt;span class="nf"&gt;I am on the home page &lt;/span&gt;

&lt;span class="nf"&gt;  &lt;/span&gt;&lt;span class="k"&gt;Scenario:&lt;/span&gt;&lt;span class="nf"&gt; Find posts by content &lt;/span&gt;
&lt;span class="k"&gt;    Given &lt;/span&gt;&lt;span class="nf"&gt;the following posts:   &lt;/span&gt;
&lt;span class="k"&gt;      |&lt;/span&gt;&lt;span class="s"&gt; title&lt;/span&gt;&lt;span class="k"&gt;             |&lt;/span&gt;&lt;span class="nf"&gt;&lt;/span&gt;
&lt;span class="k"&gt;      |&lt;/span&gt;&lt;span class="s"&gt; Demon in a Bottle&lt;/span&gt;&lt;span class="k"&gt; |&lt;/span&gt;&lt;span class="nf"&gt;&lt;/span&gt;
&lt;span class="k"&gt;      |&lt;/span&gt;&lt;span class="s"&gt; Extremis&lt;/span&gt;&lt;span class="k"&gt;          |&lt;/span&gt;&lt;span class="nf"&gt;&lt;/span&gt;
&lt;span class="nf"&gt;    &lt;/span&gt;&lt;span class="k"&gt;When &lt;/span&gt;&lt;span class="nf"&gt;I search for &amp;quot;&lt;/span&gt;&lt;span class="s"&gt;Demon&lt;/span&gt;&lt;span class="nf"&gt;&amp;quot;    &lt;/span&gt;
&lt;span class="nf"&gt;    &lt;/span&gt;&lt;span class="k"&gt;Then &lt;/span&gt;&lt;span class="nf"&gt;I should be on the search page &lt;/span&gt;
&lt;span class="nf"&gt;    &lt;/span&gt;&lt;span class="k"&gt;And &lt;/span&gt;&lt;span class="nf"&gt;I should see the post called &amp;quot;&lt;/span&gt;&lt;span class="s"&gt;Demon in a Bottle&lt;/span&gt;&lt;span class="nf"&gt;&amp;quot; in the post list&lt;/span&gt;
&lt;span class="nf"&gt;    &lt;/span&gt;&lt;span class="k"&gt;But &lt;/span&gt;&lt;span class="nf"&gt;I should not see the post called &amp;quot;&lt;/span&gt;&lt;span class="s"&gt;Extremis&lt;/span&gt;&lt;span class="nf"&gt;&amp;quot; in the post list&lt;/span&gt;

&lt;span class="nf"&gt;  &lt;/span&gt;&lt;span class="k"&gt;Scenario:&lt;/span&gt;&lt;span class="nf"&gt; No posts found  &lt;/span&gt;
&lt;span class="k"&gt;    When &lt;/span&gt;&lt;span class="nf"&gt;I search for &amp;quot;&lt;/span&gt;&lt;span class="s"&gt;Armor Wars&lt;/span&gt;&lt;span class="nf"&gt;&amp;quot; &lt;/span&gt;
&lt;span class="nf"&gt;    &lt;/span&gt;&lt;span class="k"&gt;Then &lt;/span&gt;&lt;span class="nf"&gt;I should be on the search page &lt;/span&gt;
&lt;span class="nf"&gt;    &lt;/span&gt;&lt;span class="k"&gt;And &lt;/span&gt;&lt;span class="nf"&gt;I should see a message indicating no posts were found&lt;/span&gt;

&lt;span class="s"&gt;2&lt;/span&gt;&lt;span class="nf"&gt; scenarios (&lt;/span&gt;&lt;span class="s"&gt;2&lt;/span&gt;&lt;span class="nf"&gt; passed)&lt;/span&gt;
&lt;span class="s"&gt;10&lt;/span&gt;&lt;span class="nf"&gt; steps (&lt;/span&gt;&lt;span class="s"&gt;10&lt;/span&gt;&lt;span class="nf"&gt; passed)&lt;/span&gt;
&lt;span class="s"&gt;0&lt;/span&gt;&lt;span class="nf"&gt;m&lt;/span&gt;&lt;span class="s"&gt;1.315&lt;/span&gt;&lt;span class="nf"&gt;s&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;After thoughts&lt;/h2&gt;

&lt;p&gt;I love developing outside-in as it allows you to know your progress every step of the development process. The process shown in this post was not your typical Cucumber example. Adding external services as a dependency to your integration test suite is something I always try to avoid. However, having the confidence that I will always know if my features are working outweighs that negative. &lt;/p&gt;

&lt;p&gt;Also, be sure to read about &lt;a href="http://adventuresincoding.com/2012/05/using-elasticsearch-with-heroku"&gt;using elasticsearch witin Heroku&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/5BTlJwJxbnE" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2012/05/full-text-search-in-rails-using-elasticsearch</feedburner:origLink></item>
    <item>
      <title>How to configure Cucumber and RSpec to work with Mongoid 3.0</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/jaUxedNvI5s/how-to-configure-cucumber-and-rspec-to-work-with-mongoid-30</link>
      <pubDate>Mon, 28 May 2012 21:07:11 +0000</pubDate>
      <author />
      <guid isPermaLink="false">http://adventuresincoding.com/2012/05/how-to-configure-cucumber-and-rspec-to-work-with-mongoid-30</guid>
      <description>&lt;p&gt;With the upcoming release of &lt;a href="http://mongoid.org/en/mongoid/index.html"&gt;Mongoid 3.0&lt;/a&gt;, I have decided to update a &lt;a href="http://adventuresincoding.com/2010/07/how-to-configure-cucumber-and-rspec-to-work-with-mongoid"&gt;previous post&lt;/a&gt; about getting Mongoid to play nice with RSpec and Cucumber.&lt;/p&gt;

&lt;p&gt;RSpec and Cucumber by default are setup to work with relational databases using ActiveRecord. To get them to work with Mongoid, we had previously run: &lt;code&gt;Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&amp;amp;:drop)&lt;/code&gt; in before hooks. The previous line will no longer work in Mongoid 3.0, as we now access databases via sessions. As of Mongoid version 2.0.2, the method &lt;code&gt;Mongoid.purge!&lt;/code&gt; was added to easily drop all collections and indexes from your MongoDB database. This is what you should use moving forward.&lt;/p&gt;

&lt;h2&gt;RSpec&lt;/h2&gt;

&lt;p&gt;The following example &lt;em&gt;spec_helper.rb&lt;/em&gt; file is the bare minimum you will need to get Mongoid running with RSpec:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# spec_helper.rb&lt;/span&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="c1"&gt;# Requires supporting ruby files with custom matchers and macros, etc,&lt;/span&gt;
&lt;span class="c1"&gt;# in spec/support/ and its subdirectories.&lt;/span&gt;
&lt;span class="no"&gt;Dir&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;spec/support/**/*.rb&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="no"&gt;RSpec&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="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mock_with&lt;/span&gt; &lt;span class="ss"&gt;:rspec&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;before&lt;/span&gt; &lt;span class="ss"&gt;:each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;Mongoid&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;purge!&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Cucumber&lt;/h2&gt;

&lt;p&gt;Cucumber allows defining hooks in any file under the &lt;em&gt;features/support&lt;/em&gt; directory. To setup Cucumber, create a file &lt;em&gt;features/support/hooks.rb&lt;/em&gt; that will drop your MongoDB collections before each scenario:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# features/support/hooks.rb&lt;/span&gt;

&lt;span class="no"&gt;Before&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;scenario&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="no"&gt;Mongoid&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;purge!&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/jaUxedNvI5s" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2012/05/how-to-configure-cucumber-and-rspec-to-work-with-mongoid-30</feedburner:origLink></item>
    <item>
      <title>Easily deploy to multiple environments on Heroku with heroku_san</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/SgHvZUUwuaw/easily-deploy-to-multiple-environments-on-heroku-with-herokusan</link>
      <pubDate>Sat, 26 May 2012 00:59:28 +0000</pubDate>
      <author />
      <guid isPermaLink="false">http://adventuresincoding.com/2012/05/easily-deploy-to-multiple-environments-on-heroku-with-herokusan</guid>
      <description>&lt;p&gt;Deploying to &lt;a href="http://www.heroku.com/"&gt;Heroku&lt;/a&gt; is one of the easiest things a Rails developer can experience. All you have to do is push your git repository to heroku master. As easy as this is, it is not ideal for applications with staging and production environments.&lt;/p&gt;

&lt;p&gt;One of the most popular gems available to handle this issue is &lt;a href="https://github.com/fastestforward/heroku_san"&gt;heroku_san&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Install&lt;/h2&gt;

&lt;p&gt;Add the following to your project &lt;em&gt;Gemfile&lt;/em&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;group&lt;/span&gt; &lt;span class="ss"&gt;:development&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;heroku_san&amp;#39;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Once you run &lt;code&gt;bundle install&lt;/code&gt; to add &lt;code&gt;heroku_san&lt;/code&gt; to your application, you will need to generate a heroku configuration file.&lt;/p&gt;

&lt;p&gt;Geneate &lt;em&gt;config/heroku.yml&lt;/em&gt; via the heroku_san generator:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;bundle &lt;span class="nb"&gt;exec &lt;/span&gt;rails generate heroku_san
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;config/heroku.yml&lt;/h2&gt;

&lt;p&gt;The power of &lt;code&gt;heroku_san&lt;/code&gt; over the standard heroku deploy method is that it allows you to define all your environment variables and addons in one place. These settings can be shared with all environments or specific ones.&lt;/p&gt;

&lt;p&gt;Here is a modified version of the adventuresincoding configuration file:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="l-Scalar-Plain"&gt;production&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;app&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="l-Scalar-Plain"&gt;adventuresincoding&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;stack&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="l-Scalar-Plain"&gt;cedar&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;config&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="nl"&gt;&amp;amp;default_config&lt;/span&gt;
    &lt;span class="l-Scalar-Plain"&gt;BUNDLE_WITHOUT&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;test:development&amp;quot;&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;addons&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt;
    &lt;span class="p-Indicator"&gt;-&lt;/span&gt; &lt;span class="l-Scalar-Plain"&gt;mongolab:starter&lt;/span&gt;
    &lt;span class="p-Indicator"&gt;-&lt;/span&gt; &lt;span class="l-Scalar-Plain"&gt;newrelic:standard&lt;/span&gt;

&lt;span class="l-Scalar-Plain"&gt;staging&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;app&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="l-Scalar-Plain"&gt;adventuresincoding-staging&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;stack&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="l-Scalar-Plain"&gt;cedar&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;config&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt;
    &lt;span class="l-Scalar-Plain"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;*default_config&lt;/span&gt;
    &lt;span class="l-Scalar-Plain"&gt;RAILS_ENV&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;staging&amp;#39;&lt;/span&gt;
  &lt;span class="l-Scalar-Plain"&gt;addons&lt;/span&gt;&lt;span class="p-Indicator"&gt;:&lt;/span&gt;
    &lt;span class="p-Indicator"&gt;-&lt;/span&gt; &lt;span class="l-Scalar-Plain"&gt;mongolab:starter&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Setup&lt;/h2&gt;

&lt;p&gt;To setup your Heroku environments, run the following commands:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;bundle &lt;span class="nb"&gt;exec &lt;/span&gt;rake all heroku:create
&lt;span class="nv"&gt;$ &lt;/span&gt;bundle &lt;span class="nb"&gt;exec &lt;/span&gt;rake all heroku:config    
&lt;span class="nv"&gt;$ &lt;/span&gt;bundle &lt;span class="nb"&gt;exec &lt;/span&gt;rake all heroku:addons
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The above commands will create all your applications, configuration variables, and addons specified in your &lt;em&gt;heroku.yml&lt;/em&gt;. When using the rake task &lt;em&gt;all&lt;/em&gt;, commands are executed on each of your Heroku applications. If you want to only run commands on certain environments, use the name of the environment instead of &lt;em&gt;all&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;Deploying&lt;/h2&gt;

&lt;p&gt;heroku_san comes with a wide variety of &lt;em&gt;rake&lt;/em&gt; tasks to manage your applications. One of those being &lt;code&gt;heroku:deploy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example, to deploy to staging, run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;bundle &lt;span class="nb"&gt;exec &lt;/span&gt;rake staging heroku:deploy
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Benefits&lt;/h2&gt;

&lt;p&gt;Using &lt;code&gt;heroku_san&lt;/code&gt; provides you the benefit of keeping all your deployment logic in one place. This allows you to easily keep track of the differences in configuration variables and addons between environments. It was also an essential piece of the puzzle when I migrated from the bamboo stack to cedar. &lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/SgHvZUUwuaw" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2012/05/easily-deploy-to-multiple-environments-on-heroku-with-herokusan</feedburner:origLink></item>
    <item>
      <title>Managing your web applications with Foreman</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/L3WmePHSamE/managing-your-web-applications-with-foreman</link>
      <pubDate>Fri, 25 May 2012 18:21:42 +0000</pubDate>
      <author />
      <guid isPermaLink="false">http://adventuresincoding.com/2012/05/managing-your-web-applications-with-foreman</guid>
      <description>&lt;p&gt;Not all applications are created equal. While some applications can be as simple as a Ruby on Rails web app connecting to a database, others may have dependencies such as background workers. David Dollar realized his web applications were getting a lot more complicated to run, which resulted in him creating &lt;a href="https://github.com/ddollar/foreman"&gt;Foreman&lt;/a&gt;. Foreman is an excellent tool which allows you to encompass your entire application structure in a &lt;code&gt;Procfile&lt;/code&gt;.  A &lt;code&gt;Procfile&lt;/code&gt; allows you to declare multiple processes that encompass your application and run them all with a single &lt;code&gt;foreman start&lt;/code&gt; command. &lt;/p&gt;

&lt;h2&gt;Procfile&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;Procfile&lt;/code&gt; is a list that contains a name to identify the process and the command used by the system to run it. Foreman will look for it by default in your project&amp;#39;s root directory.&lt;/p&gt;

&lt;p&gt;Here is an example &lt;code&gt;Procfile&lt;/code&gt; of a Ruby on Rails application running Resque workers in the background:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="n"&gt;bundle&lt;/span&gt; &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="n"&gt;thin&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="vg"&gt;$PORT&lt;/span&gt;
&lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;bundle&lt;/span&gt; &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="n"&gt;rake&lt;/span&gt; &lt;span class="n"&gt;resque&lt;/span&gt;&lt;span class="ss"&gt;:work&lt;/span&gt; &lt;span class="no"&gt;QUEUE&lt;/span&gt;&lt;span class="o"&gt;=*&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Getting Started&lt;/h2&gt;

&lt;p&gt;Once your &lt;code&gt;Procfile&lt;/code&gt; is in place, install the &lt;code&gt;foreman&lt;/code&gt; gem:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gem install foreman
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To start up your application from the command line, all you need to do is run &lt;code&gt;foreman start&lt;/code&gt;.  By default, this will run one instance of the each application type defined in your &lt;code&gt;Procfile&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Concurrency&lt;/h2&gt;

&lt;p&gt;Foreman supports the ability to run more than 1 process of each type via the concurrency &lt;code&gt;-c&lt;/code&gt; parameter. You must define the number of each process type to run.&lt;/p&gt;

&lt;p&gt;For example, to increase the number of worker processes to two, execute:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;foreman start -c &lt;span class="nv"&gt;web&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1,worker&lt;span class="o"&gt;=&lt;/span&gt;2
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;.env&lt;/h2&gt;

&lt;p&gt;Another great benefit to Foreman is the ability to define local ENV variables for development. By creating a &lt;code&gt;.env&lt;/code&gt; file in the root directory of the project, Foreman will load all key/value pairs specified in the file into the default environment. &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;S3_KEY&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="n"&gt;abc&lt;/span&gt;
&lt;span class="n"&gt;S3_SECRET&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="n"&gt;def&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Be sure to include &lt;code&gt;.env&lt;/code&gt; in your &lt;code&gt;.gitignore&lt;/code&gt; file as well to ensure sensitive information is not stored in your repository.&lt;/p&gt;

&lt;h2&gt;Procfile.development&lt;/h2&gt;

&lt;p&gt;Sometimes you will want a different &lt;code&gt;Procfile&lt;/code&gt; for development . For example, you may want to run redis on your local machine while developing your application to save system resources. A convention I use is that I suffix &lt;em&gt;.development&lt;/em&gt; to my development &lt;code&gt;Procfile&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# Procfile.development&lt;/span&gt;
&lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="n"&gt;bundle&lt;/span&gt; &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="n"&gt;thin&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="vg"&gt;$PORT&lt;/span&gt;
&lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="sr"&gt;/usr/&lt;/span&gt;&lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;etc&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;conf&lt;/span&gt;
&lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;bundle&lt;/span&gt; &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="n"&gt;rake&lt;/span&gt; &lt;span class="n"&gt;resque&lt;/span&gt;&lt;span class="ss"&gt;:work&lt;/span&gt; &lt;span class="no"&gt;QUEUE&lt;/span&gt;&lt;span class="o"&gt;=*&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To run Foreman with a different &lt;code&gt;Procfile&lt;/code&gt;,  run &lt;code&gt;foreman&lt;/code&gt; with the -f parameter:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;foreman start -f Procfile.development
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Further Reading&lt;/h2&gt;

&lt;p&gt;For more information, check out the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://ddollar.github.com/foreman/"&gt;Man Page&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://devcenter.heroku.com/articles/procfile"&gt;Declaring and Scaling Process Types with Procfile&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.daviddollar.org/2011/05/06/introducing-foreman.html"&gt;Introducing Foreman&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/L3WmePHSamE" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2012/05/managing-your-web-applications-with-foreman</feedburner:origLink></item>
    <item>
      <title>How to get ApacheBench(ab) to work on Mac OS X Lion</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/jPqaSmivLwg/how-to-get-apachebenchab-to-work-on-mac-os-x-lion</link>
      <pubDate>Wed, 16 May 2012 16:23:18 +0000</pubDate>
      <author />
      <guid isPermaLink="false">http://adventuresincoding.com/2012/05/how-to-get-apachebenchab-to-work-on-mac-os-x-lion</guid>
      <description>&lt;p&gt;ApacheBench is a tool to do lightweight performance benchmarking on web applications. It is part of Apache which ships by default with Mac OS X. However, Mac OS X Lion comes with a version that has some issues.&lt;/p&gt;

&lt;p&gt;Here is an example of benchmark running against a local Ruby on Rails application:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ab -n 100 -c5 http://127.0.0.1:3000/
This is ApacheBench, Version 2.3 &amp;lt;&lt;span class="nv"&gt;$Revision&lt;/span&gt;: 655654 &lt;span class="nv"&gt;$&amp;gt;&lt;/span&gt;
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 &lt;span class="o"&gt;(&lt;/span&gt;be patient&lt;span class="o"&gt;)&lt;/span&gt;...Send request failed!
Send request failed!
apr_socket_recv: Connection reset by peer &lt;span class="o"&gt;(&lt;/span&gt;54&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;A majority of the times when &lt;em&gt;ab&lt;/em&gt; is run, you will receive an &lt;code&gt;apr_socket_recv&lt;/code&gt; error on Lion.&lt;/p&gt;

&lt;h2&gt;Solution&lt;/h2&gt;

&lt;p&gt;To fix the issue with ApacheBench, we must replace the version on the system with one which is more recent.&lt;/p&gt;

&lt;h3&gt;Step 1: Download the latest version of apache&lt;/h3&gt;

&lt;p&gt;At the time of writing, the latest version of Apache availble is 2.4.2. You can find it and future versions at &lt;a href="http://apache.mirrors.pair.com/httpd/"&gt;http://apache.mirrors.pair.com/httpd/&lt;/a&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl -O http://apache.mirrors.pair.com/httpd/httpd-2.4.2.tar.gz
&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;Step 2: Install missing dependencies&lt;/h3&gt;

&lt;p&gt;If you attempt to build Apache without installing &lt;em&gt;pcre&lt;/em&gt;, you will get the following error: &lt;code&gt;configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Install pcre via &lt;a href="https://github.com/mxcl/homebrew"&gt;Homebrew&lt;/a&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;brew install pcre
&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;Step 3: Build Apache&lt;/h3&gt;

&lt;p&gt;Once downloaded, extract and build the downloaded version of Apache:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;tar xzvf httpd-2.4.2.tar.gz
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;httpd-2.4.2/
&lt;span class="nv"&gt;$ &lt;/span&gt;./configure
&lt;span class="nv"&gt;$ &lt;/span&gt;make
&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;Step 4: Replace ab&lt;/h3&gt;

&lt;p&gt;Replace the existing ApacheBench(ab) version on Mac OS X Lion with the newly built version:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;sudo cp support/ab /usr/sbin
&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/jPqaSmivLwg" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2012/05/how-to-get-apachebenchab-to-work-on-mac-os-x-lion</feedburner:origLink></item>
    <item>
      <title>The future of Adventures in Coding</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/DAK_LL7ZF6g/the-future-of-adventures-in-coding</link>
      <pubDate>Tue, 08 May 2012 16:27:26 +0000</pubDate>
      <author />
      <guid isPermaLink="false">http://adventuresincoding.com/2012/05/the-future-of-adventures-in-coding</guid>
      <description>&lt;p&gt;Over the course of the last year I had the feeling something was missing. There are a lot of factors that contribute to developer happiness such as their environment, work, and their team. One piece that I felt was missing from my day-to-day life was the joy I had when I shared something I&amp;#39;ve learned with the community. Moving forward, I plan on sharing as much as I can on adventuresincoding.com.&lt;/p&gt;

&lt;h2&gt;The Mission Statement&lt;/h2&gt;

&lt;p&gt;Before I sit down on the keyboard to write anything, I will reflect on this statement: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&amp;quot;Sharing experiences, opinions, &amp;amp; news for the ruby web developer&amp;quot;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If something doesn&amp;#39;t fall within the mission statement, it will not make it on the blog.&lt;/p&gt;

&lt;h2&gt;What can you expect&lt;/h2&gt;

&lt;p&gt;Knowledge is sparse around the web for Ruby developers. I want to transform this site to be a hub for experienced and new Ruby developers alike.&lt;/p&gt;

&lt;p&gt;Here is a list of what I&amp;#39;m planning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tutorials&lt;/li&gt;
&lt;li&gt;Tips &amp;amp; Tricks&lt;/li&gt;
&lt;li&gt;Opinions on code, team dynamics, and growing software craftsmanship in our communities&lt;/li&gt;
&lt;li&gt;Focus on things I feel are of interest to myself and others. This includes Backbone.js, Rails, testing, and good code practices in general&lt;/li&gt;
&lt;li&gt;Sharing news about our industry&lt;/li&gt;
&lt;li&gt;Sharing experiments and implementation details of this blog engine&lt;/li&gt;
&lt;li&gt;Updating older posts which have outdated information and code&lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/DAK_LL7ZF6g" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2012/05/the-future-of-adventures-in-coding</feedburner:origLink></item>
    <item>
      <title>Speed up your websites with Smusher</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/T1NM276FtdU/speed-up-your-websites-with-smusher</link>
      <pubDate>Fri, 18 Nov 2011 13:54:06 +0000</pubDate>
      <author />
      <guid isPermaLink="false">http://adventuresincoding.com/2011/11/speed-up-your-websites-with-smusher</guid>
      <description>&lt;p&gt;Images are a bulk of the load time for many sites. There are many techniques that can allow for less requests, such as creating CSS Sprites. While I believe these techniques are essential, clients will still have to download the images in the end.&lt;/p&gt;

&lt;p&gt;Saving your images with Photoshop&amp;#39;s &amp;quot;Save for Web &amp;amp; Devices&amp;quot; doesn&amp;#39;t ensure that they are truly optimized. However, there are ways to get smaller images without quality loss.&lt;/p&gt;

&lt;h2&gt;&amp;quot;Lossless&amp;quot; images&lt;/h2&gt;

&lt;p&gt;Using optimization techniques, images can be optimized to smaller sizes without effecting their visual quality.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.smushit.com/ysmush.it/"&gt;Yahoo! SmushIt&lt;/a&gt; and &lt;a href="http://punypng.com"&gt;PunyPNG&lt;/a&gt; are two great services on the web that allow you to upload images and receive byte reduced versions.&lt;/p&gt;

&lt;h2&gt;Smusher&lt;/h2&gt;

&lt;p&gt;Using the above mentioned services requires manually selecting files and uploading them to the web. Thanks to Michael Grosser, we now have Smusher, a Ruby CLI for lossless reduction of your images.&lt;/p&gt;

&lt;p&gt;Smusher will take an image or directory of images and process them through an image reduction service with just a single line in your terminal.&lt;/p&gt;

&lt;p&gt;To install it, run the following:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gem install smusher
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;By default, smusher uses &lt;a href="http://www.smushit.com/ysmush.it/"&gt;Yahoo! SmushIt&lt;/a&gt;, but also has to option to switch to the PunyPng service.&lt;/p&gt;

&lt;h2&gt;Usage&lt;/h2&gt;

&lt;p&gt;To process a directory of images, run the &lt;code&gt;smusher&lt;/code&gt; command:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;smusher public/images --service PunyPng
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To demonstrate the savings, I ran smusher on a &lt;a href="http://upload.wikimedia.org/wikipedia/en/d/de/Ruby-%28programming-language%29-logo-2008.png"&gt;Ruby logo&lt;/a&gt; found on Wikipedia:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;smushing Ruby-&lt;span class="o"&gt;(&lt;/span&gt;programming-language&lt;span class="o"&gt;)&lt;/span&gt;-logo-2008.png
27416 -&amp;gt; &lt;span class="nv"&gt;23544&lt;/span&gt;                           &lt;span class="o"&gt;=&lt;/span&gt; 85%
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Smusher saved us 15% on just one image. Multiply that by every image on your site, and you will have made your users a little more happy with faster page downloads.&lt;/p&gt;

&lt;h2&gt;.puny_png_api_key&lt;/h2&gt;

&lt;p&gt;If you are a PunyPNG service subscriber, you can store your api key for future usage by smusher.&lt;/p&gt;

&lt;p&gt;Add a file named &lt;em&gt;.puny_png_api_key&lt;/em&gt; to your home directory with your PunyPNG api key as it&amp;#39;s contents.&lt;/p&gt;

&lt;h2&gt;Use it&lt;/h2&gt;

&lt;p&gt;Smusher is a great tool that has been available for years and I feel is not really known to most web developers. Once you start using it, I truly believe it will be a part of your utility belt.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/T1NM276FtdU" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2011/11/speed-up-your-websites-with-smusher</feedburner:origLink></item>
    <item>
      <title>Getting Inherited Resources 1.2.1 and Mongoid to play nice</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/51VmC1PD6_g/getting-inherited-resources-121-and-mongoid-to-play-nice</link>
      <pubDate>Sun, 20 Mar 2011 19:27:30 +0000</pubDate>
      <author />
      <guid isPermaLink="false">http://adventuresincoding.com/2011/03/getting-inherited-resources-121-and-mongoid-to-play-nice</guid>
      <description>&lt;p&gt;With the release of version 1.2.1 of &lt;a href="https://github.com/josevalim/inherited_resources"&gt;Inherited Resources&lt;/a&gt;, compatibility with &lt;a href="http://www.mongoid.org"&gt;Mongoid&lt;/a&gt; was broken. To optimize the setting of a collection in a controller, Inherited Resources calls the &lt;code&gt;scoped&lt;/code&gt; method at the end of the collection association chain over calling &lt;code&gt;all&lt;/code&gt;. The change to use &lt;code&gt;scoped&lt;/code&gt; results in a collection being lazily queried. The reason why Mongoid breaks is because it does not have a &lt;code&gt;scoped&lt;/code&gt; method, thus using Inherited Resources in any Mongoid project causes collection controller actions to break. &lt;/p&gt;

&lt;p&gt;Inherited Resources has rectified the issue in their &lt;em&gt;master&lt;/em&gt; branch on GitHub by checking to see if the collection responds to &lt;code&gt;scoped&lt;/code&gt;. If you cannot wait for the next release of Inherited Resources, add the following code to one of your Rails initializers to get it working with Mongoid again:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# app/config/initializers/inherited_resources.rb&lt;/span&gt;

&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;InheritedResources&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Base&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;collection&lt;/span&gt;
      &lt;span class="n"&gt;get_collection_ivar&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;begin&lt;/span&gt;
        &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;end_of_association_chain&lt;/span&gt;
        &lt;span class="n"&gt;set_collection_ivar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;respond_to?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:scoped&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scoped&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;all&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;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/51VmC1PD6_g" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2011/03/getting-inherited-resources-121-and-mongoid-to-play-nice</feedburner:origLink></item>
    <item>
      <title>Rails Templater - Agnostic at last</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/zSwRmn8c004/rails-templater---agnostic-at-last</link>
      <pubDate>Mon, 31 Jan 2011 01:26:11 +0000</pubDate>
      <author />
      <guid isPermaLink="false">http://adventuresincoding.com/2011/01/rails-templater---agnostic-at-last</guid>
      <description>&lt;h2&gt;What Rails Templater used to be&lt;/h2&gt;

&lt;p&gt;Rails Templater was originally a &lt;a href="http://adventuresincoding.com/2010/07/jumpstart-your-ruby-on-rails-3-applications-with-rails-templater"&gt;Ruby on Rails 3 template&lt;/a&gt; inspired by Mike Gunderloy’s &lt;a href="http://github.com/ffmike/BigOldRailsTemplate"&gt;BigOldRailsTemplate&lt;/a&gt; project. It was a very opinionated template, as it only created Rails applications using Mongoid, RSpec, jQuery, Haml, and FactoryGirl. This resulted in a large amount of forks(34 to date) by fans of the project who wanted to generate applications their way.&lt;/p&gt;

&lt;h2&gt;What is Rails Templater today?&lt;/h2&gt;

&lt;p&gt;The initial intentions of the template framework were small. I wanted something to generate applications the way I wanted, without all the configuration changes required after the initial generation. For example, if I wanted to use Mongoid with RSpec instead of ActiveRecord, I would have to update the RSpec configuration to take care of dropping MongoDB collections. Rails Templater takes care of all these configuration details in post bundler strategies, which take into account all the recipes chosen by the user.&lt;/p&gt;

&lt;p&gt;Rails Templater is no longer just a template, but a gem providing a simple interface for generating applications. Also, the project is now completely agnostic, thus allowing you to select what you want in your application. Some examples of choices are deciding which ORM, JavaScript framework, testing framework, and fixture replacement you want to use.&lt;/p&gt;

&lt;h2&gt;Usage&lt;/h2&gt;

&lt;p&gt;You can give Rails Templater a try right now with the following:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gem install rails_templater
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Once installed, generate a new Ruby on Rails 3 application using &lt;code&gt;templater&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;templater myapp
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Rails Templater will ask you questions, such as which testing framework you would like to use. Once all questions are answered, a new Rails application will be generated just the way you like it.&lt;/p&gt;

&lt;p&gt;Be sure to watch the screencast below to see an example of generating an app.&lt;/p&gt;

&lt;div class="video-container"&gt;
&lt;iframe src="http://player.vimeo.com/video/19367709?title=0&amp;amp;byline=0&amp;amp;portrait=0&amp;amp;color=ffffff"  width="500" height="281" frameborder="0"&gt;&lt;/iframe&gt;&lt;/div&gt;

&lt;h2&gt;Contributing new recipes&lt;/h2&gt;

&lt;p&gt;If you would like to add more recipes to the project, &lt;a href="https://github.com/kfaustino/rails-templater"&gt;fork it on GitHub&lt;/a&gt; and send me a pull request. The current road map is to add DataMapper support and more fixture replacements, such as Fabrication.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/zSwRmn8c004" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2011/01/rails-templater---agnostic-at-last</feedburner:origLink></item>
    <item>
      <title>Writing modular HTTP client code with Faraday</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/jNqxY0NrRdE/writing-modular-http-client-code-with-faraday</link>
      <pubDate>Thu, 23 Sep 2010 00:00:00 +0000</pubDate>
      <author>Kevin Faustino</author>
      <guid isPermaLink="false">http://adventuresincoding.com/2010/09/writing-modular-http-client-code-with-faraday</guid>
      <description>&lt;p&gt;What is Faraday? A natural philosopher from the 1700s? An awesome Lost character? A ruby gem by &lt;a href="http://twitter.com/technoweenie"&gt;technoweenie&lt;/a&gt; himself, Rick Olson? Since this blog is mostly development related, you may have concluded that this post is about the latter. &lt;a href="http://github.com/technoweenie/faraday"&gt;Faraday&lt;/a&gt; is an HTTP client library inspired by Rack. Requests and Responses go through middleware which allow for abstraction and modular code.&lt;/p&gt;

&lt;h2&gt;Installation&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;gem install faraday
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Be sure to also install the &lt;a href="http://wynnnetherland.com/projects/faraday-middleware%20gem"&gt;faraday-middleware&lt;/a&gt; by &lt;a href="http://twitter.com/pengwynn"&gt;Wynn Netherland&lt;/a&gt; as it provides some great middleware to consume APIs.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;gem install faraday-middleware
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Using Faraday&lt;/h2&gt;

&lt;p&gt;To create a connection via Faraday, instantiate a new &lt;code&gt;Faraday::Connection&lt;/code&gt; object with your desired options. The constructor of &lt;code&gt;Faraday::Connection&lt;/code&gt; takes a block where you will define an adapter and your desired middleware.&lt;/p&gt;

&lt;p&gt;An adapter represents the client library that Faraday will be utilizing to make your request. Faraday ships with the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NetHTTP (Faraday.default_adapter)&lt;/li&gt;
&lt;li&gt;Patron&lt;/li&gt;
&lt;li&gt;Typhoeus&lt;/li&gt;
&lt;li&gt;Test (Allows stubbing of HTTP requests)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: Creating a connection&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Faraday&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Connection&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="ss"&gt;:url&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;http://api.twitter.com/1/statuses/public_timeline.json&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;:headers&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:accept&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;  &lt;span class="s1"&gt;&amp;#39;application/json&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;:user_agent&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Faraday Ruby Client&amp;#39;&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;builder&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;adapter&lt;/span&gt; &lt;span class="no"&gt;Faraday&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;default_adapter&lt;/span&gt;
    &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Faraday&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;MultiJson&lt;/span&gt;
    &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Faraday&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Mashify&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The above example represents a Faraday connection to the Twitter API public timeline method. This connection uses two middlewares from the &lt;em&gt;faraday-middleware&lt;/em&gt; gem. &lt;code&gt;Faraday::Response::MultiJson&lt;/code&gt; sets the body of the response to a parsed JSON response using the &lt;a href="http://github.com/intridea/multi_json"&gt;multi_json&lt;/a&gt; gem. Afterwards, the &lt;code&gt;Faraday::Response::Mashify&lt;/code&gt; middleware uses the &lt;a href="http://github.com/intridea/hashie"&gt;hashie&lt;/a&gt; gem to allow dot notation on the response body instead of brackets.&lt;/p&gt;

&lt;p&gt;By putting request/response operations in middleware, Faraday forces your code to follow the Single Responsibility Principle. For example, &lt;code&gt;Faraday::Response::MultiJson&lt;/code&gt; is responsible for converting the JSON response into a ruby object using the best available JSON library and that alone.&lt;/p&gt;

&lt;p&gt;Calling methods on the connection object will allow you to perform GET, HEAD, PUT, POST or DELETE operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: Faraday response with MultiJson and Mashify&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;
&lt;span class="n"&gt;first_user&lt;/span&gt; &lt;span class="o"&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;body&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;first_user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;screen_name&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Creating a Middleware&lt;/h2&gt;

&lt;p&gt;A common code practice when making client HTTP requests is to verify the response was successful. To abstract the response verification, move the code to Faraday response middleware. To create one, you must inherit from &lt;code&gt;Response::Middleware&lt;/code&gt; and implement the methods &lt;code&gt;self.register_on_complete&lt;/code&gt; and &lt;code&gt;initialize&lt;/code&gt;. The interface is very similar to Rack. You can think of &lt;code&gt;register_on_complete(env)&lt;/code&gt; as &lt;code&gt;call(env)&lt;/code&gt; from Rack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: Twitter Error Response Middleware&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Faraday&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;TwitterErrors&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Middleware&lt;/span&gt;

    &lt;span class="k"&gt;begin&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;register_on_complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:response&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;on_complete&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;finished_env&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
          &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;finished_env&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:status&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
          &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;
              &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="no"&gt;Twitter&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;RateLimitExceeded&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;finished_env&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:body&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;error&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;finished_env&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:body&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
            &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;401&lt;/span&gt;
              &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="no"&gt;Twitter&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;UnauthorizedError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;finished_env&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:body&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;error&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;finished_env&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:body&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
            &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;
              &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="no"&gt;Twitter&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ServerError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;api.twitter.com is currently down&amp;#39;&lt;/span&gt;
          &lt;span class="k"&gt;end&lt;/span&gt;
        &lt;span class="k"&gt;end&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;rescue&lt;/span&gt; &lt;span class="no"&gt;LoadError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;NameError&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
      &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load_error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;super&lt;/span&gt;
      &lt;span class="vi"&gt;@parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Note&lt;/h2&gt;

&lt;p&gt;Faraday does not have gem dependencies for many of the adapters and middlewares used within it’s codebase. Ensure you have all necessary gems installed on your system.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/jNqxY0NrRdE" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2010/09/writing-modular-http-client-code-with-faraday</feedburner:origLink></item>
    <item>
      <title>34 Ruby on Rails 3 resources to get you started</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/_SVhrjEYZDA/34-ruby-on-rails-3-resources-to-get-you-started</link>
      <pubDate>Thu, 26 Aug 2010 00:00:00 +0000</pubDate>
      <author>Kevin Faustino</author>
      <guid isPermaLink="false">http://adventuresincoding.com/2010/08/34-ruby-on-rails-3-resources-to-get-you-started</guid>
      <description>&lt;p&gt;With the &lt;del&gt;upcoming&lt;/del&gt; final release of Ruby on Rails 3 &lt;del&gt;around the corner&lt;/del&gt;, get up to speed quickly with the following 34 resources:&lt;/p&gt;

&lt;h2&gt;Overviews&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://guides.rubyonrails.org/3_0_release_notes.html"&gt;Rails 3.0 Release Notes&lt;/a&gt; - RailsGuides is an invaluable resource for all Ruby on Rails developers. The Rails 3 release notes provides a great overview the api changes and new features added to the framework.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://railsplugins.org/"&gt;RailsPlugins.org&lt;/a&gt; – Find out if your plugins work in Rails 3.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://adventuresincoding.com/2010/07/having-ruby-on-rails-3-my-way/"&gt;Having Ruby on Rails 3 My Way&lt;/a&gt; - A tutorial on how to setup Rails 3 with Mongoid, Haml, jQuery, and RSpec with Factory Girl.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://litanyagainstfear.com/blog/2010/02/03/the-rails-module/"&gt;The Rails Module (in Rails 3)&lt;/a&gt; - Nick Quaranto outlines some helpful methods in the Rails module.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blog.plataformatec.com.br/2010/02/rails-3-i18n-changes/"&gt;Rails 3 I18n changes&lt;/a&gt; - A quick walkthrough the changes made to I18n.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://rubyonrails.org/screencasts/rails3"&gt;Ruby on Rails 3 Screencast series&lt;/a&gt; - The official Ruby on Rails site screencast series by Gregg Pollack.&lt;/p&gt;

&lt;h2&gt;ActionController / Rack&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://blog.plataformatec.com.br/2009/08/embracing-rest-with-mind-body-and-soul/"&gt;Embracing REST with mind, body and soul&lt;/a&gt; - José Valim introduces RESTful responders within Rails 3 controllers.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://railscasts.com/episodes/224-controllers-in-rails-3"&gt;Railscasts #224 Controllers in Rails 3&lt;/a&gt; – Ryan shows us what’s new with Rails 3 controllers.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.engineyard.com/blog/2010/render-options-in-rails-3/"&gt;Render Options in Rails 3&lt;/a&gt; - Yehuda goes through the rendering options available in Rails 3.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://pivotallabs.com/users/jdean/blog/articles/1370-adding-routes-for-tests-specs-with-rails-3"&gt;Adding Routes for tests / specs with Rails 3&lt;/a&gt; - Jeff Dean outlines how to verify routes in our specs.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://jasonseifer.com/2009/04/08/32-rack-resources-to-get-you-started"&gt;32 Rack Resources to Get You Started&lt;/a&gt; - A great collection of links to get you familiar with rack.&lt;/p&gt;

&lt;h2&gt;ActionMailer&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://lindsaar.net/2010/1/26/new-actionmailer-api-in-rails-3"&gt;New ActionMailer API in Rails 3.0&lt;/a&gt; - Mikel Lindsaar demonstrates how to use the new ActionMailer.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://railsdispatch.com/posts/actionmailer"&gt;A Whole New ActionMailer&lt;/a&gt; - Mikel Lindsaar’s Rails Dispatch post giving a tour of the ActionMailer features.&lt;/p&gt;

&lt;h2&gt;ActiveRecord/ActiveModel&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/"&gt;ActiveModel: Make Any Ruby Object Feel Like ActiveRecord&lt;/a&gt; – Yehuda writes about the modularity of ActiveModel.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://railscasts.com/episodes/219-active-model"&gt;Railscasts #219 Active Model&lt;/a&gt; - See how to include certain ActiveModel modules to tableless models.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://railsdispatch.com/posts/activerelation"&gt;ActiveRelation: ActiveRecord Gets a Facelift&lt;/a&gt; - Carl Lerche introduces the new ActiveRecord finder api.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://m.onkey.org/2010/1/22/active-record-query-interface"&gt;Active Record Query Interface&lt;/a&gt; - Pratik gives an overview of the changes made to the ActiveRecord query interface since Rails 2.&lt;/p&gt;

&lt;h2&gt;Bundler&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://gembundler.com/"&gt;Gembundler.com&lt;/a&gt; -The official project site for Bundler that includes extensive documentation.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://railsdispatch.com/posts/bundler"&gt;Library Management Gets an Update in Rails 3&lt;/a&gt; - Andre Arko gives an overview about bundler for the Rails Dispatch blog.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://railscasts.com/episodes/201-bundler"&gt;Railscasts #201 Bundler&lt;/a&gt; - A Railscasts by Ryan Bates demonstrating how to use bundler with Rails 3.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://adventuresincoding.com/2010/02/riding-ruby-on-rails-3-bundler/"&gt;AdventuresInCoding.com screencast on Bundler&lt;/a&gt; – Our very first screencast from last February.&lt;/p&gt;

&lt;h2&gt;Railties&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://www.igvita.com/2010/08/04/rails-3-internals-railtie-creating-plugins/"&gt;Rails 3 Internals: Railtie &amp;amp; Creating Plugins&lt;/a&gt; - Ilya Grigorik goes over Railties in Rails 3.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://adventuresincoding.com/2010/07/jumpstart-your-ruby-on-rails-3-applications-with-rails-templater"&gt;rails-templater&lt;/a&gt; - A template project which generates a greenfield Rails 3 application.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://caffeinedd.com/guides/331-making-generators-for-rails-3-with-thor"&gt;Making generators for Rails 3 with Thor&lt;/a&gt; – A tutorial showing you how to use the new generators and how to bundle them in a gem.&lt;/p&gt;

&lt;h2&gt;Routes&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/"&gt;The Lowdown on Routes in Rails 3&lt;/a&gt; - Rizwan Reza gives us a great summary of all the different ways to create routes in Rails 3.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://edgeguides.rubyonrails.org/routing.html"&gt;Rails Routing from the Outside In&lt;/a&gt; - RailsGuide documentation about the new router.&lt;/p&gt;

&lt;h2&gt;Plugins&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://weblog.rubyonrails.org/2010/2/9/plugin-authors-toward-a-better-future"&gt;Plugin Authors: Toward a Better Future&lt;/a&gt; - Yehuda goes through the changes for making plugins in Rails 3.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://railsdispatch.com/posts/building-or-updating-a-rails-3-plugin"&gt;Customizing Rails Apps with Plugins&lt;/a&gt; - José Valim gives an overview of using responders, simple form, devise, and mail form plugins.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blog.plataformatec.com.br/2010/08/devise-1-1-is-out-and-ready-to-rock-with-rails-3/"&gt;Devise 1.1 is out and ready to rock with Rails 3&lt;/a&gt; - Outlines some new feature in Devise 1.1 including being able to authenticate a given url in the router!&lt;/p&gt;

&lt;h2&gt;Books&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://beginningrails.com/"&gt;Beginning Rails 3&lt;/a&gt; – Cloves Carneiro Jr and Rida Al Barazi bring us a great beginner Rails 3 book with examples viewable in gists.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://my.safaribooksonline.com/9780132480345"&gt;The Rails 3 Way&lt;/a&gt; - The ultimate Ruby on Rails reference by Obie Fernandez.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.manning.com/katz/"&gt;Rails 3 In Action&lt;/a&gt; - Ryan Bigg and Yehuda Katz deliver a great Rails book which teaches readers Rails by doing BDD with Cucumber and RSpec.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.railsupgradehandbook.com/"&gt;Rails 3 Upgrade Handbook&lt;/a&gt; - Jeremy McAnally’s book gives you 120 pages of information about upgrading your Rails 2 application to version 3.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://railstutorial.org/"&gt;Ruby on Rails Tutorial: Learn Rails by Example&lt;/a&gt; – Michael Hartl’s ebook that teaches Rails step by step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;:  Rails 3.0.0 was released on August 29th, 2010. To get more details, be sure to read the &lt;a href="http://weblog.rubyonrails.org/2010/8/29/rails-3-0-it-s-done"&gt;official announcement&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/_SVhrjEYZDA" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2010/08/34-ruby-on-rails-3-resources-to-get-you-started</feedburner:origLink></item>
    <item>
      <title>Prototypal Inheritance in JavaScript Explained</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/_v09mQGArjo/prototypal-inheritance-in-javascript-explained</link>
      <pubDate>Tue, 27 Jul 2010 00:00:00 +0000</pubDate>
      <author>Kevin Faustino</author>
      <guid isPermaLink="false">http://adventuresincoding.com/2010/07/prototypal-inheritance-in-javascript-explained</guid>
      <description>&lt;p&gt;One of the more advanced topics in JavaScript is how to implement inheritance in your code. The reason why inheritance causes so much confusion is because there are so many ways to implement it in JavaScript.&lt;/p&gt;

&lt;p&gt;For those who are unfamiliar with inheritance, it allows us to extend a previously existing entity and supplement it by adding methods/properties to create an entirely new entity. It is an amazing way to reuse code!&lt;/p&gt;

&lt;p&gt;Here is &lt;a href="http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)"&gt;Wikipedia’s&lt;/a&gt; definition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&amp;quot;In object-oriented programming(OOP), Inheritance is a way to compartmentalize and reuse code by creating collections of attributes and behaviors called objects which can be based on previously created objects&amp;quot;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are a variety of JavaScript frameworks that provide a means to perform classical inheritance. Some of these include &lt;a href="http://www.prototypejs.org/learn/class-inheritance"&gt;Prototype&lt;/a&gt;, &lt;a href="http://mootools.net/docs/core/Class/Class"&gt;MooTools&lt;/a&gt;, and &lt;a href="http://jsclass.jcoglan.com/"&gt;ClassJS&lt;/a&gt;. This is a great option for a team where most of the developers come from other object-oriented languages. Even though the above implementations are viable solutions, Douglas Crockford suggests the ideal solution is to use the prototypal inheritance, which has objects inheriting from other objects&lt;/p&gt;

&lt;h2&gt;Implementation&lt;/h2&gt;

&lt;p&gt;In JavaScript everything is an object, there is no concept of a class like other object-oriented programming languages. With prototypal inheritance, we implement inheritance by creating objects which will act as prototypes for other objects.&lt;/p&gt;

&lt;p&gt;My favourite implementation comes from Stoyan Stefanov’s book &lt;a href="http://www.amazon.com/Object-Oriented-JavaScript-high-quality-applications-libraries/dp/1847194141/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1280256010&amp;amp;sr=8-1"&gt;Object-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications, and libraries&lt;/a&gt;. Here is my slightly modified version of a function which abstracts the creation of a child object:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;supplements&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="cm"&gt;/*  Create a variable that will store our child object. */&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="cm"&gt;/*  Create an empty function F which will act as an intermediary between&lt;/span&gt;
&lt;span class="cm"&gt;      the parent and the child. F will have no properties of its own.   */&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;F&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

  &lt;span class="cm"&gt;/*  Assign the parent constructor to the prototype of F.&lt;/span&gt;
&lt;span class="cm"&gt;      This will allow the child to copy all the parentâs properties without&lt;/span&gt;
&lt;span class="cm"&gt;      having to set itâs prototype to the parent instance. */&lt;/span&gt;
  &lt;span class="nx"&gt;F&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="cm"&gt;/*  Create a new child object from F. We get a copy of all the parentâs properties.&lt;/span&gt;
&lt;span class="cm"&gt;      Functions/objects are copied by reference. */&lt;/span&gt;
  &lt;span class="nx"&gt;child&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;F&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="cm"&gt;/*  Define a property uber which points to the parent object. Simulates super&lt;/span&gt;
&lt;span class="cm"&gt;      from other languages. */&lt;/span&gt;
  &lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;uber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="cm"&gt;/*  Add any functions/properties desired for the child object. */&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;supplements&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;supplements&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Example&lt;/h2&gt;

&lt;p&gt;Here is a basic example displaying how the properties are copied and overridden in a parent/child relationship.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;William&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Adama&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot; &amp;quot;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;child&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Lee&amp;quot;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// &amp;quot;Lee Adama&amp;quot;&lt;/span&gt;
&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// &amp;quot;William Adama&amp;quot;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We can implement another version of &lt;code&gt;fullName()&lt;/code&gt; that indicates William is Lee’s father without removing the parent function.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;uber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;#39;s child &amp;quot;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// &amp;quot;William Adama&amp;#39;s child Lee&amp;quot;&lt;/span&gt;
&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// &amp;quot;William Adama&amp;quot;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We can even remove our child’s &lt;code&gt;fullName()&lt;/code&gt; function and still have access to out parent’s function because it exists in the child’s prototype chain.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;
&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// &amp;quot;Lee Adama&amp;quot;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Benefits&lt;/h2&gt;

&lt;p&gt;The benefit of using prototypal inheritance is that we are working with the strengths of JavaScript and not trying to imitate features of another language.&lt;/p&gt;

&lt;p&gt;Not only does it provide an easy way to do inheritance, but there is a performance boost as well. When a function is defined in an object, they are created by reference, resulting in all child objects pointing to the same function and not creating their own copies. This can also lead to some issues if you are unaware of this. If you do not want to share any objects or functions in your child object, make sure to supplement them. Any functions/objects you create in your child object will take precedence over the parent’s version.&lt;/p&gt;

&lt;h2&gt;Out in the Wild&lt;/h2&gt;

&lt;p&gt;In the latest ECMAScript 5 standard, prototypal inheritance became a first class citizen as a new native method &lt;a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/create"&gt;Object.create(proto [, propertiesObject ])&lt;/a&gt; was added. It allows you to create a new object with the specified prototype object and properties. You can also use it today in &lt;a href="http://www.yuiblog.com/blog/2010/01/06/inheritance-patterns-in-yui-3/"&gt;YUI 3&lt;/a&gt; via the Y.Object(…) function from YUI core.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/_v09mQGArjo" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2010/07/prototypal-inheritance-in-javascript-explained</feedburner:origLink></item>
    <item>
      <title>How to configure Cucumber and RSpec to work with Mongoid</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/o6kXkBGUdLM/how-to-configure-cucumber-and-rspec-to-work-with-mongoid</link>
      <pubDate>Thu, 22 Jul 2010 00:00:00 +0000</pubDate>
      <author>Kevin Faustino</author>
      <guid isPermaLink="false">http://adventuresincoding.com/2010/07/how-to-configure-cucumber-and-rspec-to-work-with-mongoid</guid>
      <description>&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: This post is outdated. An &lt;a href="http://adventuresincoding.com/2012/05/how-to-configure-cucumber-and-rspec-to-work-with-mongoid-30"&gt;updated version&lt;/a&gt; is available which explains how to use RSpec and Cucumber with Mongoid 3.0.&lt;/p&gt;

&lt;p&gt;RSpec and Cucumber by default are setup to work with relational databases using ActiveRecord. This has been fine for the most part until so many NoSQL databases have come onto the scene. Let’s focus on getting Cucumber and RSpec working with MongoDB using Mongoid.&lt;/p&gt;

&lt;p&gt;First you will need to setup your Rails 3 application to use &lt;a href="http://adventuresincoding.com/2010/07/having-ruby-on-rails-3-my-way/"&gt;Mongoid, Cucumber, RSpec, and Factory Girl&lt;/a&gt;. Once you have a project skeleton, you will need to setup RSpec to drop all the MongoDB collections of the project before each spec is run to ensure a clean test harness.&lt;/p&gt;

&lt;p&gt;The best command to drop your MongoDB collections comes from Mongoid creator Durran Jordan in Mongoid’s &lt;a href="http://github.com/durran/mongoid/blob/master/spec/spec_helper.rb#L32"&gt;code base&lt;/a&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="no"&gt;Mongoid&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;collections&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;select&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;!~&lt;/span&gt; &lt;span class="sr"&gt;/system/&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:drop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;RSpec&lt;/h2&gt;

&lt;p&gt;Here is a sample RSpec configuration which will empty your MongoDB db and also use Factory Girl to create your models. Note that &lt;code&gt;config.user_transactional_fixtures&lt;/code&gt; is commented out.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# spec_helper.rb&lt;/span&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;remarkable/active_model&amp;#39;&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;remarkable/mongoid&amp;#39;&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;factory_girl&amp;#39;&lt;/span&gt;

&lt;span class="c1"&gt;# Requires supporting files with custom matchers and macros, etc,&lt;/span&gt;
&lt;span class="c1"&gt;# in ./support/ and its subdirectories.&lt;/span&gt;
&lt;span class="no"&gt;Dir&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;__FILE__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/support/**/*.rb&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="no"&gt;RSpec&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="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mock_with&lt;/span&gt; &lt;span class="ss"&gt;:rspec&lt;/span&gt;

  &lt;span class="c1"&gt;# If you&amp;#39;re not using ActiveRecord, or you&amp;#39;d prefer not to run each of your&lt;/span&gt;
  &lt;span class="c1"&gt;# examples within a transaction, comment the following line or assign false&lt;/span&gt;
  &lt;span class="c1"&gt;# instead of true.&lt;/span&gt;
  &lt;span class="c1"&gt;# config.use_transactional_fixtures = true&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;before&lt;/span&gt; &lt;span class="ss"&gt;:each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="no"&gt;Mongoid&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;collections&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;select&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;!~&lt;/span&gt; &lt;span class="sr"&gt;/system/&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:drop&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;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Cucumber&lt;/h2&gt;

&lt;p&gt;Cucumber allows defining hooks in any file under the &lt;code&gt;features/support&lt;/code&gt; directory. To begin setting up Cucumber, first create a file &lt;code&gt;features/support/hooks.rb&lt;/code&gt; that will drop your MongoDB collections before each scenario.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# features/support/hooks.rb&lt;/span&gt;

&lt;span class="no"&gt;Before&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;scenario&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="no"&gt;Mongoid&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;collections&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;select&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;!~&lt;/span&gt; &lt;span class="sr"&gt;/system/&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:drop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I also find that using Factory Girl for my Cucumber scenarios makes my life a lot easier. Below is an example features/support/env.rb file which will import all your factories. It will also provide some already defined step definitions by the guys at &lt;a href="http://thoughtbot.com/"&gt;thoughtbot&lt;/a&gt;. Be sure to read about them in their post &lt;a href="http://robots.thoughtbot.com/post/284805810/gimme-three-steps"&gt;gimme three steps&lt;/a&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# env.rb&lt;/span&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="s2"&gt;&amp;quot;test&amp;quot;&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="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;__FILE__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;/../../config/environment&amp;#39;&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;cucumber/formatter/unicode&amp;#39;&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;cucumber/rails/rspec&amp;#39;&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;cucumber/rails/world&amp;#39;&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;cucumber/web/tableish&amp;#39;&lt;/span&gt;

&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;capybara/rails&amp;#39;&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;capybara/cucumber&amp;#39;&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;capybara/session&amp;#39;&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;cucumber/rails/capybara_javascript_emulation&amp;#39;&lt;/span&gt;

&lt;span class="no"&gt;Capybara&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;default_selector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ss"&gt;:css&lt;/span&gt;

&lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;allow_rescue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;

&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;factory_girl&amp;#39;&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;factory_girl/step_definitions&amp;#39;&lt;/span&gt;
&lt;span class="no"&gt;Dir&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expand_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;__FILE__&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;..&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;..&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s1"&gt;&amp;#39;spec&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;factories&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;*.rb&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;All the step definitions from &lt;code&gt;factory_girl/step_definitions&lt;/code&gt; work except for &amp;quot;create record &amp;amp; set one attribute&amp;quot;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;Given &lt;/span&gt;&lt;span class="nf"&gt;an author exists with an email of &amp;quot;&lt;/span&gt;&lt;span class="s"&gt;author@example.com&lt;/span&gt;&lt;span class="nf"&gt;&amp;quot;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The reason for this is because in the Factory girl &lt;a href="http://github.com/thoughtbot/factory_girl/blob/master/lib/factory_girl/step_definitions.rb#L41"&gt;code&lt;/a&gt;, there is a check that determines if the given model responds to &lt;code&gt;:columns&lt;/code&gt;. To get around this, add the following code to a new file &lt;em&gt;features/step_definitions/mongoid_steps.rb&lt;/em&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# features/step_definitions/mongoid_steps.rb&lt;/span&gt;

&lt;span class="no"&gt;Given&lt;/span&gt; &lt;span class="sr"&gt;/^an? (.+) exists with an? (.+) of &amp;quot;([^&amp;quot;]*)&amp;quot;$/&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;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;factory_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gsub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39; &amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;_&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="no"&gt;Factory&lt;/span&gt; &lt;span class="n"&gt;factory_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The last thing to do is stub out the rake task for &lt;code&gt;db:test:prepare&lt;/code&gt;. The Cucumber rake tasks are still dependent on the ActiveRecord test workflow and will attempt to load the db schema into the test db. Create the  file &lt;em&gt;lib/tasks/mongo.rake&lt;/em&gt; to get around this:-&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: As of version 2.0.0.beta.14 of mongoid, a stubbed rake task for &lt;code&gt;db:test:clone&lt;/code&gt; is included.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# lib/tasks/mongo.rake&lt;/span&gt;

&lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="ss"&gt;:db&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="ss"&gt;:test&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="ss"&gt;:prepare&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="c1"&gt;# Stub out for MongoDB&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Finally, if you don’t want to do this every time you setup an application, I’ve updated &lt;a href="http://github.com/kfaustino/rails-templater"&gt;rails-templater&lt;/a&gt; to do the above steps automatically.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/o6kXkBGUdLM" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2010/07/how-to-configure-cucumber-and-rspec-to-work-with-mongoid</feedburner:origLink></item>
    <item>
      <title>Jumpstart your Ruby on Rails 3 applications with rails-templater</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/-lTa8JRTLtg/jumpstart-your-ruby-on-rails-3-applications-with-rails-templater</link>
      <pubDate>Mon, 19 Jul 2010 06:00:00 +0000</pubDate>
      <author>Kevin Faustino</author>
      <guid isPermaLink="false">http://adventuresincoding.com/2010/07/jumpstart-your-ruby-on-rails-3-applications-with-rails-templater</guid>
      <description>&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: Rails Templater is now a &lt;a href="http://adventuresincoding.com/2011/01/rails-templater---agnostic-at-last"&gt;gem and agnostic&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;In &lt;a href="http://adventuresincoding.com/2010/07/having-ruby-on-rails-3-my-way/"&gt;Having Ruby on Rails my way&lt;/a&gt;, I outlined how to manually setup your Ruby on Rails 3 application to use custom generators for Mongoid, Haml, jQuery, RSpec, and factory_girl.&lt;/p&gt;

&lt;p&gt;To simplify things further, I have started a template project called &lt;a href="http://github.com/kfaustino/rails-templater"&gt;rails-templater&lt;/a&gt;. It is inspired by Mike Gunderloy’s &lt;a href="http://github.com/ffmike/BigOldRailsTemplate"&gt;BigOldRailsTemplate&lt;/a&gt; project which provides templates for Rails 2.3.x projects. To get started, clone the rails-templater repository to your hard drive:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;% git clone http://github.com/kfaustino/rails-templater.git
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;When you are ready to create a new Rails 3 application, run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;% rails new app_name -JOT -m &lt;span class="se"&gt;\&lt;/span&gt;
 /path/to/rails-tempater/templater.rb
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Optionally you can also use &lt;a href="http://compass-style.org/"&gt;Compass&lt;/a&gt; as your stylesheet framework and &lt;a href="http://github.com/aslakhellesoy/cucumber"&gt;Cucumber&lt;/a&gt; for integration testing.&lt;/p&gt;

&lt;p&gt;I will be continuously making updates to the project by adding new recipes and template options.&lt;/p&gt;

&lt;p&gt;Be sure to check out the project &lt;a href="http://github.com/kfaustino/rails-templater"&gt;GitHub page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Photo credit: &lt;a href="http://www.flickr.com/photos/dystopos/459452832/"&gt;Railroad in Crestwood&lt;/a&gt; by dystopos&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/-lTa8JRTLtg" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2010/07/jumpstart-your-ruby-on-rails-3-applications-with-rails-templater</feedburner:origLink></item>
    <item>
      <title>Having Ruby on Rails 3 my way</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/XDUkmOF7OjM/having-ruby-on-rails-3-my-way</link>
      <pubDate>Mon, 19 Jul 2010 00:00:00 +0000</pubDate>
      <author>Kevin Faustino</author>
      <guid isPermaLink="false">http://adventuresincoding.com/2010/07/having-ruby-on-rails-3-my-way</guid>
      <description>&lt;p&gt;At RailsConf 2009, David Heinemeier Hansson during his &lt;a href="http://railsconf.blip.tv/file/2081411/"&gt;keynote&lt;/a&gt; mentioned that one of the central themes behind the Ruby on Rails and Merb merge was to &amp;quot;Have it your way&amp;quot;. Given the fact that Rails 3 is almost at a release candidate, I wanted to share my ideal Ruby on Rails setup.&lt;/p&gt;

&lt;p&gt;My ideal application stack consists of Mongoid as my ODM, Haml as my template engine, jQuery as my JavaScript framework, RSpec as my testing framework, and factory_girl as my fixture replacement.&lt;/p&gt;

&lt;p&gt;I will outline how to setup each of these individually for your Ruby on Rails 3 application.&lt;/p&gt;

&lt;p&gt;To begin, generate a new Rails 3 application with the following command:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;% rails new app_name -TOJ
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The flags passed into the rails command will remove certain elements from the generation of our application.&lt;/p&gt;

&lt;p&gt;T – represents Test::Unit&lt;br&gt;
O – represents ActiveRecord&lt;br&gt;
J – represents Prototype&lt;/p&gt;

&lt;h2&gt;Replacing ActiveRecord with Mongoid&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://mongoid.org/"&gt;Mongoid&lt;/a&gt; is an Object Document Mapper for &lt;a href="http://www.mongodb.org/"&gt;MongoDB&lt;/a&gt;. It has a beautiful chaining criteria dsl for queries and is optimized for extremely large datasets.&lt;/p&gt;

&lt;p&gt;To add mongoid to your project, add the following to your Gemfile:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;mongoid&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;&amp;gt;= 2.0.0.beta14&amp;#39;&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;bson_ext&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;1.0.4&amp;#39;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Mongoid provides a generator to setup your project to create documents for model generation and setup a sample database connection configuration. To run this generator, first execute bundle install to ensure all missing dependencies are installed. Finally, run the &lt;code&gt;mongoid:config&lt;/code&gt; generator:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;% rails g mongoid:config
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Replacing ERB with Haml&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://haml-lang.com/"&gt;Haml&lt;/a&gt; was one of the primary reasons why I feel in love with working on Ruby on Rails applications. It just makes working with markup a pleasure.&lt;/p&gt;

&lt;p&gt;Adding Haml to your project requires only 1 lines in your Gemfile:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rails3-generators&amp;#39;&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;haml&amp;#39;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The gem &lt;a href="http://github.com/indirect/rails3-generators"&gt;rails3-generators&lt;/a&gt; provides a variety of generators for Ruby on Rails 3 applications including factory_girl which we will be using later.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;config/application.rb&lt;/code&gt;, add the following to your application configuration:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;generators&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;g&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;template_engine&lt;/span&gt; &lt;span class="ss"&gt;:haml&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Replacing Prototype with jQuery&lt;/h2&gt;

&lt;p&gt;Using &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt; simplifies dealing with the DOM and provides all the tools necessary to do unobtrusive JavaScript in your web application. According to &lt;a href="http://survey.hamptoncatlin.com/"&gt;Hampton Catlin&amp;#39;s 2010 ruby survey&lt;/a&gt;, 78.8% of all ruby users prefer it over Prototype.&lt;/p&gt;

&lt;p&gt;Run the following two commands in the root of your project directory:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;curl -L http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js &amp;gt; &lt;span class="se"&gt;\&lt;/span&gt;
public/javascripts/jquery.js

curl -L http://github.com/rails/jquery-ujs/raw/master/src/rails.js &amp;gt; &lt;span class="se"&gt;\&lt;/span&gt;
public/javascripts/rails.js
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Once you setup a layout file, include the two javascript files and your set.&lt;/p&gt;

&lt;h2&gt;Replacing Test::Unit with RSpec&lt;/h2&gt;

&lt;p&gt;With the latest beta release of &lt;a href="http://github.com/rspec/rspec-rails"&gt;RSpec 2&lt;/a&gt;, all generators and rake tasks are now exposed through a Railtie.&lt;/p&gt;

&lt;p&gt;To replace Test::Unit as your default test framework, include the &lt;em&gt;rspec-rails&lt;/em&gt; gem in both the :development and :test group in your Gemfile:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;group&lt;/span&gt; &lt;span class="ss"&gt;:development&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:test&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rspec-rails&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;&amp;gt;= 2.0.0.beta.19&amp;#39;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The next step is to install rspec into the application. The &lt;em&gt;rspec:install&lt;/em&gt; generator will create a spec directory and some skeleton files. Run the following in your project root:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;% bundle install
% rails g rspec:install
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Even though you are including RSpec as a development environment dependency, it is not completely loaded. It is only completely loaded when you invoke rails generate or rake. Read more details about this change at David Chelimsky&amp;#39;s post &lt;a href="http://blog.davidchelimsky.net/2010/07/11/rspec-rails-2-generators-and-rake-tasks-part-ii/"&gt;rspec-rails-2 generators and rake tasks – part II&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Replacing Fixtures with Factory Girl&lt;/h2&gt;

&lt;p&gt;The rails3-generators gem we included earlier also provides some &lt;a href="http://github.com/thoughtbot/factory_girl"&gt;factory_girl&lt;/a&gt; generators. This allows for the creation of a factory when we use the model generator from Rails.&lt;/p&gt;

&lt;p&gt;Add a dependency to the factory_girl_rails to the test environment in your Gemfile:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;factory_girl_rails&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;/pre&gt;
&lt;/div&gt;

&lt;p&gt;You will also need to manually configure your generators to use factory_girl in &lt;code&gt;config/application.rb&lt;/code&gt;. Your generators configuration should now look like the following:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;generators&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;g&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;template_engine&lt;/span&gt; &lt;span class="ss"&gt;:haml&lt;/span&gt;
  &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fixture_replacement&lt;/span&gt; &lt;span class="ss"&gt;:factory_girl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:dir&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;spec/factories&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: If you absolutely love this stack, be sure to check out &lt;a href="http://adventuresincoding.com/2010/07/jumpstart-your-ruby-on-rails-3-applications-with-rails-templater"&gt;rails-templater&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/XDUkmOF7OjM" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2010/07/having-ruby-on-rails-3-my-way</feedburner:origLink></item>
    <item>
      <title>10 TextMate bundles/plugins to boost your Ruby on Rails development productivity</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/oC8La0cjnl8/10-textmate-bundlesplugins-to-boost-your-ruby-on-rails-development-productivity</link>
      <pubDate>Mon, 31 May 2010 00:00:00 +0000</pubDate>
      <author>Kevin Faustino</author>
      <guid isPermaLink="false">http://adventuresincoding.com/2010/05/10-textmate-bundlesplugins-to-boost-your-ruby-on-rails-development-productivity</guid>
      <description>&lt;p&gt;&lt;a href="http://macromates.com/"&gt;TextMate&lt;/a&gt; is a widely used GUI text editor that many in the Ruby on Rails community use. The editor benefits from a large and active community of users whom develop bundles and plugins that extend TextMate’s functionality to work with a wide variety of languages and libraries.&lt;/p&gt;

&lt;p&gt;Here are ten of my favourite bundles and plugins available to TextMate that will save you time and effort while working with Ruby on Rails:&lt;/p&gt;

&lt;p&gt;1 - &lt;a href="http://peepcode.com/products/peepopen"&gt;PeepOpen&lt;/a&gt;: One of the best features of TextMate is fuzzy search(Command-T) which allows you to find files in your project with just a few characters of the filename. The issue with fuzzy search is that it becomes hard to pinpoint a file when there are many files with the same name in your project. Working in Ruby on Rails, you will face an endless amount repeated filenames in your views such as &lt;code&gt;index.html.erb&lt;/code&gt;. Geoffrey Grosenbach from PeepCode, has developed an application PeepOpen that replaces the default fuzzy search with a beautiful fuzzy search that also allows you to pinpoint directories within your search. For a mere $9.00, it is a must own add-on to TextMate.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.adventuresincoding.com/images/posts/peepopen.png" alt="PeepOpen"&gt;&lt;/p&gt;

&lt;p&gt;2 - &lt;a href="http://github.com/carlosbrando/ruby-on-rails-tmbundle"&gt;Ruby on Rails&lt;/a&gt;: Ruby on Rails development just got better with the recent changes made by Carlos Brando to &lt;a href="http://github.com/drnic/ruby-on-rails-tmbundle"&gt;Dr. Nic’s bundle&lt;/a&gt;. Not only does it include all the snippets and Rails integrations we are all used to, but now we also get a pseudo-intellisense for attributes for each ActiveRecord model in your project. One of my favourite additions to the bundle is the ability to easily create &lt;a href="http://adventuresincoding.com/2010/01/taking-the-helm-of-ruby-with-ruby-version-manager/"&gt;RVM&lt;/a&gt; &lt;code&gt;.rvmrc&lt;/code&gt; files for your project.  The best resource for this bundle is &lt;a href="http://peepcode.com/products/textmate-for-rails-2"&gt;PeepCode’s TextMate for Rails 2 screencast&lt;/a&gt;, be sure to check it out.&lt;/p&gt;

&lt;p&gt;3 - &lt;a href="http://github.com/protocool/AckMate"&gt;AckMate&lt;/a&gt;: If you need to find some text fast, AckMate is your answer. By default, you have the power of regular expressions to pinpoint even the most complex pattern of text. AckMate also allows the filtering of files by specifying file extensions and pattern matching if a filename contains certain text.&lt;/p&gt;

&lt;p&gt;&lt;img title="AckMate" src="http://media.adventuresincoding.com/images/posts/ackmate.png" width="408" height="421"&gt;&lt;/p&gt;

&lt;p&gt;4 - &lt;a href="http://github.com/subtleGradient/javascript-tools.tmbundle"&gt;JavaScript Tools&lt;/a&gt;: JavaScript is ingrained in web development regardless of the server technology being utilized. Using the JavaScript Tools bundle you get all of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript Lint to check all your source code for common JavaScript mistakes&lt;/li&gt;
&lt;li&gt;A JavaScript beautifier to uniform line breaks and indentation&lt;/li&gt;
&lt;li&gt;Packing and minimizer tools to compress your JavaScript documents for production&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5 - &lt;a href="http://github.com/kswedberg/jquery-tmbundle"&gt;JavaScript jQuery&lt;/a&gt;: jQuery is the most popular JavaScript library available today. This bundle by jQuery team member Karl Swedberg and Jonathan Chaffer provides a huge amount of snippets to save you some keystrokes when working with jQuery.&lt;/p&gt;

&lt;p&gt;6 - &lt;a href="http://github.com/jcf/git-tmbundle"&gt;Git&lt;/a&gt;: The Git bundle provides access to common Git commands without leaving your editor. Do things like viewing your uncommitted changes, find out who made that bad code change, and commit your changes to the repository.&lt;/p&gt;

&lt;p&gt;7 &amp;amp; 8 - &lt;a href="http://github.com/dchelimsky/rspec-tmbundle"&gt;rSpec&lt;/a&gt;/&lt;a href="http://github.com/bmabey/cucumber-tmbundle"&gt;Cucumber&lt;/a&gt;: Outside-in Rails development has never been easier with Behaviour Driven Development (BDD) tools rSpec and Cucumber. These bundles allow for the running of specs and features from within TextMate and provide an HTML output of the results.&lt;/p&gt;

&lt;p&gt;9 - &lt;a href="http://github.com/mocoso/code-beautifier.tmbundle"&gt;Code Beautifier&lt;/a&gt;:  TextMate does a good job of formatting your code but there is room for improvement. Using the Option-Command-B keyboard combination, the Code Beautifier bundle will clean up any white space and fix indentation in your ruby code. This is one of my most used bundles when I work with TextMate.&lt;/p&gt;

&lt;p&gt;10 - &lt;a href="http://github.com/grimen/compass_blueprint_tmbundle"&gt;Compass + Blueprint&lt;/a&gt;: &lt;a href="http://compass-style.org/"&gt;Compass&lt;/a&gt; is the amazing Sass framework which cuts down development time by providing mixins and a means for doing unobtrusive CSS. This Compass bundle focuses on snippets for the popular Blueprint CSS framework.&lt;/p&gt;

&lt;p&gt;Give these bundles/plugins a try and be sure to let me know of any you think belong on this list.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/oC8La0cjnl8" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2010/05/10-textmate-bundlesplugins-to-boost-your-ruby-on-rails-development-productivity</feedburner:origLink></item>
    <item>
      <title>How to setup Git to use Diffmerge</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/TPDO7DR-_S4/how-to-setup-git-to-use-diffmerge</link>
      <pubDate>Sat, 03 Apr 2010 00:00:00 +0000</pubDate>
      <author>Kevin Faustino</author>
      <guid isPermaLink="false">http://adventuresincoding.com/2010/04/how-to-setup-git-to-use-diffmerge</guid>
      <description>&lt;p&gt;Merge conflicts, they are bound to happen sooner or later when you are working in parallel with a team of developers. When Git cannot automatically merge changes, a merge fails and the conflicted file becomes littered with &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;, =======, and &amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; markers.  To ease the pain of having to performing merges, Git allows using one of many supported merge tools or you can configure Git to use the tool of your choice instead.&lt;/p&gt;

&lt;p&gt;In this post, I am going to show you how to do the latter in Mac OS X using my preferred merge tool, &lt;a href="http://www.sourcegear.com/diffmerge/index.html"&gt;Diffmerge&lt;/a&gt;.  Diffmerge is a free graphical file comparison tool by SourceGear available in Mac OS X, Linux, and Windows. It is ideal to perform 3-way merges as it supports 3-way file comparison. During a conflict, you are presented with the version of the branch you are working on, the common file ancestor, and the branch you are merging in.&lt;/p&gt;

&lt;h2&gt;Visual Mergetool Configuration&lt;/h2&gt;

&lt;p&gt;Running the following commands in your terminal will set Diffmerge as your default visual mergetool:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git config --global merge.tool diffmerge

git config --global mergetool.diffmerge.cmd &lt;span class="s2"&gt;&amp;quot;diffmerge --merge&lt;/span&gt;
&lt;span class="s2"&gt;--result=\$MERGED \$LOCAL \$BASE \$REMOTE&amp;quot;&lt;/span&gt;

git config --global mergetool.diffmerge.trustExitCode &lt;span class="nb"&gt;true&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As of the 3.3 release of Diffmerge, exiting a visual merge will return the correct status to Git. Prior to this release, Diffmerge would always return 0, indicating that the merge was resolved even on aborted merges. When you attempt to close Diffmerge, you will be prompted to choose if the merge is to be aborted or is resolved.&lt;/p&gt;

&lt;p&gt;To start a 3-way merge with your visual mergetool, run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git mergetool
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This will perform a visual merge for each file in conflict. You may also provide an optional file parameter, to perform a merge on a specific file.&lt;/p&gt;

&lt;h2&gt;Don’t store backups&lt;/h2&gt;

&lt;p&gt;By default, Git will store a backup of the original file with conflict markers after a successful merge. These files will have a &lt;code&gt;.orig&lt;/code&gt; extension added to the original filename. When I configure Git, I ensure that these files are not saved. To globally disable backups of the original merge file, run the following:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git config --global mergetool.keepBackup &lt;span class="nb"&gt;false&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Visual Difftool Configuration&lt;/h2&gt;

&lt;p&gt;You may also setup Diffmerge to be your visual Difftool by running the following commands in your terminal:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git config --global diff.tool diffmerge
git config --global difftool.diffmerge.cmd &lt;span class="s2"&gt;&amp;quot;diffmerge \$LOCAL \$REMOTE&amp;quot;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This will allow you to compare two different versions of files visually. For example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git difftool master~1 foo.rb
&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/TPDO7DR-_S4" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2010/04/how-to-setup-git-to-use-diffmerge</feedburner:origLink></item>
    <item>
      <title>Binged In Action</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/Jwiq2oFrNX4/binged-in-action</link>
      <pubDate>Sun, 21 Mar 2010 00:00:00 +0000</pubDate>
      <author>Kevin Faustino</author>
      <guid isPermaLink="false">http://adventuresincoding.com/2010/03/binged-in-action</guid>
      <description>&lt;p&gt;In our &lt;a href="http://adventuresincoding.com/2010/03/outsource-your-site-wide-search-with-binged/"&gt;last post&lt;/a&gt;, we introduced &lt;a href="http://github.com/kfaustino/binged"&gt;Binged&lt;/a&gt;, a simple api wrapper for the Bing API. To demonstrate how to utilize this gem in your Ruby on Rails projects, view the screencast below:&lt;/p&gt;

&lt;div class="video-container"&gt;
&lt;iframe src="http://player.vimeo.com/video/10337665?title=0&amp;amp;byline=0&amp;amp;portrait=0"  width="500" height="281" frameborder="0"&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/Jwiq2oFrNX4" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2010/03/binged-in-action</feedburner:origLink></item>
    <item>
      <title>Outsource your site-wide search with Binged</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/QG6-HsfmLco/outsource-your-site-wide-search-with-binged</link>
      <pubDate>Mon, 15 Mar 2010 00:00:00 +0000</pubDate>
      <author>Kevin Faustino</author>
      <guid isPermaLink="false">http://adventuresincoding.com/2010/03/outsource-your-site-wide-search-with-binged</guid>
      <description>&lt;h2&gt;Why use Bing for your site?&lt;/h2&gt;

&lt;p&gt;The &lt;a href="http://www.bing.com/developers"&gt;Bing API&lt;/a&gt; does not cost you a single penny to use. It’s completely free! Bing’s biggest competitor, &lt;a href="http://www.google.com/sitesearch/"&gt;Google Site Search&lt;/a&gt; on the other hand can cost you from $100 annually to a price point only known by calling a google sales representative. Looking at the their small business plan, it will cost $250 annually for 1,001 – 5000 indexed pages. If your site has a lot of pages, it will start feeling very limited. Using the Bing API, you have access to the same results as the Bing search engine and not a small subset. The one limitation to the API is that you can only request the first 1000 results of a query. If your site displays 20 results per page, that is a total of 50 pages of search results.&lt;/p&gt;

&lt;p&gt;Bing also provides unlimited bandwidth for queries as long as you make less than 7 queries per second per IP address. In a single day, you can query 518,400 times per server. That is a magnitude of search queries that will satisfy even large web applications. Compare this with the $750 annual google plan that only allows 500,000 queries per year!&lt;/p&gt;

&lt;p&gt;Finally,  the another great benefit of using the Bing API is that you can style the results to match the look and feel of your site.&lt;/p&gt;

&lt;h3&gt;The Rules&lt;/h3&gt;

&lt;p&gt;Based on the &lt;a href="http://www.bing.com/developers/tou.aspx"&gt;Bing Web Services API Terms of Use&lt;/a&gt;, the following rules must be followed in order to use Bing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is to only be used for display in your Website or application.&lt;/li&gt;
&lt;li&gt;You must display some form of attribution to Bing compliant with the Microsoft branding rules.&lt;/li&gt;
&lt;li&gt;Do not filter any results you request.&lt;/li&gt;
&lt;li&gt;If you interleave with other sources, you must clearly differentiate those sources.&lt;/li&gt;
&lt;li&gt;Be sure to read the full Terms of Use to know exactly what you are entitled to.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Bing in Ruby via Binged&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://github.com/kfaustino/binged"&gt;Binged&lt;/a&gt; is a ruby gem I wrote that wraps the Bing API. It allows ruby developers to work with bing using all the common ruby syntax and idioms they know. Binged also has a chainable query syntax to allow easy query construction based on a large set of available filters.&lt;/p&gt;

&lt;h3&gt;Installation&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;[sudo] gem install binged&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;Get Your Bing API key&lt;/h3&gt;

&lt;p&gt;To use binged, you will require a Bing API key. Create one at &lt;a href="http://www.bing.com/developers/createapp.aspx"&gt;http://www.bing.com/developers/createapp.aspx&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Usage&lt;/h2&gt;

&lt;h3&gt;Instantiate a Client&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;binged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Binged&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Client&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="ss"&gt;:api_key&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;binged&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The client is used to search via different sources and holds a reference to your api key to simplify calls to the API.&lt;/p&gt;

&lt;h3&gt;Ruby on Rails configuration&lt;/h3&gt;

&lt;p&gt;Binged allows for configuration to be done once using a configure block. To use binged in your Ruby on Rails project, configure it globally in an initializer like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# config/initializers/binged.rb&lt;/span&gt;
&lt;span class="no"&gt;Binged&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="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;api_key&amp;#39;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Globally configuring Binged removes the need to provide the client with any credentials or options in your code as they would have already been set. For example, defining a client would be as follows:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;binged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Binged&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Client&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;Web Search Example&lt;/h3&gt;

&lt;p&gt;The following example demonstrates how to search within a specific site for a keyword and provide a maximum of 30 results:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="no"&gt;Binged&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Client&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;web&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;ruby&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;from_site&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;adventuresincoding.com&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;per_page&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&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;result&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;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;For simple site-wide search, Bing is the optimal solution for most web applications. It is free and the usage limits are so  lax, they can almost be thought of as unlimited. Be sure to check out the &lt;a href="http://kfaustino.github.com/binged/"&gt;Binged documentation&lt;/a&gt; to get a grasp of all the conditions you can specify in your queries. If you find a bug or have a great feature idea, &lt;a href="http://github.com/kfaustino/binged/issues"&gt;create an issue&lt;/a&gt; over at GitHub.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;View the &lt;a href="http://adventuresincoding.com/2010/03/binged-in-action/"&gt;Binged screencast&lt;/a&gt; on how to integrate Binged with your Ruby on Rails application.&lt;/p&gt;

&lt;p&gt;Creative Commons: &lt;a href="http://www.flickr.com/photos/chrisjohnbeckett/"&gt;http://www.flickr.com/photos/chrisjohnbeckett/&lt;/a&gt; / &lt;a href="http://creativecommons.org/licenses/by-nc-nd/2.0/"&gt;CC BY-NC-ND 2.0&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/QG6-HsfmLco" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2010/03/outsource-your-site-wide-search-with-binged</feedburner:origLink></item>
    <item>
      <title>How to clean up your ActiveRecord migrations</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/8KYbW5ebPwc/how-to-clean-up-your-activerecord-migrations</link>
      <pubDate>Thu, 18 Feb 2010 00:00:00 +0000</pubDate>
      <author>Kevin Faustino</author>
      <guid isPermaLink="false">http://adventuresincoding.com/2010/02/how-to-clean-up-your-activerecord-migrations</guid>
      <description>&lt;p&gt;Migrations in Ruby on Rails allow you to alter your database using a convenient and easy DSL.  When I started using Rails, migrations were a breath of fresh air. Database schema were finally easy. No longer did I have to pass SQL scripts to developers to run against their development DB or make a huge script to run at deployment. Migrations took care of this. However the problem with migrations is that after a while, a huge amount of migration files build up in your project. Below are detailed steps on how to clean up your migrations to make managing them easier.&lt;/p&gt;

&lt;h2&gt;Step 1: Ensure you include &lt;code&gt;db/schema.rb&lt;/code&gt; in your SCM&lt;/h2&gt;

&lt;p&gt;The file &lt;code&gt;db/schema.rb&lt;/code&gt; is the current definition of your database. It is versioned and updated after a migration is run. This file is essential in creating your database once all your older migrations are gone. Remember that &lt;code&gt;db/schema.rb&lt;/code&gt; is db agnostic, so any implementation specific db statements in your migrations will not exist in the schema.&lt;/p&gt;

&lt;h2&gt;Step 2: Migrate to the latest version&lt;/h2&gt;

&lt;p&gt;Notify your team to checkout the latest version of your project and migrate. This will ensure that future migrations will continue to run smoothly on their current development database.&lt;/p&gt;

&lt;h2&gt;Step 3: Seed your database with db/seeds.rb&lt;/h2&gt;

&lt;p&gt;Go through each of your migrations and ensure that no data is being inserted in any of them. Migrations are only supposed to be used to manipulate the structure of your database, not populate it.  Rails provides a file &lt;code&gt;db/seeds.rb&lt;/code&gt; where you can run ruby code and model creation statements to set all the data required by your application to start.&lt;/p&gt;

&lt;p&gt;Below is an example of a &lt;code&gt;db/seeds.rb&lt;/code&gt; file setting up the users of a system:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&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="o"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;admin&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;foo&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;bar&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;You can populate your database with seed data anytime by running rake db:seed. Seeding was added in Ruby on Rails 2.3.4.&lt;/p&gt;

&lt;h2&gt;Step 4:  Keep only latest migrations&lt;/h2&gt;

&lt;p&gt;Go to &lt;code&gt;db/migrate&lt;/code&gt; folder and delete all but your latest migration or move the older migrations to a folder such as &lt;code&gt;db/archieved_migrations&lt;/code&gt;. You will want to keep the last migration as a reference if you need to migrate down.&lt;/p&gt;

&lt;h2&gt;Step 5: Setting up a fresh environment&lt;/h2&gt;

&lt;p&gt;Even though all your migrations are gone except for one, a developer can setup a fresh environment with a development database containing all required data by running one rake task: rake db:setup. This task will create the database, load the entire schema from &lt;code&gt;db/schema.rb&lt;/code&gt;, and then populate the database based on &lt;code&gt;db/seeds.rb&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Creative Commons:
&lt;a href="http://www.flickr.com/photos/kaiton/"&gt;http://www.flickr.com/photos/kaiton/&lt;/a&gt; / &lt;a href="http://creativecommons.org/licenses/by-sa/2.0/"&gt;CC BY-SA 2.0&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/8KYbW5ebPwc" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2010/02/how-to-clean-up-your-activerecord-migrations</feedburner:origLink></item>
    <item>
      <title>Riding Ruby on Rails 3: Bundler</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/dvFqZG2q5ac/riding-ruby-on-rails-3-bundler</link>
      <pubDate>Tue, 09 Feb 2010 00:00:00 +0000</pubDate>
      <author>Kevin Faustino</author>
      <guid isPermaLink="false">http://adventuresincoding.com/2010/02/riding-ruby-on-rails-3-bundler</guid>
      <description>&lt;p&gt;This week a gift was given to us by the rails core team with the release of the Ruby on Rails 3 beta. To prepare both newcomers and experienced Rails developers, AdventuresInCoding is going to be releasing screencasts and posts about Ruby on Rails 3. Our first screencast is about gem dependency management with &lt;a href="http://github.com/carlhuda/bundler"&gt;Bundler&lt;/a&gt;.&lt;/p&gt;

&lt;div class="video-container"&gt;
&lt;iframe src="http://player.vimeo.com/video/9282967?title=0&amp;amp;byline=0&amp;amp;portrait=0"  width="500" height="281" frameborder="0"&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/dvFqZG2q5ac" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2010/02/riding-ruby-on-rails-3-bundler</feedburner:origLink></item>
    <item>
      <title>Taking the helm of ruby with Ruby Version Manager</title>
      <link>http://feedproxy.google.com/~r/AdventuresInCoding/~3/ukTqrwLisao/taking-the-helm-of-ruby-with-ruby-version-manager</link>
      <pubDate>Mon, 08 Feb 2010 00:00:00 +0000</pubDate>
      <author>Kevin Faustino</author>
      <guid isPermaLink="false">http://adventuresincoding.com/2010/02/taking-the-helm-of-ruby-with-ruby-version-manager</guid>
      <description>&lt;p&gt;&lt;a href="http://rvm.beginrescueend.com/(RVM"&gt;Ruby Version Manager&lt;/a&gt;) the name says it all. The sole purpose of this gem is to mange which version of ruby your system is currently utilizing. The days of creating symbolic links to different installations of ruby are over. With a single command, RVM will change the version of ruby and its gems for your entire user profile.&lt;/p&gt;

&lt;p&gt;Developers using Mac OS X can start using ruby the moment they boot up their machine for the first time. In fact, the latest version of Mac OS X Snow Leopard ships with ruby 1.8.7 MRI patch level 77. The problem with system versions such as the one bundled with Mac OS X is that they stagnate, causing developers to miss out on the latest patches applied to ruby. For example. my current default version of ruby is 1.8.7 MRI patch level 248.&lt;/p&gt;

&lt;p&gt;Besides the benefits of patches, why would a developer with a default version of ruby care about managing it? According to a recent report by &lt;a href="http://railslab.newrelic.com/2010/01/07/state-of-the-stack-a-ruby-on-rails-benchmarking-report-7-january-2010"&gt;New Relic&lt;/a&gt;, 63.5% of all deployments use ruby 1.8.6 MRI. This indicates that a large portion of the ruby community does not want to make the jump to higher version numbers. RVM provides you the tools to test, benchmark and switch your ruby versions with minimal effort. Before RVM, you would have had to compile which version of ruby you wanted, manage the symlink to it and update the PATH environment variable.  If you wanted another version installed, you would have to link it to another name such as ruby19.&lt;/p&gt;

&lt;h2&gt;Installing RVM&lt;/h2&gt;

&lt;p&gt;Before installing RVM, make sure you have the latest Xcode developer tools installed as you will experience compilation issues with certain gems if you do not. Mongrel for example will not compile its native extensions without the latest Xcode developer tools.&lt;/p&gt;

&lt;p&gt;To get started, you must first install the gem:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;sudo gem install rvm
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Once  installed, initialize the RVM installation and follow all prompts:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;rvm-install
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Finally, install any version of ruby you would like to use, in this case the latest version of 1.8.7 MRI:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;rvm install 1.8.7
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Switching Between Ruby Versions&lt;/h2&gt;

&lt;p&gt;To switch between versions of ruby, pass the use action to RVM:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;rvm use 1.9.1 
rvm use 1.9.1-p378
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;RVM allows you to set a default version via the –default flag to easily switch to the version of ruby you use most:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;rvm 1.8.7 --default 
rvm default
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If you ever feel the need to use the original system version of ruby, RVM allows switching via the system action:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;rvm system
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Installing Gems&lt;/h2&gt;

&lt;p&gt;All gems are installed in their own gem directory for each version of ruby. You would install gems with RVM the same way you do now, except you do not prefix sudo to the gem command. RVM runs under your user profile. By running sudo, you are telling the system to run as the root user which does not have your RVM setup.&lt;/p&gt;

&lt;p&gt;Example, install the rails gem:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;gem install rails
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;RVM also allows installation of gems on multiple versions of ruby:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;rvm 1.8.7,1.9.1 gem install rails
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;RVM Configuration&lt;/h2&gt;

&lt;p&gt;To set common flags when compiling ruby gems, RVM  allows settings to be placed in a &lt;code&gt;.rvmrc&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Here are the contents of my &lt;code&gt;~/.rvmrc&lt;/code&gt; file:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;rvm_archflags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;-arch x86_64&amp;quot;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;MySQL Gem&lt;/h2&gt;

&lt;p&gt;Some gems require flags such as the MySQL gem. To install the MySQL gem make sure you have set rvm_archflags in your .rvmrc file.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;gem install mysql -- --with-mysql-config&lt;span class="o"&gt;=&lt;/span&gt;/usr/local/mysql/bin/mysql_config
&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;The Benefits&lt;/h2&gt;

&lt;p&gt;Besides the benefit of being able to experiment with any ruby interpreter and version, RVM has a few killer features as well. For example, RVM allows the execution of ruby programs and rake tasks across all of your ruby versions, which ensures backwards compatibility and future proofing of your ruby gems, applications, etc.&lt;/p&gt;

&lt;p&gt;To run a ruby program against all installed ruby interpreters, use the ruby action:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;rvm ruby hello_world.rb
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As with most actions, the ruby action can be restricted to a few versions:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;rvm ruby 1.8.7,jruby hello_word.rb
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The feature I use most often is the ability to run your rake tasks across all interpreters. This is done via the rake action.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;rvm rake spec
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Running the above command runs all of your specs across the installed interpreters. Since running your specs is a common task, RVM provides a shortcut:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;rvm specs
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Finally, with minimal effort, RVM can help you learn how fast your code will run under each ruby version through the benchmark action.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;rvm benchmark hello_world.rb
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Ruby Version Manager is an amazing project by &lt;a href="http://github.com/wayneeseguin"&gt;Wayne E. Seguin&lt;/a&gt;. It is an essential tool for gem authors and any ruby developer who has to maintain applications across multiple versions of ruby. I have been using RVM for over 6 months and it has made managing ruby a pleasure.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AdventuresInCoding/~4/ukTqrwLisao" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://adventuresincoding.com/2010/02/taking-the-helm-of-ruby-with-ruby-version-manager</feedburner:origLink></item>
  </channel>
</rss>
