Class: VCFB::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- VCFB::FormBuilder
- Defined in:
- lib/vcfb/form_builder.rb
Constant Summary collapse
- SIMPLE_FIELDS =
SIMPLE FIELDS
%i[color_field date_field datetime_field email_field month_field number_field password_field range_field search_field telephone_field text_area text_field time_field url_field week_field]
- SIMPLE_SELECTS =
SIMPLE SELECTS
%i[date_select datetime_select time_select]
Instance Attribute Summary collapse
-
#form_component ⇒ Object
Returns the value of attribute form_component.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#object_name ⇒ Object
readonly
Returns the value of attribute object_name.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Class Method Summary collapse
-
.form_component_class ⇒ Object
FORM COMPONENT.
- .form_component_defined? ⇒ Boolean
- .resolve_component_class(form_element = nil) ⇒ Object
Instance Method Summary collapse
-
#button(value = nil, options = {}, &block) ⇒ Object
The default #button method captures the block (it never makes it to the underlying button_tag helper).
-
#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
MORE COMPLEX FIELDS AND SELECTS.
- #collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object
- #collection_check_boxes_check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
- #collection_check_boxes_label(method, text = nil, options = {}, &block) ⇒ Object
- #collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object
- #collection_radio_buttons_label(method, text = nil, options = {}, &block) ⇒ Object
- #collection_radio_buttons_radio_button(method, value, options = {}) ⇒ Object
- #collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
-
#component_defined?(form_element) ⇒ Boolean
COMPONENT SUPPORT.
- #componentify(form_element, *args, **opts, &block) ⇒ Object
- #componentify_with_slots(form_element, *args, **opts, &block) ⇒ Object
- #file_field(method, options = {}) ⇒ Object
- #grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object
-
#initialize(*args) ⇒ FormBuilder
constructor
A new instance of FormBuilder.
- #label(method, text = nil, options = {}, &block) ⇒ Object
-
#method_missing(method_name, *args, **opts, &block) ⇒ Object
SUPPORT FORM COMPONENT SLOTS.
- #radio_button(method, value, options = {}) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #rich_text_area(method, options = {}) ⇒ Object
- #select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object
- #time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object
- #weekday_select(method, options = {}, html_options = {}) ⇒ Object
Constructor Details
#initialize(*args) ⇒ FormBuilder
Returns a new instance of FormBuilder.
10 11 12 13 |
# File 'lib/vcfb/form_builder.rb', line 10 def initialize(*args) super @template = Template.new(@template, self) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, **opts, &block) ⇒ Object
SUPPORT FORM COMPONENT SLOTS
215 216 217 218 219 |
# File 'lib/vcfb/form_builder.rb', line 215 def method_missing(method_name, *args, **opts, &block) super unless method_name.to_s.start_with?("with_") form_component.public_send(method_name, *args, **opts, &block) end |
Instance Attribute Details
#form_component ⇒ Object
Returns the value of attribute form_component.
8 9 10 |
# File 'lib/vcfb/form_builder.rb', line 8 def form_component @form_component end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
7 8 9 |
# File 'lib/vcfb/form_builder.rb', line 7 def object @object end |
#object_name ⇒ Object (readonly)
Returns the value of attribute object_name.
7 8 9 |
# File 'lib/vcfb/form_builder.rb', line 7 def object_name @object_name end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
7 8 9 |
# File 'lib/vcfb/form_builder.rb', line 7 def template @template end |
Class Method Details
.form_component_class ⇒ Object
FORM COMPONENT
17 18 19 |
# File 'lib/vcfb/form_builder.rb', line 17 def self.form_component_class resolve_component_class end |
.form_component_defined? ⇒ Boolean
21 22 23 24 25 |
# File 'lib/vcfb/form_builder.rb', line 21 def self.form_component_defined? form_component_class.present? rescue Errors::ComponentMissing false end |
Instance Method Details
#button(value = nil, options = {}, &block) ⇒ Object
The default #button method captures the block (it never makes it to the underlying button_tag helper). Stash it here into options so that we can pull it out later (see vcfb/template) and pass it on to the component so that we can support slots on button components.
188 189 190 191 |
# File 'lib/vcfb/form_builder.rb', line 188 def (value = nil, = {}, &block) [:_block_for_component] = block super(value, , &nil) end |
#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
MORE COMPLEX FIELDS AND SELECTS
64 65 66 67 68 |
# File 'lib/vcfb/form_builder.rb', line 64 def check_box(method, = {}, checked_value = "1", unchecked_value = "0") return super unless component_defined?(:check_box) componentify(:check_box, method, (), checked_value, unchecked_value) end |
#collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object
70 71 72 73 74 |
# File 'lib/vcfb/form_builder.rb', line 70 def collection_check_boxes(method, collection, value_method, text_method, = {}, = {}, &block) return super unless component_defined?(:collection_check_boxes) componentify(:collection_check_boxes, method, collection, value_method, text_method, (), @default_html_options.merge(), &block) end |
#collection_check_boxes_check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/vcfb/form_builder.rb', line 87 def collection_check_boxes_check_box(method, = {}, checked_value = "1", unchecked_value = "0") if component_defined?(:"collection_check_boxes/check_box") componentify(:"collection_check_boxes/check_box", method, , checked_value, unchecked_value) elsif component_defined?(:check_box) componentify(:check_box, method, , checked_value, unchecked_value) else check_box(method, , checked_value, unchecked_value) end end |
#collection_check_boxes_label(method, text = nil, options = {}, &block) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/vcfb/form_builder.rb', line 76 def collection_check_boxes_label(method, text = nil, = {}, &block) , text = text, nil if text.is_a?(Hash) if component_defined?(:"collection_check_boxes/label") componentify(:"collection_check_boxes/label", method, text, , &block) elsif component_defined?(:label) componentify(:label, method, text, , &block) else label(method, text, , &block) end end |
#collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object
97 98 99 100 101 |
# File 'lib/vcfb/form_builder.rb', line 97 def (method, collection, value_method, text_method, = {}, = {}, &block) return super unless component_defined?(:collection_radio_buttons) componentify(:collection_radio_buttons, method, collection, value_method, text_method, (), @default_html_options.merge(), &block) end |
#collection_radio_buttons_label(method, text = nil, options = {}, &block) ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/vcfb/form_builder.rb', line 103 def (method, text = nil, = {}, &block) , text = text, nil if text.is_a?(Hash) if component_defined?(:"collection_radio_buttons/label") componentify_with_slots(:"collection_radio_buttons/label", method, text, , &block) elsif component_defined?(:label) componentify_with_slots(:label, method, text, , &block) else label(method, text, , &block) end end |
#collection_radio_buttons_radio_button(method, value, options = {}) ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/vcfb/form_builder.rb', line 114 def (method, value, = {}) if component_defined?(:"collection_radio_buttons/radio_button") componentify(:"collection_radio_buttons/radio_button", method, value, ) elsif component_defined?(:radio_button) componentify(:radio_button, method, value, ) else (method, , checked_value, unchecked_value) end end |
#collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
124 125 126 127 128 |
# File 'lib/vcfb/form_builder.rb', line 124 def collection_select(method, collection, value_method, text_method, = {}, = {}) return super unless component_defined?(:collection_select) componentify(:collection_select, method, collection, value_method, text_method, (), @default_html_options.merge()) end |
#component_defined?(form_element) ⇒ Boolean
COMPONENT SUPPORT
195 196 197 198 199 |
# File 'lib/vcfb/form_builder.rb', line 195 def component_defined?(form_element) resolve_component_class(form_element).present? rescue Errors::ComponentMissing false end |
#componentify(form_element, *args, **opts, &block) ⇒ Object
201 202 203 204 |
# File 'lib/vcfb/form_builder.rb', line 201 def componentify(form_element, *args, **opts, &block) component_class = resolve_component_class(form_element) @template.render component_class.new(self, *args, **opts), &block end |
#componentify_with_slots(form_element, *args, **opts, &block) ⇒ Object
206 207 208 209 210 211 |
# File 'lib/vcfb/form_builder.rb', line 206 def componentify_with_slots(form_element, *args, **opts, &block) component_class = resolve_component_class(form_element) @template.render component_class.new(self, *args, **opts) do |component| block&.call(component) end end |
#file_field(method, options = {}) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/vcfb/form_builder.rb', line 130 def file_field(method, = {}) return super unless component_defined?(:file_field) self.multipart = true componentify(:file_field, method, ()) end |
#grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object
137 138 139 140 141 |
# File 'lib/vcfb/form_builder.rb', line 137 def grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, = {}, = {}) return super unless component_defined?(:grouped_collection_select) componentify(:grouped_collection_select, method, collection, group_method, group_label_method, option_key_method, option_value_method, (), @default_html_options.merge()) end |
#label(method, text = nil, options = {}, &block) ⇒ Object
143 144 145 146 147 148 |
# File 'lib/vcfb/form_builder.rb', line 143 def label(method, text = nil, = {}, &block) return super unless component_defined?(:label) , text = text, nil if text.is_a?(Hash) componentify_with_slots(:label, method, text, (), &block) end |
#radio_button(method, value, options = {}) ⇒ Object
150 151 152 153 154 |
# File 'lib/vcfb/form_builder.rb', line 150 def (method, value, = {}) return super unless component_defined?(:radio_button) componentify(:radio_button, method, value, ()) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
221 222 223 224 225 |
# File 'lib/vcfb/form_builder.rb', line 221 def respond_to_missing?(method_name, include_private = false) super unless method_name.to_s.start_with?("with_") form_component.respond_to?(method_name, include_private) end |
#rich_text_area(method, options = {}) ⇒ Object
156 157 158 159 160 |
# File 'lib/vcfb/form_builder.rb', line 156 def rich_text_area(method, = {}) return super unless component_defined?(:rich_text_area) componentify(:rich_text_area, method, ()) end |
#select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object
162 163 164 165 166 |
# File 'lib/vcfb/form_builder.rb', line 162 def select(method, choices = nil, = {}, = {}, &block) return super unless component_defined?(:select) componentify(:select, method, choices, (), @default_html_options.merge(), &block) end |
#time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object
168 169 170 171 172 |
# File 'lib/vcfb/form_builder.rb', line 168 def time_zone_select(method, priority_zones = nil, = {}, = {}) return super unless component_defined?(:time_zone_select) componentify(:time_zone_select, method, priority_zones, (), @default_html_options.merge()) end |
#weekday_select(method, options = {}, html_options = {}) ⇒ Object
175 176 177 178 179 |
# File 'lib/vcfb/form_builder.rb', line 175 def weekday_select(method, = {}, = {}) return super unless component_defined?(:weekday_select) componentify(:weekday_select, method, (), ) end |