Class: Fluxbit::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::OutputSafetyHelper
Defined in:
app/helpers/fluxbit/form_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#templateObject (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



36
37
38
39
40
41
# File 'app/helpers/fluxbit/form_builder.rb', line 36

def error_for(method)
  return if object.blank?
  return unless object.errors.key?(method)

  raw object.errors.full_messages_for(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
# 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 |banner|
    [
      render(Fluxbit::ListComponent.new) do |list|
        object.errors.full_messages.each do |error|
          list.with_item { error.html_safe }
        end
      end,
      (template.capture { yield(banner) } if block_given?)
    ].compact.join.html_safe
  end
end

#fluxbit_inline_error_for(method, **options, &block) ⇒ Object



43
44
45
46
47
48
49
50
# File 'app/helpers/fluxbit/form_builder.rb', line 43

def fluxbit_inline_error_for(method, **options, &block)
  error_message = error_for(method)
  return unless error_message

  render(Fluxbit::InlineErrorComponent.new(**options, &block)) do
    error_message
  end
end

#fx_select(method, **options, &block) ⇒ Object

select(object, method, choices = nil, options = {}, html_options = {}, &block) public



78
79
80
81
82
83
84
85
# File 'app/helpers/fluxbit/form_builder.rb', line 78

def fx_select(method, **options, &block)
  options[:error] ||= error_for(method)
  options[:error] = !!options[:error] if options[:error_hidden] && options[:error]
  value = object&.public_send(method)
  options[:selected] = value if value.present?

  render Fluxbit::Form::SelectComponent.new(form: self, attribute: method, **options, &block)
end