Module: BootstrapConcerns::FormBuilder

Defined in:
lib/bootstrap_concerns/form_builder.rb

Constant Summary collapse

FORM_LABEL_BASE_CLASS =
"form-label".freeze
FORM_CHECK_INPUT_BASE_CLASS =
"form-check-input".freeze
FORM_CONTROL_BASE_CLASS =
"form-control".freeze
FORM_CONTROL_PLAIN_TEXT_BASE_CLASS =
"form-control-plaintext".freeze
FORM_SELECT_BASE_CLASS =
"form-select".freeze
REQUIRED_CLASS =
"required".freeze

Instance Method Summary collapse

Instance Method Details

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



10
11
12
13
14
15
# File 'lib/bootstrap_concerns/form_builder.rb', line 10

def bs_button(value = nil, options = {}, &)
  normalized_options = value.is_a?(Hash) ? value : options
  normalized_options.merge!(Option.options_with_button_class(normalized_options))

  button(value, options, &)
end

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



17
18
19
# File 'lib/bootstrap_concerns/form_builder.rb', line 17

def bs_check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  check_box(method, options_with_form_check_input_class(options), checked_value, unchecked_value)
end

#bs_check_label(method, text = nil, options = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/bootstrap_concerns/form_builder.rb', line 21

def bs_check_label(method, text = nil, options = {}, &)
  normalized_options = text.is_a?(Hash) ? text : options
  normalized_options.merge!(options_with_form_label_class(normalized_options))

  label(method, text, options, &)
end

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



32
33
34
35
36
37
38
39
40
41
# File 'lib/bootstrap_concerns/form_builder.rb', line 32

def bs_collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
  collection_select(
    method,
    collection,
    value_method,
    text_method,
    options,
    options_with_form_select_class(options, html_options)
  )
end

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



28
29
30
# File 'lib/bootstrap_concerns/form_builder.rb', line 28

def bs_color_field(method, options = {})
  color_field(method, options_with_form_control_class(options))
end

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



43
44
45
# File 'lib/bootstrap_concerns/form_builder.rb', line 43

def bs_date_field(method, options = {})
  date_field(method, options_with_form_control_class(options))
end

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



47
48
49
# File 'lib/bootstrap_concerns/form_builder.rb', line 47

def bs_datetime_field(method, options = {})
  datetime_field(method, options_with_form_control_class(options))
end

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



51
52
53
# File 'lib/bootstrap_concerns/form_builder.rb', line 51

def bs_email_field(method, options = {})
  email_field(method, options_with_form_control_class(options))
end

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/bootstrap_concerns/form_builder.rb', line 93

def bs_file_field(method, options = {})
  file_field(
    method,
    {
      class: FORM_CONTROL_BASE_CLASS,
      direct_upload: true,
      data: {
        controller: "direct-upload-field",
        action: <<~ACTION.squish
          direct-upload:initialize->direct-upload-field#setup
          direct-upload:progress->direct-upload-field#progress
          direct-upload:error->direct-upload-field#error
          direct-upload:end->direct-upload-field#end
        ACTION
      }
    }.merge(options)
  )
end

#bs_form_group(method, type, *args) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/bootstrap_concerns/form_builder.rb', line 55

def bs_form_group(method, type, *args)
  options = args.find { it.is_a?(Hash) }.to_h

  @template.(:div, class: ComponentsHelper::MARGIN) do
    @template.concat bs_label(method, options.slice(:required))
    @template.concat public_send("bs_#{type}", method, *args)
  end
end

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bootstrap_concerns/form_builder.rb', line 64

def bs_grouped_collection_select(
  method,
  collection,
  group_method,
  group_label_method,
  option_key_method,
  option_value_method,
  options = {},
  html_options = {}
)
  grouped_collection_select(
    method,
    collection,
    group_method,
    group_label_method,
    option_key_method,
    option_value_method,
    options,
    options_with_form_select_class(options, html_options)
  )
end

#bs_label(method, text = nil, options = {}) ⇒ Object



86
87
88
89
90
91
# File 'lib/bootstrap_concerns/form_builder.rb', line 86

def bs_label(method, text = nil, options = {}, &)
  normalized_options = text.is_a?(Hash) ? text : options
  normalized_options.merge!(options_with_form_label_class(normalized_options))

  label(method, text, options, &)
end

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



112
113
114
# File 'lib/bootstrap_concerns/form_builder.rb', line 112

def bs_number_field(method, options = {})
  number_field(method, options_with_form_control_class(options))
end

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



116
117
118
# File 'lib/bootstrap_concerns/form_builder.rb', line 116

def bs_password_field(method, options = {})
  password_field(method, options_with_form_control_class(options))
end

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



143
144
145
# File 'lib/bootstrap_concerns/form_builder.rb', line 143

def bs_phone_field(method, options = {})
  phone_field(method, options_with_form_control_class(options))
end

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



147
148
149
150
151
152
# File 'lib/bootstrap_concerns/form_builder.rb', line 147

def bs_plain_text_field(method, options = {})
  text_field(
    method,
    Option.options_with_base_class(options, FORM_CONTROL_PLAIN_TEXT_BASE_CLASS).merge(readonly: true)
  )
end

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



120
121
122
# File 'lib/bootstrap_concerns/form_builder.rb', line 120

def bs_radio_button(method, tag_value, options = {})
  radio_button(method, tag_value, options_with_form_check_input_class(options))
end

#bs_save_submit(options = {}) ⇒ Object



128
129
130
# File 'lib/bootstrap_concerns/form_builder.rb', line 128

def bs_save_submit(options = {})
  bs_submit("Save", options)
end

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



124
125
126
# File 'lib/bootstrap_concerns/form_builder.rb', line 124

def bs_search_field(method, options = {})
  search_field(method, options_with_form_control_class(options))
end

#bs_select(method, choices = nil, options = {}, html_options = {}) ⇒ Object



132
133
134
# File 'lib/bootstrap_concerns/form_builder.rb', line 132

def bs_select(method, choices = nil, options = {}, html_options = {}, &)
  select(method, choices, options, options_with_form_select_class(options, html_options), &)
end

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



136
137
138
139
140
141
# File 'lib/bootstrap_concerns/form_builder.rb', line 136

def bs_submit(value = nil, options = {})
  normalized_options = value.is_a?(Hash) ? value : options
  normalized_options.merge!(Option.options_with_button_class(normalized_options))

  submit(value, options)
end

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



158
159
160
# File 'lib/bootstrap_concerns/form_builder.rb', line 158

def bs_text_area(method, options = {})
  text_area(method, options_with_form_control_class(options))
end

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



154
155
156
# File 'lib/bootstrap_concerns/form_builder.rb', line 154

def bs_text_field(method, options = {})
  text_field(method, options_with_form_control_class(options))
end

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



162
163
164
# File 'lib/bootstrap_concerns/form_builder.rb', line 162

def bs_url_field(method, options = {})
  url_field(method, options_with_form_control_class(options))
end