Class: FiForm::Builder

Inherits:
Object
  • Object
show all
Includes:
FiForm::BuilderExtensions::ActiveRecord
Defined in:
lib/fi_form/builder.rb

Overview

PORO o/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FiForm::BuilderExtensions::ActiveRecord

#fields_for

Constructor Details

#initialize(resource, options = {}, template, &block) ⇒ Builder

Returns a new instance of Builder.



6
7
8
9
10
11
12
13
14
15
16
17
# 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

Instance Attribute Details

#formObject (readonly)

Returns the value of attribute form.



3
4
5
# File 'lib/fi_form/builder.rb', line 3

def form
  @form
end

#indentationObject (readonly)

Returns the value of attribute indentation.



3
4
5
# File 'lib/fi_form/builder.rb', line 3

def indentation
  @indentation
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/fi_form/builder.rb', line 3

def options
  @options
end

#resourceObject (readonly) Also known as: object

Returns the value of attribute resource.



3
4
5
# File 'lib/fi_form/builder.rb', line 3

def resource
  @resource
end

#validation_errorsObject

Returns the value of attribute validation_errors.



4
5
6
# File 'lib/fi_form/builder.rb', line 4

def validation_errors
  @validation_errors
end

Class Method Details

.add_builder_method(name, &block) ⇒ Object



74
75
76
# File 'lib/fi_form/builder.rb', line 74

def self.add_builder_method(name, &block)
  define_method name, &block
end

Instance Method Details

#add(kind, options = {}) ⇒ Object



21
22
23
24
# 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) ⇒ Object



70
71
72
# 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) ⇒ Object



26
27
28
29
30
# 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 = {}) ⇒ Object



54
55
56
# File 'lib/fi_form/builder.rb', line 54

def hint(hint, options={})
  add(:hint, options.merge(hint: hint, indentation: indentation))
end

#input(field, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object



47
48
49
50
51
52
# 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 = {}) ⇒ Object



62
63
64
# 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 = {}) ⇒ Object



58
59
60
# File 'lib/fi_form/builder.rb', line 58

def title(title, options={})
  add(:title, options.merge(title: title, indentation: indentation))
end

#to_htmlObject



66
67
68
# File 'lib/fi_form/builder.rb', line 66

def to_html
  FiForm.configuration.get_renderer(@renderer).new(@template).render(@form)
end