Class: Railsui::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/railsui/form_builder.rb

Instance Method Summary collapse

Instance Method Details

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



232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/railsui/form_builder.rb', line 232

def button_toggle(method, tag_value, options = {})
  label_text = options.delete(:label) || tag_value.to_s.humanize
  variant = options.delete(:variant) # sm, lg, ghost, muted

  field_wrapper(method, options.merge(label: label_text)) do
    base_class = "form-input-button-toggle"
    base_class += "-#{variant}" if variant
    add_default_class!(options, base_class)

    # Call ActionView radio_button helper directly to avoid flex wrapper
    @template.radio_button(@object_name, method, tag_value, objectify_options(options))
  end
end

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/railsui/form_builder.rb', line 112

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  wrapper_options = options.delete(:wrapper)
  label_text = options.delete(:label)
  label_class = options.delete(:label_class) || "form-label"
  is_required = options.delete(:required) || false

  # If wrapper is explicitly false, return just the checkbox without wrapping
  if wrapper_options == false
    add_error_class!(options) if has_error?(method)
    return super(method, options, checked_value, unchecked_value)
  end

  wrapper_options ||= {}

  # Set default flex layout for the wrapper
  wrapper_class = "flex items-center justify-start gap-2"
  wrapper_options[:class] = [wrapper_class, wrapper_options[:class]].compact.join(" ")

  (:div, wrapper_options) do
    add_default_class!(options, "form-input-checkbox")
    add_error_class!(options) if has_error?(method)

    if label_text
      check_box_html = super(method, options, checked_value, unchecked_value)
      label_options = { class: label_class }
      label_options[:required] = true if is_required
      label_html = label(method, label_text, label_options)
      safe_join([check_box_html, label_html])
    else
      super(method, options, checked_value, unchecked_value)
    end
  end
end

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



80
81
82
83
84
85
86
# File 'lib/railsui/form_builder.rb', line 80

def color_field(method, options = {})
  field_wrapper(method, options) do
    add_default_class!(options, "form-input-color")
    add_error_class!(options) if has_error?(method)
    super(method, options)
  end
end

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



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

def date_field(method, options = {})
  field_wrapper(method, options) do
    add_default_class!(options, "form-input")
    add_error_class!(options) if has_error?(method)
    super(method, options)
  end
end

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



64
65
66
67
68
69
70
# File 'lib/railsui/form_builder.rb', line 64

def datetime_field(method, options = {})
  field_wrapper(method, options) do
    add_default_class!(options, "form-input")
    add_error_class!(options) if has_error?(method)
    super(method, options)
  end
end

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



15
16
17
18
19
20
21
# File 'lib/railsui/form_builder.rb', line 15

def email_field(method, options = {})
  field_wrapper(method, options) do
    add_default_class!(options, "form-input")
    add_error_class!(options) if has_error?(method)
    super(method, options)
  end
end

#error_message(method) ⇒ Object



254
255
256
257
258
259
260
# File 'lib/railsui/form_builder.rb', line 254

def error_message(method)
  return unless has_error?(method)

  (:p, class: "mt-1 text-sm text-red-600 dark:text-red-400") do
    @object.errors[method].join(", ")
  end
end

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



176
177
178
179
180
181
182
# File 'lib/railsui/form_builder.rb', line 176

def file_field(method, options = {})
  field_wrapper(method, options) do
    add_default_class!(options, "form-file")
    add_error_class!(options) if has_error?(method)
    super(method, options)
  end
end

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



262
263
264
# File 'lib/railsui/form_builder.rb', line 262

def form_group(method = nil, options = {}, &block)
  (:div, class: "form-group #{options[:class]}", &block)
end

#form_help(text, options = {}) ⇒ Object



266
267
268
269
# File 'lib/railsui/form_builder.rb', line 266

def form_help(text, options = {})
  add_default_class!(options, "form-help")
  (:p, text, options)
end

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



246
247
248
249
250
251
252
# File 'lib/railsui/form_builder.rb', line 246

def label(method, text = nil, options = {})
  add_default_class!(options, "form-label")
  # Check both the required option and model validators
  is_required = options.delete(:required) || required_field?(method)
  options[:class] += " form-label-required" if is_required
  super(method, text, options)
end

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



31
32
33
34
35
36
37
# File 'lib/railsui/form_builder.rb', line 31

def number_field(method, options = {})
  field_wrapper(method, options) do
    add_default_class!(options, "form-input")
    add_error_class!(options) if has_error?(method)
    super(method, options)
  end
end

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



23
24
25
26
27
28
29
# File 'lib/railsui/form_builder.rb', line 23

def password_field(method, options = {})
  field_wrapper(method, options) do
    add_default_class!(options, "form-input")
    add_error_class!(options) if has_error?(method)
    super(method, options)
  end
end

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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/railsui/form_builder.rb', line 146

