Sha256: 5f8c0a9b3112e72449b5f03902822907362dc22e490f6f1324a1eb63222ea58e

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

require 'delegate'

module BentoSearch
  # 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. 
  class DecoratorBase < SimpleDelegator
    
    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
    
    def _h
      @view_context
    end    
    
    def _base
      @base
    end
    
    # _h.html_escape won't work because Rails makes html_escape
    # private for some weird reason. We provide our own here instead. 
    def html_escape(*args, &block)
      ERB::Util.html_escape(*args, &block)
    end
    
  end  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bento_search-0.8.0 app/item_decorators/bento_search/decorator_base.rb
bento_search-0.7.0 app/item_decorators/bento_search/decorator_base.rb