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.
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.