Class: SexyForm::Themes::Bootstrap4Horizontal

Inherits:
BaseTheme
  • Object
show all
Defined in:
lib/sexy_form/themes/bootstrap_4_horizontal.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column_classes: ["col-sm-3", "col-sm-9"]) ⇒ Bootstrap4Horizontal

Returns a new instance of Bootstrap4Horizontal.



9
10
11
12
13
14
# File 'lib/sexy_form/themes/bootstrap_4_horizontal.rb', line 9

def initialize(column_classes: ["col-sm-3", "col-sm-9"])
  @column_classes = column_classes.first(2)

  s = "#{@column_classes[0]}"
  @offset_class = (i = s.index(/-\d/)) ? s.insert(i+1, "offset-") : ""
end

Class Method Details

.theme_nameObject



5
6
7
# File 'lib/sexy_form/themes/bootstrap_4_horizontal.rb', line 5

def self.theme_name
  "bootstrap_4_horizontal"
end

Instance Method Details

#build_html_error(error:, html_attrs:, field_type:) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/sexy_form/themes/bootstrap_4_horizontal.rb', line 83

def build_html_error(error:, html_attrs:, field_type:)
  html_attrs["class"] = "invalid-feedback #{html_attrs["class"]}".strip

  s = ""
  s << (html_attrs.empty? ? "<div>" : "<div #{SexyForm.build_html_attr_string(html_attrs)}>")
  s << "#{error}"
  s << "</div>"
  s
end

#build_html_help_text(help_text:, html_attrs:, field_type:) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/sexy_form/themes/bootstrap_4_horizontal.rb', line 73

def build_html_help_text(help_text:, html_attrs:, field_type:)
  html_attrs["class"] = "form-text #{html_attrs["class"]}".strip

  s = ""
  s << (html_attrs.empty? ? "<small>" : "<small #{SexyForm.build_html_attr_string(html_attrs)}>")
  s << "#{help_text}"
  s << "</small>"
  s
end

#form_html_attributes(html_attrs:) ⇒ Object



69
70
71
# File 'lib/sexy_form/themes/bootstrap_4_horizontal.rb', line 69

def form_html_attributes(html_attrs:)
  html_attrs
end

#input_html_attributes(html_attrs:, field_type:, has_errors:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sexy_form/themes/bootstrap_4_horizontal.rb', line 46

def input_html_attributes(html_attrs:, field_type:, has_errors:)
  case field_type
  when "checkbox", "radio"
    html_attrs["class"] = "form-check-input#{" is-invalid" if has_errors} #{html_attrs["class"]}".strip
  when "file"
    html_attrs["class"] = "form-control-file#{" is-invalid" if has_errors} #{html_attrs["class"]}".strip
  else
    html_attrs["class"] = "form-control#{" is-invalid" if has_errors} #{html_attrs["class"]}".strip
  end

  html_attrs
end

#label_html_attributes(html_attrs:, field_type:, has_errors:) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/sexy_form/themes/bootstrap_4_horizontal.rb', line 59

def label_html_attributes(html_attrs:, field_type:, has_errors:)
  if ["checkbox", "radio"].include?(field_type)
    html_attrs["class"] = "form-check-label #{html_attrs["class"]}".strip
  else
    html_attrs["class"] =  "#{@column_classes[0]} col-form-label #{html_attrs["class"]}".strip
  end

  html_attrs
end

#wrap_field(field_type:, html_field:, html_label:, html_help_text: nil, html_errors: nil, wrapper_html_attributes:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sexy_form/themes/bootstrap_4_horizontal.rb', line 16

def wrap_field(field_type:, html_field:, html_label:, html_help_text: nil, html_errors: nil, wrapper_html_attributes:)
  s = ""

  wrapper_html_attributes["class"] = "form-group row #{wrapper_html_attributes["class"]}".strip

  attr_str = SexyForm.build_html_attr_string(wrapper_html_attributes)
  s << "#{attr_str.empty? ? "<div>" : "<div #{attr_str}>"}"

  if ["checkbox", "radio"].include?(field_type)
    s << %Q(<div class="#{@offset_class} #{@column_classes[1]}">)
    s << %Q(<div class="form-check">)
    s << "#{html_field}"
    s << "#{html_label}"
    s << "#{html_help_text}"
    s << html_errors.join if html_errors
    s << "</div>"
    s << "</div>"
  else
    s << "#{html_label}"
    s << %Q(<div class="#{"#{@offset_class} " unless html_label}#{@column_classes[1]}">)
    s << "#{html_field}"
    s << "#{html_help_text}"
    s << html_errors.join if html_errors
    s << "</div>"

  end

  s << "</div>"
end