Class: PrimerFormBuilder::Builder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- PrimerFormBuilder::Builder
- Defined in:
- lib/primer_form_builder/form_builder.rb
Overview
The Builder class is used by Rails to build forms with Primer markup.
It should be passed as the builder:
argument to a form_with
method, i.e. form_with builder: PrimerFormBuilder::Builder.
Instance Method Summary collapse
- #button(label = nil, *args) ⇒ Object
- #check_box_single(name, *args) ⇒ Object
- #datetime_select(method, options = {}, html_options = {}) ⇒ Object
- #email_field(name, *args) ⇒ Object
- #error_note(field) ⇒ Object
- #errored?(field) ⇒ Boolean
- #group(field = nil, *args, &block) ⇒ Object
- #label(name, *args) ⇒ Object
- #note(content, *args) ⇒ Object
- #number_field(name, *args) ⇒ Object
- #password_field(name, *args) ⇒ Object
- #plain_label ⇒ Object
- #sr_label(name, *args) ⇒ Object
- #submit(label = nil, *args) ⇒ Object
- #telephone_field(name, *args) ⇒ Object
- #text_area(name, *args) ⇒ Object
- #text_field(name, *args) ⇒ Object
- #url_field(name, *args) ⇒ Object
Instance Method Details
#button(label = nil, *args) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/primer_form_builder/form_builder.rb', line 88 def (label = nil, *args) = args. [:class] = if [:type].nil? || [:type] == :submit [[:class], :btn, :'btn-primary'] else [[:class], :btn] end super(label, *(args << )) end |
#check_box_single(name, *args) ⇒ Object
70 71 72 73 74 |
# File 'lib/primer_form_builder/form_builder.rb', line 70 def check_box_single(name, *args) cb = check_box(name, *(args << )) cb << human_name(name) tag.div tag.label(cb), class: "form-checkbox" end |
#datetime_select(method, options = {}, html_options = {}) ⇒ Object
65 66 67 68 |
# File 'lib/primer_form_builder/form_builder.rb', line 65 def datetime_select(method, = {}, = {}) [:class] = [[:class], :'form-select'] super(method, , ) end |
#email_field(name, *args) ⇒ Object
29 30 31 32 33 |
# File 'lib/primer_form_builder/form_builder.rb', line 29 def email_field(name, *args) = args. [:class] = [[:class], :'form-control'] super(name, *(args << )) end |
#error_note(field) ⇒ Object
110 111 112 113 114 |
# File 'lib/primer_form_builder/form_builder.rb', line 110 def error_note(field) return unless errored?(field) tag.p(@object.errors[field].join(", "), class: %i[note error]) end |
#errored?(field) ⇒ Boolean
116 117 118 119 120 |
# File 'lib/primer_form_builder/form_builder.rb', line 116 def errored?(field) return false unless @object.respond_to?(:errors) @object.errors.key?(field) end |
#group(field = nil, *args, &block) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/primer_form_builder/form_builder.rb', line 98 def group(field = nil, *args, &block) = args. [:class] = %w[form-group] [:class] << "errored" if errored?(field) tag.div(**) do safe_join [ capture(&block), error_note(field) ].compact end end |
#label(name, *args) ⇒ Object
19 20 21 |
# File 'lib/primer_form_builder/form_builder.rb', line 19 def label(name, *args) tag.div super(name, *args), class: :'form-group-header' end |
#note(content, *args) ⇒ Object
76 77 78 79 80 |
# File 'lib/primer_form_builder/form_builder.rb', line 76 def note(content, *args) = args. [:class] = [[:class], :note] tag.p content, ** end |
#number_field(name, *args) ⇒ Object
41 42 43 44 45 |
# File 'lib/primer_form_builder/form_builder.rb', line 41 def number_field(name, *args) = args. [:class] = [[:class], :'form-control'] super(name, *(args << )) end |
#password_field(name, *args) ⇒ Object
53 54 55 56 57 |
# File 'lib/primer_form_builder/form_builder.rb', line 53 def password_field(name, *args) = args. [:class] = [[:class], :'form-control'] super(name, *(args << )) end |
#plain_label ⇒ Object
11 |
# File 'lib/primer_form_builder/form_builder.rb', line 11 alias plain_label label |
#sr_label(name, *args) ⇒ Object
13 14 15 16 17 |
# File 'lib/primer_form_builder/form_builder.rb', line 13 def sr_label(name, *args) = args. [:class] = [[:class], :'sr-only'] plain_label(name, *(args << )) end |
#submit(label = nil, *args) ⇒ Object
82 83 84 85 86 |
# File 'lib/primer_form_builder/form_builder.rb', line 82 def submit(label = nil, *args) = args. [:class] = %w[btn btn-primary] if [:class].blank? super(label, *(args << )) end |
#telephone_field(name, *args) ⇒ Object
47 48 49 50 51 |
# File 'lib/primer_form_builder/form_builder.rb', line 47 def telephone_field(name, *args) = args. [:class] = [[:class], :'form-control'] super(name, *(args << )) end |
#text_area(name, *args) ⇒ Object
59 60 61 62 63 |
# File 'lib/primer_form_builder/form_builder.rb', line 59 def text_area(name, *args) = args. [:class] = [[:class], :'form-control'] super(name, *(args << )) end |
#text_field(name, *args) ⇒ Object
23 24 25 26 27 |
# File 'lib/primer_form_builder/form_builder.rb', line 23 def text_field(name, *args) = args. [:class] = [[:class], :'form-control'] super(name, *(args << )) end |
#url_field(name, *args) ⇒ Object
35 36 37 38 39 |
# File 'lib/primer_form_builder/form_builder.rb', line 35 def url_field(name, *args) = args. [:class] = [[:class], :'form-control'] super(name, *(args << )) end |