Five Things You Can Do Today to Make Your App Ready For Ruby on Rails 3

Thursday, February 4th, 2010

Rails 3 is coming!

I’ve been doing a lot of thinking about how I can help clients get in a better position to upgrade to Rails 3 when it’s ready for prime time. There’s a few things you can do today with your 2.3.x application to give it more Rails 3 flava. Yeah boy!!!

Use Bundler instead of config.gem

Yehuda Katz has done a good job replacing config.gem with a more platform agnostic utility called Bundler. The API has changed around a bit with the latest 0.9 release, but he supplies a decent guide. For details on what’s changed since that writeup, check out this post from Yehuda

Use inherited_resources to get respond_with

respond_with will be a nice shortcut in Rails 3 for the rendering end of RESTful controller actions. Jose Valim’s inherited_resources actually gives you respond_with and respond_to methods for Rails 2.3.x applications. No more repetitive and un-DRY respond_to blocks

Use rails_xss

Unsafe strings will automatically be escaped by default in Rails 3. If you want this behavior in 2.3.x, all you have to do is install Koz’s rails_xss. It is surprisingly hard to break out of the habit of using h() calls in your markup, though! Additionally, be wary that you might have to hack some of your view related plugins to get them working with rails_xss.

Use More Named Scopes

Fortunately, ActiveRecord find() calls will soon be unfound in Rails applications. Pratik has done some really cool stuff with active_record. These changes are all pretty significant, but perhaps the largest deal is that the find() method and its relatives will be deprecated come Rails 3.1. One thing that is remaining a fairly consistent part of the API is named_scopes (despite a name change).

Developers should be encouraged to use named_scopes and association chains even more aggressively, now. It will certainly be easier to refactor a few named scopes around than it would be to refactor scattered find() calls all over your libraries. You should be doing this anyway, because it definitely helps with improving code and test quality

An excellent gem that can help you with this is Ben Johnson’s searchlogic. It gives you a bunch of very useful, canned scopes.

Replace references to RAILS_ENV, RAILS_ROOT, and RAILS_DEFAULT_LOGGER

Thankfully, these ugly constants have no place in a Rails 3 application. There are now methods to access these properties as part of the Rails module. So, for example, you would use Rails.env instead of RAILS_ENV

Nick Quaranto actually just wrote a solid article with the details on the Rails module.

Boston.rb talk on Thinking Sphinx

Wednesday, March 11th, 2009

Last night I did a talk on Thinking Sphinx at Boston.rb.

The slides are below.

Evan’s blog post with the benchmarks can be found here. There are links to other benchmarks and details.

The process library I alluded to is called God.

Other links

Let me know what you thought of the talk and the slides. I hope you found it helpful!

Rails Magazine Inaugural Issue

Sunday, March 1st, 2009

The first issue of Rails Magazine has been published. It looks like there is a lot of great content from some great authors.Rails Magazine

I wrote an article about my switch from RSpec to Shoulda. A lot of issues I discussed have been addressed by both teams. Please participate in the discussion about the latest development in test frameworks.

I think it’s really cool that Rails now has a publication. Props to Olimpiu for putting it together.

Fix nil.rewrite errors in your Helper Tests

Sunday, December 14th, 2008

In testing my helpers, I discovered a nil.rewrite exception due to ActionController::Base.initialize_current_url not being called. This occurs when using url_for with a hash as arguments.

ActionView::TestCase does not initialize current url so you won’t be able to use *_url and *_path helpers generated from your routes. Put the following lines at the end of your test_helper.rb file to resolve the issue.

class ActionView::TestCase < ActiveSupport::TestCase
  class TestController < ActionController::Base
    attr_accessor :request, :response, :params

    def initialize
      @request = ActionController::TestRequest.new
      @response = ActionController::TestResponse.new
      
      #TestCase doesn't have context of a current url so cheat a bit
      @params = {}
      send(:initialize_current_url)
    end
  end
end

I've submitted a patch to core regarding this. Please +1 for the good of helper_tests!

Client Side TDD with JQuery and QUnit

Monday, September 29th, 2008

For a while now, I’ve been looking for a solid framework to make assertions around my client side code. In my move from prototype to jquery, I’ve begun to use JSON a lot more as responses to my AJAX requests. Because I’m not returning markup anymore, there is some logic in the presentation layer of my apps.

Thanks to Chad Myers for his simple intro to qUnit – I will definitely be trying it out in a few of my projects.