delegate - khanhld
TRANSCRIPT
Delegatein Ruby on Rails
● Delegate● Options● Demo
Delegate
● Easily expose contained objects’ public methods as your own.
● Useful with ActiveRecord associations● delegate :method_name, to: :target
Delegate
● Multiple delegates to the same target are allowed
● delegate :method1, :method2, to: :target● Methods can be delegated to instance
variables, class variables, or constants
Delegate
● Delegate a method to the class by using :class
● delegate :hello, to: :class
Options
● :allow_nil● If the target is nil and does not respond to
the delegated method a NoMethodError is raised
● delegate :name, to: :profile, allow_nil: true● Returns nil
Options
● delegate :name, to: :profile, prefix: true● Delegate methods are prefixed with the
name of the object being delegated to.● delegate :name, to: :client, prefix: :customer● Supply a custom prefix
Thank you