Class: Fluxbit::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Fluxbit::FormBuilder
- Includes:
- ActionView::Helpers::OutputSafetyHelper
- Defined in:
- app/helpers/fluxbit/form_builder.rb
Instance Attribute Summary collapse
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
- #error_for(method) ⇒ Object
- #errors_summary(within: :container) ⇒ Object
- #fluxbit_inline_error_for(method, **options, &block) ⇒ Object
- #fx_radio_group_button(method, **options, &block) ⇒ Object
-
#fx_select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object
Mimics Rails’ form.select signature: select(method, choices = nil, options = {}, html_options = {}, &block).
- #fx_submit(content = nil, **options, &block) ⇒ Object
Instance Attribute Details
#template ⇒ Object (readonly)
Returns the value of attribute template.
5 6 7 |
# File 'app/helpers/fluxbit/form_builder.rb', line 5 def template @template end |
Instance Method Details
#error_for(method) ⇒ Object
39 40 41 42 43 44 |
# File 'app/helpers/fluxbit/form_builder.rb', line 39 def error_for(method) return if object.blank? return unless object.errors.key?(method) raw object.errors.(method)&.first end |
#errors_summary(within: :container) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/helpers/fluxbit/form_builder.rb', line 9 def errors_summary(within: :container) return if object.blank? return unless object.errors.any? title = I18n.t( "polaris.form_builder.errors_summary", count: object.errors.count, model: object.class.model_name.human.downcase ) render Fluxbit::BannerComponent.new( title: title, status: :critical, within: within, data: { errors_summary: true } ) do || parts = [] parts << render(Fluxbit::ListComponent.new) do |list| object.errors..each do |error| list.with_item { template.sanitize(error.to_s, tags: [], attributes: []) } end end parts << template.capture { yield() } if block_given? template.safe_join(parts) end end |
#fluxbit_inline_error_for(method, **options, &block) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'app/helpers/fluxbit/form_builder.rb', line 46 def fluxbit_inline_error_for(method, **, &block) = error_for(method) return unless render(Fluxbit::InlineErrorComponent.new(**, &block)) do end end |
#fx_radio_group_button(method, **options, &block) ⇒ Object
80 81 82 83 |
# File 'app/helpers/fluxbit/form_builder.rb', line 80 def (method, **, &block) [:name] ||= "#{@object_name}[#{method}]" render Fluxbit::Form::RadioGroupButtonComponent.new(**), &block end |
#fx_select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object
Mimics Rails’ form.select signature: select(method, choices = nil, options = {}, html_options = {}, &block)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/helpers/fluxbit/form_builder.rb', line 102 def fx_select(method, choices = nil, = {}, = {}, &block) # Handle Rails-style signature = .merge() # Set the choices/options [:options] = choices if choices.present? # Add error handling [:error] ||= error_for(method) [:error] = !![:error] if [:error_hidden] && [:error] # Set selected value from object if not explicitly provided value = object&.public_send(method) [:selected] ||= value if value.present? render Fluxbit::Form::SelectComponent.new(form: self, attribute: method, **, &block) end |
#fx_submit(content = nil, **options, &block) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'app/helpers/fluxbit/form_builder.rb', line 120 def fx_submit(content = nil, **, &block) [:form] = self [:content] = content if content.present? [:color] ||= :primary [:size] ||= 2 [:disabled] = true if object&.persisted? && !object.valid? if block_given? render Fluxbit::ButtonComponent.new(**) do |*args| yield(*args) end else render Fluxbit::ButtonComponent.new(**) end end |