Blog

Last week, RSpec 1.1.4 was released; and given my optimistic disposition I immediately upgraded. Happily, the upgrade was almost seamless. Two interdependent issues kept this from being a flawless upgrade:

  1. A new deprecation warning on our helper specs (no one really likes to see deprecation warnings)
  2. A bug in the new way to write helper specs (a show stopper)

Lets see what happens when we run our spec1.

Wonderland$ spec spec/helpers/dog_helper_spec.rb 
Modules will no longer be automatically included in RSpec version 1.1.4.  Called from ./spec/helpers/dog_helper_spec.rb:8
.Modules will no longer be automatically included in RSpec version 1.1.4.  Called from ./spec/helpers/dog_helper_spec.rb:16
.
Finished in 0.393129 seconds
2 examples, 0 failures

So, what does this deprecation warning tell us? Lets examine the helper spec that generated these warnings:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe DogHelper do

  describe "name_or_description()" do
    it "should return a description for a dog without a name" do
      dog = mock_model(Dog, :name => nil, :sex => 'male', :breed => 'Labrador', :color => 'black')
      name_or_description(dog).should == "(male Labrador, black)" 
    end
  end

  describe "owner_link()" do
    it "should return a link to the owner" do
      owner = mock_model(User, :login => 'sally_smith', :to_param => 'sally_smith')
      dog = mock_model(Dog, :owner => owner)
      owner_link(dog).should =~ /users\/sally_smith/
    end
  end
end

We are being warned that calls to name_or_description and owner_link, which are called on the implicit self of the RSpec it blocks, are going to fail in the future when the helper module (DogHelper in this example) is no longer automatically included by RSpec.

Or put another way, calling helper methods like name_or_description directly, exactly as one might in a view, will no longer work.

The new way to call helper methods is through the helper attribute of RSpec, giving access to an ActionView::Base instance with the helper module included. Lets go ahead and make this change to our two helper calls:

      helper.name_or_description(dog).should == "(male Labrador, black)" 
      helper.owner_link(dog).should =~ /users\/sally_smith/

Lets try this out!

Wonderland$ spec spec/helpers/dog_helper_spec.rb 
.F
1)
NoMethodError in 'DogHelper owner_link() should return a link to the owner'
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.url_for
(eval):17:in `user_path'
/Users/leshill/src/rails/rspec114/app/helpers/dog_helper.rb:9:in `owner_link'
./spec/helpers/dog_helper_spec.rb:16:
Finished in 0.398686 seconds
2 examples, 1 failure

Hmmm, we have gotten rid of the deprecation warnings, but we now have a new (and truly unexpected!) failure.

This is a bona-fide bug in 1.1.4, and might lead to our having to rollback to 1.1.3 if there was no easy workaround. A quick search on the github repository and there is a fix already available for 1.1.5.

Lets adapt that fix, and use it within our own specs. Add this before block to our spec:

  before(:each) do
    # Patch until 1.1.5
    helper_controller = @controller
    helper.instance_eval { @controller = helper_controller }
  end

And then try it out:

Wonderland$ spec spec/helpers/dog_helper_spec.rb 
..
Finished in 0.40663 seconds
2 examples, 0 failures

Much better!

1 I am using a mock spec to demonstrate the issue, the minimal Rails app which fits around this spec is left as an exercise for the reader.

I have worked on many applications that require breadcrumbs. Every time, one of the developers has built a quick custom solution. Finally, I decided that we needed a complete solution that we could easily add to future projects, and thus Crummy was born.

Requirements

I set a few requirements that must be fulfilled in order for me to call the plugin a success. They were:

  • Ability to add breadcrumbs at Controller class level (like a before filter)
  • Ability to add breadcrumbs in the controller methods (such as actions)
  • Ability to add breadcrumbs from the views
  • Ability to add breadcrumbs with or without an url
  • No variable conflicts (such as instance variables)
  • Full and strong spec coverage
  • Good documentation

I managed to fulfill these requirements and add a few shiny methods to make life easy.

Examples of Usage

    class ApplicationController < ActionController::Baseee
      add_crumb 'Home', '/' # This will display on EVERY list of breadcrumbs
    end
  
    class BusinessController < ApplicationController
      # The load order DOES matter.
      add_crumb("Businesses") { |instance| instance.send :businesses_path } # Show on all actions
      add_crumb("Comments", :only => [:comments, :show]) { |instance| instance.send :business_comments_path } # Only add the comments link on the comments and show actions.
      before_filter :load_comment, :only => "show"

      def show
       add_crumb @business.display_name, @business
      end

      def load_comment
       @comment = Comment.find(params[:id])
      end
    end
  

Golden Feature

One of the most useful argument usage is add_crumb :comment. What this does is turns the symbol into an instance variable (@comment.) Then it roughly translates into:
  # add_crumb :comment
  before_filter :add_comment_breadcrumb
  def add_comment_breadcrumb
    add_crumb @comment.to_s, @comment
  end

Installation

script/plugin install git://github.com/zachinglis/crummy.git

RubyJax, the Ruby and Rails user group for Jacksonville, FL, is a monthly get-together with one or more presentations. Pizza and refreshments always accompany the awesome line-up of topics and presenters. Past topics include Advanced Active Record, jQuery, RSpec and Amazon EC2.

The group is populated with lovable characters from the local Ruby community like Steven Bristol of Less Everything, our own Obie Fernandez (author of The Rails Way) and a collection really smart tech people who've embraced Ruby for all the right reasons. Defectors from Java, ColdFusion, .Net, and PHP can be found at each meeting.

RubyJax members are also active in the mailing list and IRC channel

Meetings are typically held the third Thursday of each month but, as the group is made up of entrepreneurs, self-employed, and family people life tends to intervene and move the occasional meeting. To make sure you find us check out the group calendar.

We've been pretty excited about the feedback we've received so far on our flagship product offering, 3-2-1 Launch. It seems as though a lot of people are getting on board with the idea of rapid-fire, super-focused development. Sure, we've had a few "that's crazy, dudes" or "you must be smoking a hashrocket," but we've also had dozens of inquiries from enthusiastic folks who want help getting off on the right foot.

And we're not offering the unattainable. 3-2-1 Launch is about beginning with a set of reasonable mutual expectations, working swiftly and building a great 1.0 version of a site in three days. Our theory is that working within such a short time frame will force quick, efficient decision making, thereby eliminating the extraneous, clunky development that we see so much of these days. For a client, this means getting a solid and fertile site foundation in the first go-around.

Soon, we'll be adding an FAQ page to answer all of your questions about the 3-2-1 Launch process. Until then, keep 'em comin', Gleep-Glop.

Thanks to a lot of hard work from the Hashrocket team and our friends at nGen Works, hashrocket.com launches today!

We’re all pretty excited about this, as the site’s launch represents not only months and months of team discussions, brainstorming and planning, but also hours of grueling, drunken hot-tubbery and thousands of hard-fought ping-pong matches. Needless to say, there is a price for success that this team is willing to pay.

Our official press release is forthcoming so I’ll spare you the gory details on our product offerings. We do, however, have a few projects in the works that we think are pretty cool: clean-undies.com is an emergency contact list service that allows you to post your “in case of emergency” instructions and contact info to a website that your friends and family can access. CityCliq.com, our enhanced Yellow Pages site, has been in the testing phase for about a month now and will be entering its second iteration soon.

So, between these sites and our upcoming projects, things are looking good. We’ll be sure to keep you posted on all of our project and team news as things happen.

Cheers!
Hashrocket

Blog Archive »