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, :hook, :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

Constructor Details

#initialize(template, form = nil) ⇒ Renderer

Returns a new instance of Renderer.


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

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


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

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


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

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


28
29
30
# File 'lib/trestle/form/renderer.rb', line 28

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

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

Returns:

  • (Boolean)

59
60
61
62
63
# File 'lib/trestle/form/renderer.rb', line 59

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