Class: PartialForm::Builder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/partial_form/builder.rb

Overview

The builder allows the rendering of form fields similar to rails default ‘FormBuilder`. This Builder uses partials to manage and maintain the form field markup code. The Builder mimics the API of the default [Rails FormBuilder](api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html) with the prefix `_`.

The general idea is that each helper renders a dedicated partial and passes the arguments: ‘f`, `method`, `options`, `errors`.

Please see README.md for more details.

Constant Summary collapse

SIMPLE_FIELD_HELPER =

:nodoc:

[ # :nodoc:
  :color_field, :date_field, :datetime_field, :datetime_local_field, :email_field, :file_field,
  :hidden_field, :month_field, :number_field, :password_field, :phone_field, :range_field,
  :search_field, :telephone_field, :text_field, :text_area, :time_field, :url_field, :week_field
]

Instance Method Summary collapse

Constructor Details

#initializeBuilder

:nodoc:



13
14
15
16
17
18
19
# File 'lib/partial_form/builder.rb', line 13

def initialize(*) # :nodoc:
  super
  # CAUTION
  # we want to make sure that fields are not wrapped by a "field_with_errors" proc
  # this change affects all builders working on the current template
  @template.field_error_proc = proc { |html_tag, instance| html_tag }
end

Instance Method Details

#_button(value = nil, options = {}, &block) ⇒ Object

Renders the partial ‘views/layouts/form/_button`.

The partial template usually wraps [ActionView::Helpers::FormBuilder#button](api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-button).



200
201
202
# File 'lib/partial_form/builder.rb', line 200

def _button(value = nil, options = {}, &block)
  render_partial("button", {f: self, value:, options:, block:})
end

#_check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object

Renders the partial ‘views/layouts/form/_check_box`.

The partial template usually wraps [ActionView::Helpers::FormBuilder#check_box](api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-check_box).



216
217
218
219
# File 'lib/partial_form/builder.rb', line 216

def _check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  errors = object.errors.messages_for(method)
  render_partial("check_box", {f: self, method:, options:, checked_value:, unchecked_value:, errors:})
end

#_collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object

Renders the partial ‘views/layouts/form/_collection_check_boxes`.

The partial template usually wraps [ActionView::Helpers::FormBuilder#collection_check_boxes](api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-collection_check_boxes).



279
280
281
282
# File 'lib/partial_form/builder.rb', line 279

def _collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
  errors = object.errors.messages_for(method)
  render_partial("collection_check_boxes", {f: self, method:, collection:, value_method:, text_method:, options:, html_options:, block:, errors:})
end

#_collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object

Renders the partial ‘views/layouts/form/_collection_radio_buttons`.

The partial template usually wraps [ActionView::Helpers::FormBuilder#collection_radio_buttons](api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-collection_radio_buttons).



288
289
290
291
# File 'lib/partial_form/builder.rb', line 288

def _collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
  errors = object.errors.messages_for(method)
  render_partial("collection_radio_buttons", {f: self, method:, collection:, value_method:, text_method:, options:, html_options:, block:, errors:})
end

#_collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object

Renders the partial ‘views/layouts/form/_collection_select`.

The partial template usually wraps [ActionView::Helpers::FormBuilder#collection_select](api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-collection_select).



243
244
245
246
# File 'lib/partial_form/builder.rb', line 243

def _collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
  errors = object.errors.messages_for(method)
  render_partial("collection_select", {f: self, method:, collection:, value_method:, text_method:, options:, html_options:, errors:})
end

#_grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object

Renders the partial ‘views/layouts/form/_grouped_collection_select`.

The partial template usually wraps [ActionView::Helpers::FormBuilder#grouped_collection_select](api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-grouped_collection_select).



252
253
254
255
# File 'lib/partial_form/builder.rb', line 252

def _grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
  errors = object.errors.messages_for(method)
  render_partial("grouped_collection_select", {f: self, method:, collection:, group_method:, group_label_method:, option_key_method:, option_value_method:, options:, html_options:, errors:})
end

#_label(method, text = nil, options = {}, &block) ⇒ Object

Renders the partial ‘views/layouts/form/_label`.

The partial template usually wraps [ActionView::Helpers::FormBuilder#label](api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-label).



191
192
193
194
# File 'lib/partial_form/builder.rb', line 191

def _label(method, text = nil, options = {}, &block)
  errors = object.errors.messages_for(method)
  render_partial("label", {f: self, method:, text:, errors:, options:, block:})
end

#_radio_button(method, tag_value, options = {}) ⇒ Object

Renders the partial ‘views/layouts/form/_radio_button`.

The partial template usually wraps [ActionView::Helpers::FormBuilder#radio_button](api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-radio_button).



225
226
227
228
# File 'lib/partial_form/builder.rb', line 225

def _radio_button(method, tag_value, options = {})
  errors = object.errors.messages_for(method)
  render_partial("radio_button", {f: self, method:, tag_value:, options:, errors:})
end

#_select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object

Renders the partial ‘views/layouts/form/_select`.

The partial template usually wraps [ActionView::Helpers::FormBuilder#select](api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-select).



234
235
236
237
# File 'lib/partial_form/builder.rb', line 234

def _select(method, choices = nil, options = {}, html_options = {}, &block)
  errors = object.errors.messages_for(method)
  render_partial("select", {f: self, method:, choices:, options:, html_options:, block:, errors:})
end

#_submit(value = nil, options = {}) ⇒ Object

Renders the partial ‘views/layouts/form/_submit`.

The partial template usually wraps [ActionView::Helpers::FormBuilder#submit](api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-submit).



208
209
210
# File 'lib/partial_form/builder.rb', line 208

def _submit(value = nil, options = {})
  render_partial("submit", {f: self, value:, options:})
end

#_time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object

Renders the partial ‘views/layouts/form/_time_zone_select`.

The partial template usually wraps [ActionView::Helpers::FormBuilder#time_zone_select](api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-time_zone_select).



261
262
263
264
# File 'lib/partial_form/builder.rb', line 261

def _time_zone_select(method, priority_zones = nil, options = {}, html_options = {})
  errors = object.errors.messages_for(method)
  render_partial("time_zone_select", {f: self, method:, priority_zones:, options:, html_options:, errors:})
end

#_weekday_select(method, options = {}, html_options = {}) ⇒ Object

Renders the partial ‘views/layouts/form/_weekday_select`.

The partial template usually wraps [ActionView::Helpers::FormBuilder#weekday_select](api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-weekday_select).



270
271
272
273
# File 'lib/partial_form/builder.rb', line 270

def _weekday_select(method, options = {}, html_options = {})
  errors = object.errors.messages_for(method)
  render_partial("weekday_select", {f: self, method:, options:, html_options:, errors:})
end

#method_nameObject

:method: _week_field :call-seq: _week_field(method, options = {})

Renders the partial ‘views/layouts/form/_week_field`.

Please see #_text_field for more details. The partial template usually wraps [ActionView::Helpers::FormBuilder#week_field](api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-week_field).



181
182
183
184
185
# File 'lib/partial_form/builder.rb', line 181

SIMPLE_FIELD_HELPER.each do |method_name|
  define_method :"_#{method_name}" do |method, options = {}|
    render_simple_field(__method__, method, options)
  end
end