Class: FiForm::Builder
- Inherits:
-
Object
- Object
- FiForm::Builder
- Includes:
- FiForm::BuilderExtensions::ActiveRecord
- Defined in:
- lib/fi_form/builder.rb
Overview
PORO o/
Instance Attribute Summary collapse
-
#form ⇒ Object
readonly
Returns the value of attribute form.
-
#indentation ⇒ Object
readonly
Returns the value of attribute indentation.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#resource ⇒ Object
(also: #object)
readonly
Returns the value of attribute resource.
-
#validation_errors ⇒ Object
Returns the value of attribute validation_errors.
Class Method Summary collapse
Instance Method Summary collapse
- #add(kind, options = {}) ⇒ Object
- #add_children(indent: true, &block) ⇒ Object
- #group(title = nil, &block) ⇒ Object
- #hint(hint, options = {}) ⇒ Object
-
#initialize(resource, options = {}, template, &block) ⇒ Builder
constructor
A new instance of Builder.
- #input(field, options = {}) ⇒ Object
- #render(*args) ⇒ Object
- #submit(label = nil, options = {}) ⇒ Object
- #title(title, options = {}) ⇒ Object
- #to_html ⇒ Object
Methods included from FiForm::BuilderExtensions::ActiveRecord
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, ={}, template, &block) @resource = resource @template = template = .dup @validation_errors = {} @form = build_form @target = @form @indentation = [:indentation] || 0 @renderer = .delete(:renderer) end |
Instance Attribute Details
#form ⇒ Object (readonly)
Returns the value of attribute form.
3 4 5 |
# File 'lib/fi_form/builder.rb', line 3 def form @form end |
#indentation ⇒ Object (readonly)
Returns the value of attribute indentation.
3 4 5 |
# File 'lib/fi_form/builder.rb', line 3 def indentation @indentation end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/fi_form/builder.rb', line 3 def end |
#resource ⇒ Object (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_errors ⇒ Object
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, ={}) @target << build(kind, ) 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, ={}) add(:hint, .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, ={}) = { value: [:value] || resource_value(field), field: field, label: [:label], } [:hint] = [:hint] if [:hint] = .except(:as, :label, :hint, :value) [:input] = if .any? add(:input, ) 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, ={}) add(:submit, .merge(label: label, indentation: indentation)) end |
#title(title, options = {}) ⇒ Object
58 59 60 |
# File 'lib/fi_form/builder.rb', line 58 def title(title, ={}) add(:title, .merge(title: title, indentation: indentation)) end |
#to_html ⇒ Object
66 67 68 |
# File 'lib/fi_form/builder.rb', line 66 def to_html FiForm.configuration.get_renderer(@renderer).new(@template).render(@form) end |