rails/rails — pull request #11908 by jackxxu (2013-08-15 23:56:09+00:00)
Title: Added the :default option to Module delegation

Add :default option to Module delegation

When using Rails Module delegation, if the target object can't guarantee to respond to all the methods that are specified, having a default value can help to handle the situation gracefully. This has been discussed a few times in online forums. It is particularly useful when the target is polymorphic.

Here is an example:

```ruby
Employee = Struct.new(:name, :employee_id)
Guest = Struct.new(:name)

Entry = Struct.new(:person) do
delegate :name, :to => :person, :allow_nil => true
delegate :employee_id, :to => :person, :default => 'N/A'
delegate :birth_date, :to => :person, :default => nil
delegate :ssn, :to => :person, :default => 'N/A', :allow_nil => true
end

guest_entry = Entry.new(Guest.new('John'))
guest_entry.birth_date # => ‘N/A’
```