Class: ActionView::BlockHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/block_helpers.rb

Class Method Summary collapse

Class Method Details

.inherited(klass) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/block_helpers.rb', line 8

def self.inherited(klass)
  # Define the helper method
  # e.g. for a class:
  #   class HelloHelper < ActionView::BlockHelper
  #     #.....
  #   end
  #
  # then we define a helper method 'hello_helper'
  #
  method_name = klass.name.split('::').last.underscore
  klass.parent.class_eval %(      
    def #{method_name}(*args, &block)
      renderer = #{klass.name}.new(*args)
      if renderer.public_methods(false).include? 'display'
        concat renderer.display(capture(renderer, &block))
      else
        block.call(renderer)
      end
    end
  )
  
  # Make a 'helper' object available, for calling
  # other helper methods / action view helpers,
  # in case of name clashes
  klass.class_eval do
    
    include klass.parent
    include ActionView::Helpers
    
    protected
    define_method :helper do
      if @helper.nil?
        @helper = Object.new
        # Open the singleton class of @helper, while
        # keeping 'klass' visible
        class << @helper; self; end.class_eval do
          include klass.parent
          include ActionView::Helpers
        end
      end
      @helper
    end

  end
end