Friday, May 7, 2010

Delegation with Rails

Why delegation?

Before going to detail let me start with an example , we have a two models named User and profile having has-one association.

class User < ActiveRecord::Base
  has_one :profile
end

class Profile < ActiveRecord::Base
  belongs_to :user
end

Suppose Profile have attributes like first_name, last_name, phone etc and @user is the instance of User class.now to find name or phone of an user we have two ways -

1. Using dot-magic

FirstName = @user.profile.first_name

This is simple but becomes bulky for long chain associated object.For example @user.sth.sth.sth.sth…some_attribute

2. Using delegation

Instead we can delegate desired attributes in User model. Here is the code to add

class User < ActiveRecord::Base
  has_one :profile
  [:first_name, :last_name, :phone, ...etc.].each {|attr| delegate attr, :to => :profile}
end

Lets see usage of delegation in depth

Delegation in Rails

In ruby on rails Delegation provide us a fine way over dot-magic. Using delegation it is easy to access associated object’s attributes. Delegation is a feature Rails introduced in it’s 2.2 version.The concept of delegation is to take some methods and send them off to another object to be processed.Delegate simply delegates a method to another class.This is useful if you have a lot of cases where you are referring to fields in associated classes.

Let me explain this with a brief example:

Suppose you have a User class for anyone registered on your site, and a Consumer class for those who have actually placed orders:

class User < ActiveRecord::Base
  belongs_to :consumer
end

class Consumer < ActiveRecord::Base
  has_one :user
end

As for now, if you are in a Consumer instance, you can get their User information doing @consumer.user.name, or @consumer.user.email. Delegation allows you to simplify this:

class User < ActiveRecord::Base
  belongs_to :consumer
end

class Consumer < ActiveRecord::Base
  has_one :user
  delegate :name,:email,:to => :user
end

Now you can refer to @consumer.name and @consumer.email to retrieve and set values for those attributes directly.

This is all well and good if you can be sure that there will always be a user for any given consumer. If consumer’s user is nil and name is called, however, an exception will be raised (because nil.name is undefined). There’s a new feature in Rails 2.2 that relate to delegation and using a prefix for delegated methods also you can set default value by extending delegate method

Delegate with default

class Consumer < ActiveRecord::Base
  has_one :user
  delegate :name,:to => :user, :default => "Sapna Prajapati"
end

Delegate with prefixes

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:

class User < ActiveRecord::Base
  belongs_to :consumer
end

class Consumer < ActiveRecord::Base
  has_one :user
  delegate :name,:email,:to => :user ,prefix => true
end

This will produce delegated methods @consumer.user_name and @consumer.user_email.

The problem is if you refactor user or consumer , you’ll have to go back and fix all the places you did this access chaining as well. Means if you rename the user class, you’re almost always going to want to rename that prefix too.But delegates provide custom prefixes so you will end up to change.

Delegate with custom prefixes

class User < ActiveRecord::Base
  belongs_to :consumer
end

class Consumer < ActiveRecord::Base
  has_one :user
  delegate :name,:email,:to => :user ,prefix => :owner
end

This will produce delegated methods @consumer.owner_name and consumer.owner_email.

No comments:

Post a Comment