Class: BentoSearch::DecoratorBase

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
app/item_decorators/bento_search/decorator_base.rb

Overview

A delegator with an ActionView context. You can access the ActionView context at _h , to call Rails helper methods (framework or app specific, whatever should be avail at given context)

inside a method in a decorator, ‘_h.content_tag` or `_h.html_escape` or `_h.url_for` etc.

(Except you can't call html_escape that way becuase Rails makes it
private for some reason, wtf. We provide an html_escape)

Inside a decorator, access #_base to get undecorated base model.

Direct Known Subclasses

StandardDecorator

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, view_context) ⇒ DecoratorBase

Returns a new instance of DecoratorBase.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/item_decorators/bento_search/decorator_base.rb', line 18

def initialize(base, view_context)
  super(base)
  
  
  # This worked to make html_escape avail at _h.html_escape, but
  # yfeldblum warned it messes up the method lookup cache, so
  # we just provide a straight #html_escape instead.  
  #if view_context.respond_to?(:html_escape, true)
    # thanks yfeldblum in #rails for this simple way to make
    # html_escape public, which I don't entirely understand myself. :)
   
  #  class << view_context
  #    public :html_escape
  #  end        
  #end      
  
  @view_context = view_context
  @base = base
end

Class Method Details

.decorate(item, view_context) ⇒ Object

Applies decorator to item and returns decorated item. Will decide what decorator to apply based on String class name in item.decorator, or else apply StandardDecorator. The point of this method is just that logic, nothing else special.

Need to pass a Rails ActionView::Context in, to use to initialize decorator. In Rails, in most places you can get one of those from #view_context. In helpers/views you can also use ‘self`.



61
62
63
64
65
66
67
68
69
70
# File 'app/item_decorators/bento_search/decorator_base.rb', line 61

def self.decorate(item, view_context)
  # What decorator class? Specified in #decorator as a String,
  # we intentionally do not allow an actual class constant, to
  # maintain problem-free serialization of ItemResults and configuration. 
  decorator_class = item.decorator.try do |arg|
    BentoSearch::Util.constantize(arg.to_s)           
  end || BentoSearch::StandardDecorator
  
  return decorator_class.new(item, view_context)    
end

Instance Method Details

#_baseObject



42
43
44
# File 'app/item_decorators/bento_search/decorator_base.rb', line 42

def _base
  @base
end

#_hObject



38
39
40
# File 'app/item_decorators/bento_search/decorator_base.rb', line 38

def _h
  @view_context
end

#html_escape(*args, &block) ⇒ Object

_h.html_escape won’t work because Rails makes html_escape private for some weird reason. We provide our own here instead.



48
49
50
# File 'app/item_decorators/bento_search/decorator_base.rb', line 48

def html_escape(*args, &block)
  ERB::Util.html_escape(*args, &block)
end