Class: Railsui::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Railsui::FormBuilder
- Defined in:
- lib/railsui/form_builder.rb
Instance Method Summary collapse
- #button_toggle(method, tag_value, options = {}) ⇒ Object
- #check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
- #color_field(method, options = {}) ⇒ Object
- #date_field(method, options = {}) ⇒ Object
- #datetime_field(method, options = {}) ⇒ Object
- #email_field(method, options = {}) ⇒ Object
- #error_message(method) ⇒ Object
- #file_field(method, options = {}) ⇒ Object
- #form_group(method = nil, options = {}, &block) ⇒ Object
- #form_help(text, options = {}) ⇒ Object
- #label(method, text = nil, options = {}) ⇒ Object
- #number_field(method, options = {}) ⇒ Object
- #password_field(method, options = {}) ⇒ Object
- #radio_button(method, tag_value, options = {}) ⇒ Object
- #range_field(method, options = {}) ⇒ Object
- #rich_text_area(method, options = {}) ⇒ Object
- #search_field(method, options = {}) ⇒ Object
- #select(method, choices = nil, options = {}, html_options = {}) ⇒ Object
- #submit(value = nil, options = {}) ⇒ Object
- #switch_field(method, options = {}) ⇒ Object
- #telephone_field(method, options = {}) ⇒ Object (also: #phone_field)
- #text_area(method, options = {}) ⇒ Object
- #text_field(method, options = {}) ⇒ Object
- #time_field(method, options = {}) ⇒ Object
- #url_field(method, options = {}) ⇒ Object
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 (method, tag_value, = {}) label_text = .delete(:label) || tag_value.to_s.humanize variant = .delete(:variant) # sm, lg, ghost, muted field_wrapper(method, .merge(label: label_text)) do base_class = "form-input-button-toggle" base_class += "-#{variant}" if variant add_default_class!(, base_class) # Call ActionView radio_button helper directly to avoid flex wrapper @template.(@object_name, method, tag_value, ()) 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, = {}, checked_value = "1", unchecked_value = "0") = .delete(:wrapper) label_text = .delete(:label) label_class = .delete(:label_class) || "form-label" is_required = .delete(:required) || false # If wrapper is explicitly false, return just the checkbox without wrapping if == false add_error_class!() if has_error?(method) return super(method, , checked_value, unchecked_value) end ||= {} # Set default flex layout for the wrapper wrapper_class = "flex items-center justify-start gap-2" [:class] = [wrapper_class, [:class]].compact.join(" ") content_tag(:div, ) do add_default_class!(, "form-input-checkbox") add_error_class!() if has_error?(method) if label_text check_box_html = super(method, , checked_value, unchecked_value) = { class: label_class } [:required] = true if is_required label_html = label(method, label_text, ) safe_join([check_box_html, label_html]) else super(method, , 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, = {}) field_wrapper(method, ) do add_default_class!(, "form-input-color") add_error_class!() if has_error?(method) super(method, ) 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, = {}) field_wrapper(method, ) do add_default_class!(, "form-input") add_error_class!() if has_error?(method) super(method, ) 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, = {}) field_wrapper(method, ) do add_default_class!(, "form-input") add_error_class!() if has_error?(method) super(method, ) 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, = {}) field_wrapper(method, ) do add_default_class!(, "form-input") add_error_class!() if has_error?(method) super(method, ) end end |
#error_message(method) ⇒ Object
254 255 256 257 258 259 260 |
# File 'lib/railsui/form_builder.rb', line 254 def (method) return unless has_error?(method) content_tag(: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, = {}) field_wrapper(method, ) do add_default_class!(, "form-file") add_error_class!() if has_error?(method) super(method, ) 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, = {}, &block) content_tag(:div, class: "form-group #{[:class]}", &block) end |
#form_help(text, options = {}) ⇒ Object
266 267 268 269 |
# File 'lib/railsui/form_builder.rb', line 266 def form_help(text, = {}) add_default_class!(, "form-help") content_tag(:p, text, ) 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, = {}) add_default_class!(, "form-label") # Check both the required option and model validators is_required = .delete(:required) || required_field?(method) [:class] += " form-label-required" if is_required super(method, text, ) 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, = {}) field_wrapper(method, ) do add_default_class!(, "form-input") add_error_class!() if has_error?(method) super(method, ) 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, = {}) field_wrapper(method, ) do add_default_class!(, "form-input") add_error_class!() if has_error?(method) super(method, ) 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 (method, tag_value, = {}) = .delete(:wrapper) label_text = .delete(:label) # If wrapper is explicitly false, return just the radio button without wrapping if == false add_error_class!() if has_error?(method) return super(method, tag_value, ) end ||= {} # Set default flex layout for the wrapper wrapper_class = "flex items-center justify-start gap-2" [:class] = [wrapper_class, [:class]].compact.join(" ") content_tag(:div, ) do add_default_class!(, "form-input-radio") add_error_class!() if has_error?(method) if label_text radio_html = super(method, tag_value, ) label_html = label(method, label_text, value: tag_value, class: "form-label") safe_join([radio_html, label_html]) else super(method, tag_value, ) 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, = {}) # Extract wrapper options and add stimulus controller to the form group = .delete(:wrapper) || {} ["data-controller"] = "railsui-range" field_wrapper(method, .merge(wrapper: )) do add_default_class!(, "form-input-range") add_error_class!() if has_error?(method) # Add stimulus target and action data attributes to the input ["data-railsui-range-target"] = "range" ["data-action"] = "input->railsui-range#onInput" super(method, ) 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, = {}) field_wrapper(method, ) do add_default_class!(, "trix-content") add_error_class!() if has_error?(method) super(method, ) 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, = {}) field_wrapper(method, ) do add_default_class!(, "form-input") add_error_class!() if has_error?(method) super(method, ) 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, = {}, = {}) field_wrapper(method, ) do add_default_class!(, "form-select") add_error_class!() if has_error?(method) super(method, choices, , ) end end |
#submit(value = nil, options = {}) ⇒ Object
271 272 273 274 |
# File 'lib/railsui/form_builder.rb', line 271 def submit(value = nil, = {}) add_default_class!(, "btn btn-primary") super(value, ) 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, = {}) = .delete(:wrapper) label_text = .delete(:label) || method.to_s.humanize add_default_class!(, "form-input-switch") add_error_class!() if has_error?(method) # Create switch input without hidden field switch_html = @template.check_box(@object_name, method, (.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 == false return switch_content end field_wrapper(method, { wrapper: , 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, = {}) field_wrapper(method, ) do add_default_class!(, "form-input") add_error_class!() if has_error?(method) super(method, ) 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, = {}) field_wrapper(method, ) do add_default_class!(, "form-textarea") add_error_class!() if has_error?(method) super(method, ) 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, = {}) field_wrapper(method, ) do add_default_class!(, "form-input") add_error_class!() if has_error?(method) super(method, ) 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, = {}) field_wrapper(method, ) do add_default_class!(, "form-input") add_error_class!() if has_error?(method) super(method, ) 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, = {}) field_wrapper(method, ) do add_default_class!(, "form-input") add_error_class!() if has_error?(method) super(method, ) end end |