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, :turbo_frame_tag]
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.



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

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



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

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



37
38
39
40
41
42
43
44
# File 'lib/trestle/form/renderer.rb', line 37

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



33
34
35
# File 'lib/trestle/form/renderer.rb', line 33

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

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

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/trestle/form/renderer.rb', line 65

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