Author: Matthew

  • Textile toolbar plugin for Rails

    Have you ever wanted a WYSIWYG-style toolbar that generates Textile markup? textile_toolbar is a Rails plugin that does just that. Extracted from a recent Terralien project, it’s a great way get your users used to the power and simplicity of Textile without frightening them away with long markup guides. Check out the announcement on the Terralien blog for more information.

    To install from the command line:

    script/plugin install git://github.com/pelargir/textile_toolbar.git
    

    Update: A live example of a Textile-enabled text area can now be found here.

  • Surrounding text with a tag using Ruby’s gsub

    Here’s how to identify a keyword in a block of text using a case-insensitive regular expression, and surround that keyword with an HTML tag:

    text = "hello world, this is a test"
    text.gsub!(/(WORLD)/i, "\1")
    

    The following string is produced:

    hello world, this is a test
    

    Note that \1 references the group in the regex (surrounded by parens). This results in the original value being placed within the HTML tags, instead of the uppercased WORLD in the regex.

  • rspec_validation_expectations gem released

    I just released a new gem on GitHub that provides some common validation expectations to rspec. Instead of writing specs to verify that your models are handling validation correctly, these expectations simply check that the validation is getting declared correctly in your model. For example:

    describe User do
      it_should_validate_presence_of :first_name, :last_name, :email
      it_should_validate_numericality_of :zip
      it_should_validate_uniqueness_of :email
    end
    

    Since the expectations never hit the database, they are also faster than testing the traditional way. It’s dead simple to install on Rails 2.1 or later:

    script/plugin install git://github.com/pelargir/rspec_validation_expectations.git
    

    The expectations become available to your specs immediately.

  • RubyCamp is coming to Raleigh

    Raleigh’s first RubyCamp is coming to Red Hat on October 18th. This is a similar format to BarCamp in that the presentations are pitched the morning of the conference, and attendees self organize the remainder of the day. Relevance will be running their popular Refactotum workshop in the morning. The conference is free, but attendance is capped at 200 so visit the wiki to grab your spot.

  • Lindo graduates to a gem

    Lindo renders the body of an HTTP response from inside a Rails functional or integration test. It used to be a plugin, but now it’s a gem. Why? Because Rails gems are the new hotness. Now go get it.

  • finder_filter gem released

    I’m at the Ruby Hoedown in Huntsville this weekend. Being around so many brilliant geeks encouraged me to release a gem I’ve had sitting in the hopper for several weeks.

    finder_filter encapsulates a pattern I find myself using quite frequently in Rails. Namely, looking up an instance variable before an action. For example:

    class UsersController < ActionController::Base
      before_filter :find_user, :only => [:show, :edit]
      
      def show
        # do something with @user
      end
      
      def edit
        # do something with @user
      end
      
      def find_user
        @user = User.find(params[:id)
      end
    end
    

    Sticking the finder in a before filter keeps the code DRY, but it still takes several lines to do this. finder_filter reduces this to a single line of code:

    class UsersController < ActionController::Base
      finder_filter :only => [:show, :edit]
      
      def show; end
      def edit; end
    end
    

    There are other options to customize the column and param used in the lookup. Check out the README for full details.

    To install the gem as a plugin in your Rails project:

    sudo gem install pelargir-finder_filter --source=http://gems.github.com
    

    Then open environment.rb in your Rails app and add the gem as a dependency in your initializer block:

    Rails::Initializer.run do |config| 
      config.gem "pelargir-finder_filter"
      ...
    end
    

    If you have any comments or suggestions, I’d love to hear from you. Contact me through the finder_filter project on GitHub.

  • Pair programming showdown at BarCampRDU

    BarCampRDU 2008 came and went. It was quite enjoyable. There wasn’t as much grub as last year, but I thought the topics were more interesting.

    I gave a talk on pair programming during the afternoon. It really became more of a group discussion, which was exactly what I was hoping for. Some attendees have requested the slides so I’ve attached them to this post as a PDF.

    The slides have been edited somewhat. I gave this same presentation at Agile ITX last month and it was an hour and a half long. I had to cut out a few things for BarCamp. But the central ideas are still there.

  • Picking values from Ruby hashes

    Want to pick a certain set of key/value pairs from a Ruby hash? You might do this:

    hash = {:foo => "bar", :bar => "baz", :baz => "boo"}
    hash.select { |k,v| [:foo, :bar].include?(k) }
    
    # returns [[:foo, "bar"], [:bar, "baz"]]
    

    Kind of messy. We can do better by reopening the Hash class this way:

    class Hash
      def pick(*values)
        select { |k,v| values.include?(k) }
      end
    end
    

    Now our selection works like this:

    hash = {:foo => "bar", :bar => "baz", :baz => "boo"}
    hash.pick(:foo, :bar)
    
    # returns [[:foo, "bar"], [:bar, "baz"]]
    

    Ruby is a wonderful language. This is one small example of how having access to existing classes can be incredibly powerful. With this power comes great responsibility. Wield your power wisely.

  • Speaking at Agile ITX next weekend

    I’ll be speaking at the Agile ITX conference in Reston, Virginia on June 27th. My presentation is titled Pragmatic Pair Programming and is based on the many diverse pairing experiences I’ve had over the past six years. Mention pair programming in any crowd of programmers and you’ll get two responses: adoration or outright hatred. Why is pairing so controversial? Does it have any tangible benefits? That’s what we’ll be exploring together. Agile ITX is shaping up to be a great conference with many top-notch presentations. I’m excited about getting the chance to participate. Hope to see you there.