RSS
 

Let’s read some Rails code: with_options

05 Jun

Did you know you can refactor this:

# Duplicated :dependent => :destroy option.
class Account < ActiveRecord::Base
  has_many :customers, :dependent => :destroy
  has_many :products,  :dependent => :destroy
  has_many :invoices,  :dependent => :destroy
  has_many :expenses,  :dependent => :destroy
end

into this?

# Nice and DRY!
class Account < ActiveRecord::Base
  with_options :dependent => :destroy do |assoc|
    assoc.has_many :customers
    assoc.has_many :products
    assoc.has_many :invoices
    assoc.has_many :expenses
  end
end

The with_options method is a really cool chunk of code that lets you DRY up duplication that sometimes appear when passing the same options to a series of methods.

But the point of this post is how it works behind the scenes, so check out this 11-minute code walkthrough:

By the way, this is an excerpt of a longer screencast I’m working on about ActiveSupport internals. If you’d like to be notified when the full screencast is released, drop your email in this form. (One email, ever.)

You can also check out the other Rails-related screencasts I’ve already done.

 
No Comments

Posted in general

 

Video: write code faster — expert-level vim

30 May

In May of 2010 I gave a one-hour talk to the Boston Ruby Group titled ‘Write Code Faster: Expert-level Vim’.

I’d volunteered to give it as a test-run before delivering the same talk at RailsConf a month later.

Little did I know, someone recorded the Boston.rb talk and it recently surfaced on the presentations page.

If the title sounds interesting, go watch the talk!

 
 

Let’s read some Rails code: OrderedOptions

28 May

Curious about Rails’ internals? In the screencast below, I take you on a 5-minute tour of the OrderedOptions and InheritedOptions classes in ActiveSupport. Check it out for a code-level tour of an interesting bit of Rails.

By the way, this is an excerpt of a longer screencast I’m creating of guided tour through the best parts of the Rails source. If you’d like to be notified when the full screencast is released, drop your email in this form. (One email, ever.)

UPDATE: Youtube’s compression isn’t looking so hot, so if you prefer, you can download the high quality version here. (43MB)

 
1 Comment

Posted in general