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.
What does the
delegate
line actually mean? Does it mean that when I try to access thetitle
,name
,region
,keys
attributes/methods, they will be delegated to theGlassDecorator
? What does theprefix:
part mean?For the
full_title
method, Does theglass_title
part try to lookup thetitle
attribute/method in theGlassDecorator
? If that is the case, is it made possible only because of thedelegate
line? If so, is it the:prefix
portion that makes it possible?
Thank you.
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 callsglass_title
on yourPageDecorator
, it will then go and calltitle
on theGlassDecorator
and hand you the result. ie - it delegates that method to the object named in:to
2) yes. You have understood that correctly