A Fresh Cup is Mike Gunderloy's software development weblog, covering Ruby on Rails and whatever else I find interesting in the universe of software. I'm a full-time Rails developer and contributor, available for long- or short-term consulting, with solid experience in working as part of a distributed team. If you'd like to hire me, drop me a line. I'm also the author of Rails Rescue Handbook and Rails Freelancing Handbook.

Navigation

A Fresh Cup

Notes on Rails and other development

Sunday
Oct192008

Coming in Rails 2.2: Delegate Prefixes

Delegates are a useful feature that I haven't seen used in that many Rails codebases - perhaps I've just been looking at the wrong codebases. Active Support makes them available for Modules generally, but the use case I find myself most often exercising is with Active Record classes. Delegates let you take some methods and send them off to another object to be processed.

For example, suppose you have a User class for anyone registered on your site, and a Customer class for those who have actually placed orders:

[sourcecode language='ruby']
class User << ActiveRecord::Base
belongs_to :customer
end

class Customer << ActiveRecord::Base
has_one :user
end
[/sourcecode]

If you're hanging on to a Customer instance, you can get their User information with methods like @customer.user.name and @customer.user.email.

Delegation allows you to simplify this:

[sourcecode language='ruby']
class User << ActiveRecord::Base
belongs_to :customer
end

class Customer << ActiveRecord::Base
has_one :user
delegate :name, :email, :to => :user
end
[/sourcecode]

Now you can refer to @customer.name and @customer.email directly. That's all been around and works in the current release version of Rails.

Delegate prefixes just appeared in edge Rails and will work in Rails 2.2. If you delegate behavior from one class to another, you can now specify a prefix that will be used to identify the delegated methods. For example:

[sourcecode language='ruby']
class User << ActiveRecord::Base
belongs_to :customer
end

class Customer << ActiveRecord::Base
has_one :user
delegate :name, :email, :to => :user, :prefix => true
end
[/sourcecode]

This will produce delegated methods @customer.user_name and @customer.user_email. You can also specify a custom prefix:

[sourcecode language='ruby']
class User << ActiveRecord::Base
belongs_to :customer
end

class Customer << ActiveRecord::Base
has_one :user
delegate :name, :email, :to => :user, :prefix => :account
end
[/sourcecode]

This will produce delegated methods @customer.account_name and @customer.account_email.
Friday
Oct172008

Double Shot #314

Just mass-unfollowed a bunch of people on Twitter. Sorry, folks, but something had to give.

  • Liquid Wiki - The Liquid markup language is something I'm currently struggling to make work. Documentation is sketchy, to say the least.

  • Rails for .NET Developers - Latest book from Pragmatic. I read it in manuscript; good stuff.

  • RailsConf'09: Accepting Proposals for Vegas! - Call me back when RailsConf is held in a city fit for human beings.
  • Wednesday
    Oct152008

    Rails 2.2 Deprecations

    Rails 2.2 won't be all about new features. The Rails team is also taking advantage of this release to deprecate some existing features. Here's a list of what will be going away in the 2.2 release:

  • The country_select helper has been removed. This is the one most likely to affect existing applications, I think, but it's going away because of arguments over which list of countries to use. The deprecation page points to a couple of replacement plugins.

  • Rails::SecretKeyGenerator has been replaced by ActiveSupport::SecureRandom.
    render_component is deprecated. If you need it, grab the render_components plugin.

  • ActiveRecord::Base.allow_concurrency no longer has any effect. Might as well stop calling it. Remember, though, that ActiveRecord is getting connection pool management.

  • ActiveRecord::Errors.default_error_messages has been deprecated in favor of I18n.translate('activerecord.errors.messages')

  • The %s and %d interpolation syntax for internationalization is deprecated.

  • String#chars has been deprecated in favor of String#mb_chars.

  • Durations of fractional months or fractional years are deprecated. Use Ruby's core +Date+ and +Time+ class arithmetic instead.
  • Tuesday
    Oct142008

    Double Shot #313

    Tonight I'm demonstrating Rails to the local .NET user group. I may survive.

  • Firefox 3.1 beta 1 now available for download - The alphas were pretty rough, but I'll be giving the beta a try shortly.

  • Slim-Attributes v0.5.0 released - ActiveRecord patch to speed up performance if you don't feel like limiting your finds to only the fields you need.

  • ScrumNinja - New hosted project management tool built for Scrum teams.

  • BackgrounDRb 1.1 - Better scripting, logging, tests, yay.
  • Tuesday
    Oct142008

    Double Shot #312

    The week started out with a bang: I completed the first draft of Getting Started with Rails and got started on a new client project as well.


  • Rails Migrations Cheatsheet - A compact guide, available in either HTML or PDF.

  • Announcing follow cost: Is that Twitter celebrity worth the pain? - According to this tool, following me results in much pain.

  • Blank: A Starter App for r_c and shoulda Users - Another attempt at a Rails starter app with all the trimmings. (via RailsGuts, Blank)

  • suprails - Early days yet, but worth watching: the plan is to create a configurable Rails application generator driven by its own little DSL.

  • Life in the fast lane - Thoughtbot introduces Pacecar, a plugin to generate a whole bunch of named scopes for you.