<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,5 +1,7 @@
 *2.3.0 [Edge]*
 
+* Add :allow_nil option to delegate. #1127 [Sergio Gil]
+
 * Add Benchmark.ms convenience method to benchmark realtime in milliseconds.  [Jeremy Kemper]
 
 * Updated included memcache-client to the 1.5.0.5 version which includes fixes from fiveruns and 37signals to deal with failover and timeouts #1535 [Joshua Sierles]</diff>
      <filename>activesupport/CHANGELOG</filename>
    </modified>
    <modified>
      <diff>@@ -72,6 +72,30 @@ class Module
   #   invoice.customer_name    # =&gt; &quot;John Doe&quot;
   #   invoice.customer_address # =&gt; &quot;Vimmersvej 13&quot;
   #
+  # If the object to which you delegate can be nil, you may want to use the
+  # :allow_nil option. In that case, it returns nil instead of raising a
+  # NoMethodError exception:
+  #
+  #  class Foo
+  #    attr_accessor :bar
+  #    def initialize(bar = nil)
+  #      @bar = bar
+  #    end
+  #    delegate :zoo, :to =&gt; :bar
+  #  end
+  #
+  #  Foo.new.zoo   # raises NoMethodError exception (you called nil.zoo)
+  #
+  #  class Foo
+  #    attr_accessor :bar
+  #    def initialize(bar = nil)
+  #      @bar = bar
+  #    end
+  #    delegate :zoo, :to =&gt; :bar, :allow_nil =&gt; true
+  #  end
+  #
+  #  Foo.new.zoo   # returns nil
+  #
   def delegate(*methods)
     options = methods.pop
     unless options.is_a?(Hash) &amp;&amp; to = options[:to]
@@ -84,10 +108,12 @@ class Module
 
     prefix = options[:prefix] &amp;&amp; &quot;#{options[:prefix] == true ? to : options[:prefix]}_&quot;
 
+    allow_nil = options[:allow_nil] &amp;&amp; &quot;#{to} &amp;&amp; &quot;
+
     methods.each do |method|
       module_eval(&lt;&lt;-EOS, &quot;(__DELEGATION__)&quot;, 1)
         def #{prefix}#{method}(*args, &amp;block)
-          #{to}.__send__(#{method.inspect}, *args, &amp;block)
+          #{allow_nil}#{to}.__send__(#{method.inspect}, *args, &amp;block)
         end
       EOS
     end</diff>
      <filename>activesupport/lib/active_support/core_ext/module/delegation.rb</filename>
    </modified>
    <modified>
      <diff>@@ -41,6 +41,10 @@ Invoice   = Struct.new(:client) do
   delegate :street, :city, :name, :to =&gt; :client, :prefix =&gt; :customer
 end
 
+Project   = Struct.new(:description, :person) do
+  delegate :name, :to =&gt; :person, :allow_nil =&gt; true
+end
+
 class Name
   delegate :upcase, :to =&gt; :@full_name
 
@@ -117,6 +121,29 @@ class ModuleTest &lt; Test::Unit::TestCase
     end
   end
 
+  def test_delegation_with_allow_nil
+    rails = Project.new(&quot;Rails&quot;, Someone.new(&quot;David&quot;))
+    assert_equal rails.name, &quot;David&quot;
+  end
+
+  def test_delegation_with_allow_nil_and_nil_value
+    rails = Project.new(&quot;Rails&quot;)
+    assert_nil rails.name
+  end
+
+  def test_delegation_with_allow_nil_and_nil_value_and_prefix
+    Project.class_eval do
+      delegate :name, :to =&gt; :person, :allow_nil =&gt; true, :prefix =&gt; true
+    end
+    rails = Project.new(&quot;Rails&quot;)
+    assert_nil rails.person_name
+  end
+
+  def test_delegation_without_allow_nil_and_nil_value
+    david = Someone.new(&quot;David&quot;)
+    assert_raises(NoMethodError) { david.street }
+  end
+
   def test_parent
     assert_equal Yz::Zy, Yz::Zy::Cd.parent
     assert_equal Yz, Yz::Zy.parent</diff>
      <filename>activesupport/test/core_ext/module_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>f7bd0beb67c5d9d50e37aa596605b91e61197fbe</id>
    </parent>
  </parents>
  <author>
    <name>Sergio Gil</name>
    <login>porras</login>
    <email>sgilperez@gmail.com</email>
  </author>
  <url>http://github.com/rails/rails/commit/e8de7a67a5ef063164da022845a7cae1753da80e</url>
  <id>e8de7a67a5ef063164da022845a7cae1753da80e</id>
  <committed-date>2008-12-21T15:24:06-08:00</committed-date>
  <authored-date>2008-12-07T15:53:38-08:00</authored-date>
  <message>Add :allow_nil option to delegate [#1127 state:resolved]

Signed-off-by: Pratik Naik &lt;pratiknaik@gmail.com&gt;</message>
  <tree>6b9f49120dd9f95b4b5800454520bf1cc4dcf793</tree>
  <committer>
    <name>Pratik Naik</name>
    <login>lifo</login>
    <email>pratiknaik@gmail.com</email>
  </committer>
</commit>
