Class: Trestle::Form::Renderer

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers::CaptureHelper
Defined in:
lib/trestle/form/renderer.rb

Constant Summary collapse

WHITELISTED_HELPERS =

Whitelisted helpers will concatenate their result to the output buffer when called.

[:row, :col, :render, :tab, :toolbar, :table, :divider, :h1, :h2, :h3, :h4, :h5, :h6, :panel, :well]
RAW_BLOCK_HELPERS =

Raw block helpers will pass their block argument directly to the method without wrapping it in a new output buffer.

[:table]

Instance Method Summary collapse

Constructor Details

#initialize(template, form = nil) ⇒ Renderer

Returns a new instance of Renderer.



20
21
22
23
# File 'lib/trestle/form/renderer.rb', line 20

def initialize(template, form=nil)
  @template = template
  @form = form || @template.form
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/trestle/form/renderer.rb', line 38

def method_missing(name, *args, &block)
  target = @form.respond_to?(name) ? @form : @template

  if block_given? && !RAW_BLOCK_HELPERS.include?(name)
    result = target.send(name, *args) do |*blockargs|
      render_form(*blockargs, &block)
    end
  else
    result = target.send(name, *args, &block)
  end

  if target == @form || WHITELISTED_HELPERS.include?(name)
    concat(result)
  else
    result
  end
end

Instance Method Details

#fields_for(*args, &block) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/trestle/form/renderer.rb', line 29

def fields_for(*args, &block)
  result = @form.fields_for(*args) do |f|
    renderer = self.class.new(@template, f)
    renderer.render_form(f, &block)
  end

  concat(result)
end

#render_form(*args, &block) ⇒ Object



25
26
27
# File 'lib/trestle/form/renderer.rb', line 25

def render_form(*args, &block)
  capture { instance_exec(*args, &block).to_s }
end

#respond_to_missing?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/trestle/form/renderer.rb', line 56

def respond_to_missing?(name, include_all=false)
  @form.respond_to?(name, include_all) ||
    @template.respond_to?(name, include_all) ||
    super
end