This project is archived and is in readonly mode.

#1127 ✓resolved
porras

:allow_nil option for delegate

Reported by porras | September 27th, 2008 @ 08:43 PM | in 2.x

Adds :allow_nil option to delegate method:


  # If the object in 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 => :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 => :bar, :allow_nil => true
  #  end
  #
  #  Foo.new.zoo   # returns nil
  #

Comments and changes to this ticket

  • Daniel Schierbeck

    Daniel Schierbeck September 29th, 2008 @ 02:23 PM

    +1: This seems like a good addition to delegate, and I like the simplicity of the implementation.

  • Emili Parreño
  • David Röthlisberger

    David Röthlisberger October 14th, 2008 @ 02:06 PM

    1. Useful, and the patch is very simple.

    If anything, I'd change the rdoc to say "If the object to which you delegate...".

  • David Röthlisberger

    David Röthlisberger October 14th, 2008 @ 02:07 PM

    (Um, that "1." in the comment above was supposed to be a "+1").

  • Josh Susser

    Josh Susser October 14th, 2008 @ 03:30 PM

    -1

    I dislike silently hiding errors like that. Seems like just asking for trouble.

  • David Röthlisberger

    David Röthlisberger October 14th, 2008 @ 04:46 PM

    How is it hiding the error any more than:

    
    def contact_name; contact && contact.name; end
    

    It's intended as a shortcut for the case where not having a contact isn't an error. Remember you have to specify :allow_nil, this isn't going to be default behaviour.

  • porras

    porras December 8th, 2008 @ 12:09 AM

    I updated the patch, which no longer applied after the :prefix option was added.

    @Josh, as Dave explained, this option is intended only for the cases where this is not an error but a legitimate situation, just to avoid that verbose and ugly definition.

  • Daniel Schierbeck

    Daniel Schierbeck December 16th, 2008 @ 07:02 PM

    I'd like some more attention brought on this ticket (i.e. BUMP), as I've recently found use cases for it abound.

    I currently have the following simplified setup:

    # A resident may or may not have a current residentship -- he
    # could be a former resident.
    class Resident < ActiveRecord::Base
      has_many :residentships
    
      delegate :room_number, :room, :to => :residentship
    
      def residentship
        residentships.only_current.first
      end
    end
    
    class Residentship < ActiveRecord::Base
      belongs_to :resident
      validates_presence_of :room_number, :moved_out_at
      named_scope :only_current, ...
      composed_of :room, :mapping => %w(room_number number)
    end
    
    class Room # value class, as there's a finite set of rooms
      def residentship
        Residentship.only_current.find_by_room_number(@number)
      end
    
      delegate :resident, :to => :residentships
    end
    
    

    I'd like Room#resident, Resident#room and Resident#room_number (as well as several more in my real app) to work properly for former residents, i.e. just return nil -- but with the current implementation of delegate, they raise an exception instead. Adding an :allow_nil option to delegate would greatly reduce the number of extra methods i need to have.

  • Repository

    Repository December 22nd, 2008 @ 12:14 AM

    • State changed from “new” to “resolved”

    (from [e8de7a67a5ef063164da022845a7cae1753da80e]) Add :allow_nil option to delegate [#1127 state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...

  • Ryan Bigg

    Ryan Bigg October 9th, 2010 @ 09:49 PM

    • Tag cleared.

    Automatic cleanup of spam.

  • bingbing
  • yoyogo

    yoyogo March 31st, 2011 @ 05:59 AM

    oh my!! i love you so much!!

    Thank you so much.
    Hermes Bags
    http://www.louisvuittonparty.com/
    Hermes Bags

  • slina

    slina March 31st, 2011 @ 06:36 AM

    iPad 2 Video Converter is perfectly compatible with iPad 2

    iPad 2 Video Converter Mac is your best choice to convert videos to iPad 2 on Mac OS X.

    DVD to iPad 2 is specially designed for Apple users.

    DVD to iPad 2 Mac is a wonderful gift for Mac and iPad users

    Play 1080P Blu-ray movies with Blu-ray to iPad 2 Converter

    Blu-ray to iPad 2 Mac is definitely your best choice to convert Blu-ray DVD to iPad 2 for playback with with its large and high-resolution screen.

    Blu-ray to ipad 2

    Blu-ray to Nintendo 3DS Converter can easily rip/convert Blu-ray DVD, even protected DVDs and encrypted AACS/ BD+ Blu-ray movies to Nintendo 3DS easily and quickly.

    DVD to Nintendo 3DS, actally an all-in-one Blu-ray DVD Converter, enables users to convert Blu-ray (.m2ts) ) or DVD movies (.vob) or ISO image files or IFO to Nintendo 3DS/NDSL/ DS, iPhone 4, iPad 2, Motorola Xoom, Android Phones, iPod Touch, Xbox 360, PSP, PS3, Motorola Droid, Nexus One, HTC EVO 4G, etc for entertainment freely.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages