2

Given that I have a class that inherits from Draper::Decorator, like this:

class PageDecorator < Draper::Decorator
  delegate_all

  delegate :title, :name, :region, :keys,
       to: :glass_decorator,
       prefix: :glass

  def full_title
    full_title = []
    full_title << glass_title if glass_title
    full_title.join(" ")
  end

There is a decorator called GlassDecorator in another file in the same directory.

  1. What does the delegate line actually mean? Does it mean that when I try to access the title, name, region, keys attributes/methods, they will be delegated to the GlassDecorator ? What does the prefix: part mean?

  2. For the full_title method, Does the glass_title part try to lookup the title attribute/method in the GlassDecorator? If that is the case, is it made possible only because of the delegate line? If so, is it the :prefix portion that makes it possible?

Thank you.

yanhan
  • 3,507
  • 3
  • 28
  • 38

1 Answers1

2

1) :prefix will add the prefix to the front of the method-name. eg "glass_title" instead of just "title" delegate means that if somebody calls glass_title on your PageDecorator, it will then go and call title on the GlassDecorator and hand you the result. ie - it delegates that method to the object named in :to

2) yes. You have understood that correctly

Taryn East
  • 27,486
  • 9
  • 86
  • 108
  • Hmm, am I right to say that if I keep everything in the code the same and omit the `prefix:` portion and I try to access `title`, it will be first be looked up in `PageDecorator`, then if it does not exist, `title` will be looked up in `GlassDecorator`, and if that fails, in the decorated object? – yanhan Dec 19 '13 at 03:15
  • I don't think so. If you have "delegate" it won't look in your current model. If you have a method called "title" on PageDecorator, it will overload the delegated one. – Taryn East Dec 19 '13 at 03:18
  • You'd have to write one that checked the local value, then tried the delegated one... and name them differently. – Taryn East Dec 19 '13 at 03:20