Class: Trestle::Form::Renderer

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers::CaptureHelper, Hook::Helpers
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, :table, :divider, :h1, :h2, :h3, :h4, :h5, :h6, :card, :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, :toolbar]

Instance Method Summary collapse

Methods included from Hook::Helpers

#hook, #hook?

Constructor Details

#initialize(template, form = nil) ⇒ Renderer

Returns a new instance of Renderer.



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

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/trestle/form/renderer.rb', line 43

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



34
35
36
37
38
39
40
41
# File 'lib/trestle/form/renderer.rb', line 34

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



30
31
32
# File 'lib/trestle/form/renderer.rb', line 30

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

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

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/trestle/form/renderer.rb', line 61

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