Class: PrimerFormBuilder::Builder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
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

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 button(label = nil, *args)
  options = args.extract_options!
  options[:class] = if options[:type].nil? || options[:type] == :submit
                      [options[:class], :btn, :'btn-primary']
                    else
                      [options[:class], :btn]
                    end
  super(label, *(args << options))
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 << options))
  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, options = {}, html_options = {})
  html_options[:class] = [html_options[:class], :'form-select']
  super(method, options, html_options)
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)
  options = args.extract_options!
  options[:class] = [options[:class], :'form-control']
  super(name, *(args << options))
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

Returns:

  • (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)
  options = args.extract_options!
  options[:class] = %w[form-group]
  options[:class] << "errored" if errored?(field)
  tag.div(**options) 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)
  options = args.extract_options!
  options[:class] = [options[:class], :note]
  tag.p content, **options
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)
  options = args.extract_options!
  options[:class] = [options[:class], :'form-control']
  super(name, *(args << options))
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)
  options = args.extract_options!
  options[:class] = [options[:class], :'form-control']
  super(name, *(args << options))
end

#plain_labelObject



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)
  options = args.extract_options!
  options[:class] = [options[:class], :'sr-only']
  plain_label(name, *(args << options))
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)
  options = args.extract_options!
  options[:class] = %w[btn btn-primary] if options[:class].blank?
  super(label, *(args << options))
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)
  options = args.extract_options!
  options[:class] = [options[:class], :'form-control']
  super(name, *(args << options))
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)
  options = args.extract_options!
  options[:class] = [options[:class], :'form-control']
  super(name, *(args << options))
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)
  options = args.extract_options!
  options[:class] = [options[:class], :'form-control']
  super(name, *(args << options))
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)
  options = args.extract_options!
  options[:class] = [options[:class], :'form-control']
  super(name, *(args << options))
end