Class: Polaris::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::OutputSafetyHelper
Defined in:
app/helpers/polaris/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/polaris/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/polaris/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/polaris/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 Polaris::BannerComponent.new(
    title: title,
    status: :critical,
    within: within,
    data: {errors_summary: true}
  ) do |banner|
    [
      render(Polaris::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

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



121
122
123
124
125
126
127
# File 'app/helpers/polaris/form_builder.rb', line 121

def polaris_autocomplete(method, **options, &block)
  options[:error] ||= error_for(method)
  if options[:error_hidden] && options[:error]
    options[:error] = !!options[:error]
  end
  render Polaris::AutocompleteComponent.new(form: self, attribute: method, name: method, **options), &block
end

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



72
73
74
75
76
77
78
# File 'app/helpers/polaris/form_builder.rb', line 72

def polaris_check_box(method, **options, &block)
  options[:error] ||= error_for(method)
  if options[:error_hidden] && options[:error]
    options[:error] = !!options[:error]
  end
  render Polaris::CheckboxComponent.new(form: self, attribute: method, **options, &block)
end

#polaris_collection_check_boxes(method, collection, value_method, text_method, **options, &block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/helpers/polaris/form_builder.rb', line 96

def polaris_collection_check_boxes(method, collection, value_method, text_method, **options, &block)
  options[:error] ||= error_for(method)
  if options[:error_hidden] && options[:error]
    options[:error] = !!options[:error]
  end

  value = object&.public_send(method)
  if value.present?
    options[:selected] = value.map { |el| el.public_send(value_method) }
  end

  render Polaris::ChoiceListComponent.new(
    form: self,
    title: method.to_s.humanize,
    attribute: method,
    name: method,
    **options,
    &block
  ) do |choice|
    collection.each do |item|
      choice.with_checkbox(label: item.public_send(text_method), value: item.public_send(value_method))
    end
  end
end

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



88
89
90
91
92
93
94
# File 'app/helpers/polaris/form_builder.rb', line 88

def polaris_dropzone(method, **options, &block)
  options[:error] ||= error_for(method)
  if options[:error_hidden] && options[:error]
    options[:error] = !!options[:error]
  end
  render Polaris::DropzoneComponent.new(form: self, attribute: method, **options, &block)
end

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



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

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

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

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



80
81
82
83
84
85
86
# File 'app/helpers/polaris/form_builder.rb', line 80

def polaris_radio_button(method, **options, &block)
  options[:error] ||= error_for(method)
  if options[:error_hidden] && options[:error]
    options[:error] = !!options[:error]
  end
  render Polaris::RadioButtonComponent.new(form: self, attribute: method, **options, &block)
end

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



60
61
62
63
64
65
66
67
68
69
70
# File 'app/helpers/polaris/form_builder.rb', line 60

def polaris_select(method, **options, &block)
  options[:error] ||= error_for(method)
  if options[:error_hidden] && options[:error]
    options[:error] = !!options[:error]
  end
  value = object&.public_send(method)
  if value.present?
    options[:selected] = value
  end
  render Polaris::SelectComponent.new(form: self, attribute: method, **options, &block)
end

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



52
53
54
55
56
57
58
# File 'app/helpers/polaris/form_builder.rb', line 52

def polaris_text_field(method, **options, &block)
  options[:error] ||= error_for(method)
  if options[:error_hidden] && options[:error]
    options[:error] = !!options[:error]
  end
  render Polaris::TextFieldComponent.new(form: self, attribute: method, **options, &block)
end