def radio_button(method, tag_value, options = {})
  wrapper_options = options.delete(:wrapper)
  label_text = options.delete(:label)

  # If wrapper is explicitly false, return just the radio button without wrapping
  if wrapper_options == false
    add_error_class!(options) if has_error?(method)
    return super(method, tag_value, options)
  end

  wrapper_options ||= {}

  # Set default flex layout for the wrapper
  wrapper_class = "flex items-center justify-start gap-2"
  wrapper_options[:class] = [wrapper_class, wrapper_options[:class]].compact.join(" ")

  (:div, wrapper_options) do
    add_default_class!(options, "form-input-radio")
    add_error_class!(options) if has_error?(method)

    if label_text
      radio_html = super(method, tag_value, options)
      label_html = label(method, label_text, value: tag_value, class: "form-label")
      safe_join([radio_html, label_html])
    else
      super(method, tag_value, options)
    end
  end
end

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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/railsui/form_builder.rb', line 192

def range_field(method, options = {})
  # Extract wrapper options and add stimulus controller to the form group
  wrapper_options = options.delete(:wrapper) || {}
  wrapper_options["data-controller"] = "railsui-range"

  field_wrapper(method, options.merge(wrapper: wrapper_options)) do
    add_default_class!(options, "form-input-range")
    add_error_class!(options) if has_error?(method)

    # Add stimulus target and action data attributes to the input
    options["data-railsui-range-target"] = "range"
    options["data-action"] = "input->railsui-range#onInput"

    super(method, options)
  end
end

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



184
185
186
187
188
189
190
# File 'lib/railsui/form_builder.rb', line 184

def rich_text_area(method, options = {})
  field_wrapper(method, options) do
    add_default_class!(options, "trix-content")
    add_error_class!(options) if has_error?(method)
    super(method, options)
  end
end

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



88
89
90
91
92
93
94
# File 'lib/railsui/form_builder.rb', line 88

def search_field(method, options = {})
  field_wrapper(method, options) do
    add_default_class!(options, "form-input")
    add_error_class!(options) if has_error?(method)
    super(method, options)
  end
end

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



104
105
106
107
108
109
110
# File 'lib/railsui/form_builder.rb', line 104

def select(method, choices = nil, options = {}, html_options = {})
  field_wrapper(method, html_options) do
    add_default_class!(html_options, "form-select")
    add_error_class!(html_options) if has_error?(method)
    super(method, choices, options, html_options)
  end
end

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



271
272
273
274
# File 'lib/railsui/form_builder.rb', line 271

def submit(value = nil, options = {})
  add_default_class!(options, "btn btn-primary")
  super(value, options)
end

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



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/railsui/form_builder.rb', line 210

def switch_field(method, options = {})
  wrapper_options = options.delete(:wrapper)
  label_text = options.delete(:label) || method.to_s.humanize

  add_default_class!(options, "form-input-switch")
  add_error_class!(options) if has_error?(method)

  # Create switch input without hidden field
  switch_html = @template.check_box(@object_name, method, objectify_options(options.merge(include_hidden: false)), "1", "0")
  label_html = label(method, label_text, class: "")
  switch_content = safe_join([switch_html, label_html])

  # If wrapper is explicitly false, return just the switch without wrapping
  if wrapper_options == false
    return switch_content
  end

  field_wrapper(method, { wrapper: wrapper_options, label: false }) do
    switch_content
  end
end

#telephone_field(method, options = {}) ⇒ Object Also known as: phone_field



39
40
41
42
43
44
45
# File 'lib/railsui/form_builder.rb', line 39

def telephone_field(method, options = {})
  field_wrapper(method, options) do
    add_default_class!(options, "form-input")
    add_error_class!(options) if has_error?(method)
    super(method, options)
  end
end

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



96
97
98
99
100
101
102
# File 'lib/railsui/form_builder.rb', line 96

def text_area(method, options = {})
  field_wrapper(method, options) do
    add_default_class!(options, "form-textarea")
    add_error_class!(options) if has_error?(method)
    super(method, options)
  end
end

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



7
8
9
10
11
12
13
# File 'lib/railsui/form_builder.rb', line 7

def text_field(method, options = {})
  field_wrapper(method, options) do
    add_default_class!(options, "form-input")
    add_error_class!(options) if has_error?(method)
    super(method, options)
  end
end

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



72
73
74
75
76
77
78
# File 'lib/railsui/form_builder.rb', line 72

def time_field(method, options = {})
  field_wrapper(method, options) do
    add_default_class!(options, "form-input")
    add_error_class!(options) if has_error?(method)
    super(method, options)
  end
end

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



48
49
50
51
52
53
54
# File 'lib/railsui/form_builder.rb', line 48

def url_field(method, options = {})
  field_wrapper(method, options) do
    add_default_class!(options, "form-input")
    add_error_class!(options) if has_error?(method)
    super(method, options)
  end
end