Class: Bh::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
Adornments, Buttons, Comboboxes, Controls, Pins
Defined in:
lib/bh/form_builder.rb,
lib/bh/form_builder/pins.rb,
lib/bh/form_builder/buttons.rb,
lib/bh/form_builder/controls.rb,
lib/bh/form_builder/adornments.rb,
lib/bh/form_builder/comboboxes.rb

Overview

The form: what a label, a field and a button of one wear, and the field Rails has none of. Given to one form as builder:, or to every form of a controller through default_form_builder. What a view passes is kept: its classes join the builder's, its data is merged key by key with the builder's, and any other option of its overrides.

Defined Under Namespace

Modules: Adornments, Buttons, Comboboxes, Controls, Pins

Constant Summary collapse

CONTROL =

What a field wears: Bootstrap's control, at the size a thumb finds.

'form-control form-control-lg'
PHONE =

What a phone field carries: the bundle's phone controller, which shapes the number as it is typed, and the keyboard and autofill a phone offers for one.

{
  autocomplete: 'tel',
  data: { controller: 'phone', action: 'keydown->phone#down input->phone#input' },
}.freeze

Constants included from Adornments

Adornments::ADORN, Adornments::PRICE

Constants included from Buttons

Buttons::PILL, Buttons::SPACE

Constants included from Comboboxes

Comboboxes::WORDS

Constants included from Controls

Controls::CHECK, Controls::RADIO

Constants included from Pins

Pins::PIN

Instance Method Summary collapse

Methods included from Adornments

#percentage_field, #price_field

Methods included from Buttons

#button, #submit

Methods included from Comboboxes

#combobox

Methods included from Controls

#check, #check_box, #date_field, #datetime_field, #email_field, #number_field, #password_field, #radio_button, #search_field, #select, #text_area, #time_field, #url_field

Methods included from Pins

#pin_field

Instance Method Details

#fieldset(legend = nil, **options) ⇒ Object

The fieldset a form's controls sit in: a grid, with legend over them where one is given, wearing nothing of ours. It carries its own spacing, twice as much under it as over, so one group reads as belonging to the words above it rather than sitting evenly between two. Every option is the fieldset's.



30
31
32
33
34
35
# File 'lib/bh/form_builder.rb', line 30

def fieldset(legend = nil, **options, &)
  heading = (@template.tag.legend legend if legend)
  inside = @template.safe_join [heading, @template.capture(&)].compact

  @template.tag.fieldset inside, **with_classes(options, 'd-grid bh-fieldset')
end

#file_field(method, options = {}) ⇒ Object

A file to attach, which Rails hands over undressed.



57
# File 'lib/bh/form_builder.rb', line 57

def file_field(method, options = {}) = super(method, dressed(method, options))

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

A label is Bootstrap's form-label, whatever else it is given -- and given both words and a block, the control the words name as well, which Rails' own label has no meaning for: it drops the words and renders the block. Wrapped, the pair is one element, so a form laid out in columns has a cell to place and neither an id nor a stylesheet counting children has to bind them -- which a control drawing markup of its own, a combobox say, would break on connect.



43
44
45
46
47
48
49
50
51
# File 'lib/bh/form_builder.rb', line 43

def label(method, text = nil, options = {}, &block)
  return label(method, nil, text, &block) if text.is_a? Hash
  return super(method, text, with_classes(options, 'form-label'), &block) unless text && block

  words = @template.tag.span text, class: 'form-label'
  inside = @template.safe_join [words, @template.capture(&block)]

  @template.tag.label inside, for: field_id(method), **with_classes(options, 'bh-field')
end

#phone_field(method, options = {}) ⇒ Object

A phone field is a large control shaped by the phone controller as it is typed, and showing the shape it will take. Read at the call rather than frozen into PHONE, since how a number is written is the reader's language and not this gem's.



62
63
64
65
66
# File 'lib/bh/form_builder.rb', line 62

def phone_field(method, options = {})
  shown = { placeholder: @template.t('bh.phone') }

  super(method, PHONE.merge(shown).deep_merge(dressed(method, options)))
end

#text_field(method, options = {}) ⇒ Object

A text field is a large control.



54
# File 'lib/bh/form_builder.rb', line 54

def text_field(method, options = {}) = super(method, dressed(method, options))