draper - Help understanding delegate with "to" and "prefix"

992 views Asked by At

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.

1

There are 1 answers

3
Taryn East On BEST ANSWER

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