Class: VCFB::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_componentObject

Returns the value of attribute form_component.



8
9
10
# File 'lib/vcfb/form_builder.rb', line 8

def form_component
  @form_component
end

#objectObject (readonly)

Returns the value of attribute object.



7
8
9
# File 'lib/vcfb/form_builder.rb', line 7

def object
  @object
end

#object_nameObject (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

#templateObject (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_classObject

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

Returns:

  • (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

.resolve_component_class(form_element = nil) ⇒ Object



27
28
29
# File 'lib/vcfb/form_builder.rb', line 27

def self.resolve_component_class(form_element = nil)
  VCFB::Resolver.call(namespace, form_element)
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 button(value = nil, options = {}, &block)
  options[:_block_for_component] = block
  super(value, options, &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, options = {}, checked_value = "1", unchecked_value = "0")
  return super unless component_defined?(:check_box)

  componentify(:check_box, method, objectify_options(options), 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, options = {}, html_options = {}, &block)
  return super unless component_defined?(:collection_check_boxes)

  componentify(:collection_check_boxes, method, collection, value_method, text_method, objectify_options(options), @default_html_options.merge(html_options), &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, options = {}, checked_value = "1", unchecked_value = "0")
  if component_defined?(:"collection_check_boxes/check_box")
    componentify(:"collection_check_boxes/check_box", method, options, checked_value, unchecked_value)
  elsif component_defined?(:check_box)
    componentify(:check_box, method, options, checked_value, unchecked_value)
  else
    check_box(method, options, 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, options = {}, &block)
  options, text = text, nil if text.is_a?(Hash)
  if component_defined?(:"collection_check_boxes/label")
    componentify(:"collection_check_boxes/label", method, text, options, &block)
  elsif component_defined?(:label)
    componentify(:label, method, text, options, &block)
  else
    label(method, text, options, &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 collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
  return super unless component_defined?(:collection_radio_buttons)

  componentify(:collection_radio_buttons, method, collection, value_method, text_method, objectify_options(options), @default_html_options.merge(html_options), &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 collection_radio_buttons_label(method, text = nil, options = {}, &block)
  options, text = text, nil if text.is_a?(Hash)
  if component_defined?(:"collection_radio_buttons/label")
    componentify_with_slots(:"collection_radio_buttons/label", method, text, options, &block)
  elsif component_defined?(:label)
    componentify_with_slots(:label, method, text, options, &block)
  else
    label(method, text, options, &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 collection_radio_buttons_radio_button(method, value, options = {})
  if component_defined?(:"collection_radio_buttons/radio_button")
    componentify(:"collection_radio_buttons/radio_button", method, value, options)
  elsif component_defined?(:radio_button)
    componentify(:radio_button, method, value, options)
  else
    radio_button(method, options, 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, options = {}, html_options = {})
  return super unless component_defined?(:collection_select)

  componentify(:collection_select, method, collection, value_method, text_method, objectify_options(options), @default_html_options.merge(html_options))
end

#component_defined?(form_element) ⇒ Boolean

COMPONENT SUPPORT

Returns:

  • (Boolean)


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, options = {})
  return super unless component_defined?(:file_field)

  self.multipart = true
  componentify(:file_field, method, objectify_options(options))
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, options = {}, html_options = {})
  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, objectify_options(options), @default_html_options.merge(html_options))
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, options = {}, &block)
  return super unless component_defined?(:label)

  options, text = text, nil if text.is_a?(Hash)
  componentify_with_slots(:label, method, text, objectify_options(options), &block)
end

#radio_button(method, value, options = {}) ⇒ Object



150
151
152
153
154
# File 'lib/vcfb/form_builder.rb', line 150

def radio_button(method, value, options = {})
  return super unless component_defined?(:radio_button)

  componentify(:radio_button, method, value, objectify_options(options))
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (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, options = {})
  return super unless component_defined?(:rich_text_area)

  componentify(:rich_text_area, method, objectify_options(options))
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, options = {}, html_options = {}, &block)
  return super unless component_defined?(:select)

  componentify(:select, method, choices, objectify_options(options), @default_html_options.merge(html_options), &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, options = {}, html_options = {})
  return super unless component_defined?(:time_zone_select)

  componentify(:time_zone_select, method, priority_zones, objectify_options(options), @default_html_options.merge(html_options))
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, options = {}, html_options = {})
  return super unless component_defined?(:weekday_select)

  componentify(:weekday_select, method, objectify_options(options), html_options)
end