Class: Spree::Admin::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Spree::Admin::FormBuilder
- Defined in:
- app/models/spree/admin/form_builder.rb
Overview
Custom form builder for Spree admin interface
This form builder provides helper methods for creating form fields with consistent styling and behavior across the Spree admin interface. It includes form groups, error handling, help text, and other features.
Instance Method Summary collapse
-
#error_message_on(method, options = {}) ⇒ String
Display error messages for a specific field.
-
#spree_check_box(method, options = {}) ⇒ String
Create a checkbox with Spree custom control styling.
-
#spree_collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ String
Create a collection select field with Spree form styling.
-
#spree_date_field(method, options = {}) ⇒ String
Create a date field with Spree form styling.
-
#spree_datetime_field(method, options = {}) ⇒ String
Create a datetime field with Spree form styling.
-
#spree_email_field(method, options = {}) ⇒ String
Create an email field with Spree form styling.
-
#spree_file_field(method, options = {}) ⇒ String
Create a direct file upload field with Spree form styling.
-
#spree_money_field(method, options = {}) ⇒ String
Create a money/currency field with Spree form styling and locale-aware formatting.
-
#spree_number_field(method, options = {}) ⇒ String
Create a number field with Spree form styling.
-
#spree_radio_button(method, tag_value, options = {}) ⇒ String
Create a radio button with Spree custom control styling.
-
#spree_rich_text_area(method, options = {}) ⇒ String
Create a rich text area (Trix editor) with Spree form styling.
-
#spree_select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ String
Create a select field with Spree form styling.
-
#spree_text_area(method, options = {}) ⇒ String
Create a textarea with Spree form styling and auto-grow functionality.
-
#spree_text_field(method, options = {}) ⇒ String
Create a text field with Spree form styling.
Instance Method Details
#error_message_on(method, options = {}) ⇒ String
Display error messages for a specific field
14 15 16 |
# File 'app/models/spree/admin/form_builder.rb', line 14 def (method, = {}) @template.(@object_name, method, ()) end |
#spree_check_box(method, options = {}) ⇒ String
Create a checkbox with Spree custom control styling
267 268 269 270 271 272 273 274 275 |
# File 'app/models/spree/admin/form_builder.rb', line 267 def spree_check_box(method, = {}) @template.content_tag(:div, class: 'form-group') do @template.content_tag(:div, class: 'form-checkbox') do checkbox = @template.check_box(@object_name, method, (.merge(class: 'custom-control-input'))) label = [:label] == false ? ''.html_safe : @template.label(@object_name, method, get_label(method, ), class: 'custom-control-label') checkbox + label end + @template.(@object_name, method) + spree_field_help(method, .merge!(class: 'form-text mt-2 ml-6 pl-1')) end end |
#spree_collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ String
Create a collection select field with Spree form styling
247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'app/models/spree/admin/form_builder.rb', line 247 def spree_collection_select(method, collection, value_method, text_method, = {}, = {}) if [:autocomplete] [:data] ||= {} [:data][:controller] ||= 'autocomplete-select' else [:class] ||= 'form-select' end @template.content_tag(:div, class: 'form-group') do spree_label(method, ) + @template.collection_select(@object_name, method, collection, value_method, text_method, (), ) + @template.(@object_name, method) + spree_field_help(method, .merge(class: 'form-text mt-2')) end end |
#spree_date_field(method, options = {}) ⇒ String
Create a date field with Spree form styling
152 153 154 155 156 157 158 159 |
# File 'app/models/spree/admin/form_builder.rb', line 152 def spree_date_field(method, = {}) [:class] ||= 'form-input' @template.content_tag(:div, class: 'form-group') do spree_label(method, ) + @template.date_field(@object_name, method, ()) + @template.(@object_name, method) + spree_field_help(method, .merge(class: 'form-text mt-2')) end end |
#spree_datetime_field(method, options = {}) ⇒ String
Create a datetime field with Spree form styling
166 167 168 169 170 171 172 173 |
# File 'app/models/spree/admin/form_builder.rb', line 166 def spree_datetime_field(method, = {}) [:class] ||= 'form-input' @template.content_tag(:div, class: 'form-group') do spree_label(method, ) + @template.datetime_field(@object_name, method, ()) + @template.(@object_name, method) + spree_field_help(method, .merge(class: 'form-text mt-2')) end end |
#spree_email_field(method, options = {}) ⇒ String
Create an email field with Spree form styling
138 139 140 141 142 143 144 145 |
# File 'app/models/spree/admin/form_builder.rb', line 138 def spree_email_field(method, = {}) [:class] ||= 'form-input' @template.content_tag(:div, class: 'form-group') do spree_label(method, ) + @template.email_field(@object_name, method, ()) + @template.(@object_name, method) + spree_field_help(method, .merge(class: 'form-text mt-2')) end end |
#spree_file_field(method, options = {}) ⇒ String
Create a direct file upload field with Spree form styling
310 311 312 313 314 315 316 |
# File 'app/models/spree/admin/form_builder.rb', line 310 def spree_file_field(method, = {}) @template.content_tag(:div, class: 'form-group') do spree_label(method, ) + @template.render('active_storage/upload_form', form: self, field_name: method, **) + @template.(@object_name, method) + spree_field_help(method, .merge(class: 'form-text mt-2')) end end |
#spree_money_field(method, options = {}) ⇒ String
Create a money/currency field with Spree form styling and locale-aware formatting
This field displays amounts in the user’s locale format (e.g., “1.234,56” for German) and normalizes values to standard decimal format before form submission.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/spree/admin/form_builder.rb', line 82 def spree_money_field(method, = {}) currency = .delete(:currency) prepend = .delete(:prepend) append = .delete(:append) # Get currency object for formatting currency_obj = currency.present? ? ::Money::Currency.find(currency) : nil # Use currency symbol as append if currency is provided and no append specified if currency_obj.present? && append.nil? append = currency_obj.symbol end # Get decimal/thousands separators from currency or fall back to I18n locale locale = I18n.locale if currency_obj.present? decimal_separator = currency_obj.decimal_mark thousands_separator = currency_obj.thousands_separator else decimal_separator = I18n.t(:'number.currency.format.separator', default: '.') thousands_separator = I18n.t(:'number.currency.format.delimiter', default: ',') end # Format the value for display if present if [:value].present? [:value] = format_money_value([:value], decimal_separator) elsif @object.respond_to?(method) && @object.send(method).present? [:value] = format_money_value(@object.send(method), decimal_separator) end [:class] ||= 'form-input' [:inputmode] = 'decimal' [:data] ||= {} [:data][:controller] = 'money-field' [:data][:money_field_locale_value] = locale [:data][:money_field_decimal_separator_value] = decimal_separator [:data][:money_field_thousands_separator_value] = thousands_separator [:data][:action] = 'blur->money-field#format' if prepend.present? || append.present? [:class] = 'border-0 focus:ring-0 focus:outline-none text-base grow' [:class] += ' pl-0' if prepend.present? end @template.content_tag(:div, class: 'form-group') do spree_label(method, ) + wrap_with_input_group(@template.text_field(@object_name, method, ()), prepend, append) + @template.(@object_name, method) + spree_field_help(method, .merge(class: 'form-text mt-2')) end end |
#spree_number_field(method, options = {}) ⇒ String
Create a number field with Spree form styling
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/models/spree/admin/form_builder.rb', line 55 def spree_number_field(method, = {}) [:class] ||= 'form-input' prepend = .delete(:prepend) append = .delete(:append) if prepend.present? || append.present? [:class] ||= 'border-0 focus:ring-0 focus:outline-none' end @template.content_tag(:div, class: 'form-group') do spree_label(method, ) + wrap_with_input_group(@template.number_field(@object_name, method, ()), prepend, append) + @template.(@object_name, method) + spree_field_help(method, .merge(class: 'form-text mt-2')) end end |
#spree_radio_button(method, tag_value, options = {}) ⇒ String
Create a radio button with Spree custom control styling
285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'app/models/spree/admin/form_builder.rb', line 285 def (method, tag_value, = {}) @template.content_tag(:div, class: 'form-group') do @template.content_tag(:div, class: 'custom-control custom-radio') do radio = @template.(@object_name, method, tag_value, (.merge(class: 'custom-control-input form-radio'))) = { class: 'custom-control-label' } [:for] = [:id] if [:id] [:value] = tag_value unless [:id] label = [:label] == false ? ''.html_safe : @template.label(@object_name, method, get_label(method, ), ) radio + label end + @template.(@object_name, method) + spree_field_help(method, .merge(class: 'form-text mt-2')) end end |
#spree_rich_text_area(method, options = {}) ⇒ String
Create a rich text area (Trix editor) with Spree form styling
201 202 203 204 205 206 207 208 209 |
# File 'app/models/spree/admin/form_builder.rb', line 201 def spree_rich_text_area(method, = {}) @template.content_tag(:div, class: 'form-group') do spree_label(method, ) + @template.content_tag(:div, class: 'trix-container') do @template.rich_text_area(@object_name, method, ()) end + @template.(@object_name, method) + spree_field_help(method, .merge(class: 'form-text mt-2')) end end |
#spree_select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ String
Create a select field with Spree form styling
221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'app/models/spree/admin/form_builder.rb', line 221 def spree_select(method, choices = nil, = {}, = {}, &block) if [:autocomplete] [:data] ||= {} [:data][:controller] ||= 'autocomplete-select' else [:class] ||= 'form-select' end @template.content_tag(:div, class: 'form-group') do spree_label(method, ) + @template.select(@object_name, method, choices, (), , &block) + @template.(@object_name, method) + spree_field_help(method, .merge(class: 'form-text mt-2')) end end |
#spree_text_area(method, options = {}) ⇒ String
Create a textarea with Spree form styling and auto-grow functionality
184 185 186 187 188 189 190 191 192 193 194 |
# File 'app/models/spree/admin/form_builder.rb', line 184 def spree_text_area(method, = {}) @template.content_tag(:div, class: 'form-group') do [:class] ||= 'form-input' [:rows] ||= 5 [:data] ||= { controller: 'textarea-autogrow' } spree_label(method, ) + @template.text_area(@object_name, method, ()) + @template.(@object_name, method) + spree_field_help(method, .merge(class: 'form-text mt-2')) end end |
#spree_text_field(method, options = {}) ⇒ String
Create a text field with Spree form styling
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/models/spree/admin/form_builder.rb', line 30 def spree_text_field(method, = {}) [:class] ||= 'form-input' prepend = .delete(:prepend) append = .delete(:append) if prepend.present? || append.present? [:class] = 'border-0 focus:ring-0 focus:outline-none text-base grow' if prepend.present? [:class] += ' pl-0' end end @template.content_tag(:div, class: 'form-group') do spree_label(method, ) + wrap_with_input_group(@template.text_field(@object_name, method, ()), prepend, append) + @template.(@object_name, method) + spree_field_help(method, .merge(class: 'form-text mt-2')) end end |