class FiForm::Builder

Attributes

form[R]
indentation[R]
object[R]
options[R]
resource[R]
validation_errors[RW]

Public Class Methods

add_builder_method(name, &block) click to toggle source
# File lib/fi_form/builder.rb, line 74
def self.add_builder_method(name, &block)
  define_method name, &block
end
new(resource, options={}, template, &block) click to toggle source
# File lib/fi_form/builder.rb, line 6
def initialize(resource, options={}, template, &block)
  @resource = resource
  @template = template
  @options = options.dup

  @validation_errors = {}

  @form = build_form
  @target = @form
  @indentation = options[:indentation] || 0
  @renderer = options.delete(:renderer)
end

Public Instance Methods

add(kind, options={}) click to toggle source
# File lib/fi_form/builder.rb, line 21
def add(kind, options={})
  @target << build(kind, options)
  self # allow chaining
end
add_children(indent: true, &block) click to toggle source
# File lib/fi_form/builder.rb, line 70
def add_children(indent: true, &block)
  with_target last_item, indent: indent, &block
end
group(title=nil, &block) click to toggle source
# File lib/fi_form/builder.rb, line 26
def group(title=nil, &block)
  add(:group, title: title, indentation: indentation).add_children do
    @template.capture(self, &block)
  end
end
hint(hint, options={}) click to toggle source
# File lib/fi_form/builder.rb, line 54
def hint(hint, options={})
  add(:hint, options.merge(hint: hint, indentation: indentation))
end
input(field, options={}) click to toggle source
# File lib/fi_form/builder.rb, line 32
def input(field, options={})
  input_options = {
    value: options[:value] || resource_value(field),
    field: field,
    label: options[:label],
  }

  input_options[:hint] = options[:hint] if options[:hint]

  html_options = options.except(:as, :label, :hint, :value)
  input_options[:input] = html_options if html_options.any?

  add(:input, input_options)
end
render(*args) click to toggle source
# File lib/fi_form/builder.rb, line 47
def render(*args)
  content = @template.capture(self) do
    @template.render *args
  end
  add :raw, content: content
end
submit(label=nil, options={}) click to toggle source
# File lib/fi_form/builder.rb, line 62
def submit(label=nil, options={})
  add(:submit, options.merge(label: label, indentation: indentation))
end
title(title, options={}) click to toggle source
# File lib/fi_form/builder.rb, line 58
def title(title, options={})
  add(:title, options.merge(title: title, indentation: indentation))
end
to_html() click to toggle source
# File lib/fi_form/builder.rb, line 66
def to_html
  FiForm.configuration.get_renderer(@renderer).new(@template).render(@form)
end

Private Instance Methods

build(kind, options) click to toggle source
# File lib/fi_form/builder.rb, line 82
def build(kind, options)
  FiForm::Item.build(kind, form: @form, **options)
end
build_form() click to toggle source
# File lib/fi_form/builder.rb, line 114
def build_form
  FiForm::Item.build(:form,
    resource: @resource,
    errors: validation_errors,
    form_options: @options
  )
end
capture_target(options={}, &block) click to toggle source
# File lib/fi_form/builder.rb, line 103
def capture_target(options={}, &block)
  indent = !!options.delete(:indent)

  target_list = []
  with_target(target_list, indent: indent, &block)

  single_target = target_list.first
  single_target.options.merge! options
  single_target
end
last_item() click to toggle source
# File lib/fi_form/builder.rb, line 90
def last_item
  @target.last_child
end
resource_value(field) click to toggle source
# File lib/fi_form/builder.rb, line 79
def resource_value(field)
end
sub_builder(resource, &block) click to toggle source
# File lib/fi_form/builder.rb, line 86
def sub_builder(resource, &block)
  FiForm::Builder.new(resource, @options.merge(indentation: indentation), @template, &block)
end
with_target(target_list, indent: true) { || ... } click to toggle source
# File lib/fi_form/builder.rb, line 94
def with_target(target_list, indent: true, &block)
  previous_target = @target
  @indentation += 1 if indent
  @target = target_list
  yield
  @indentation -= 1 if indent
  @target = previous_target
end