-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_box.rb
82 lines (68 loc) · 3.02 KB
/
check_box.rb
1
2
3
4
5
6
7
8
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-
module RailsBootstrapForm
module Helpers
module CheckBox
extend ActiveSupport::Concern
def self.included(base_class)
def check_box_label(attribute, checked_value, options, bootstrap, &block)
unless bootstrap.skip_label?
label_options = {
class: check_box_label_class(attribute, bootstrap, options)
}
label_options[:for] = options[:id] if options[:id].present?
label_name = if options[:multiple]
check_box_value(attribute, checked_value)
else
attribute
end
label_text = check_box_label_text(attribute, options, bootstrap, &block)
label(label_name, label_text, label_options)
end
end
def check_box_label_text(attribute, options, bootstrap, &block)
block ? capture(&block) : label_text(attribute, bootstrap)
end
def check_box_value(attribute, value)
# label's `for` attribute needs to match checkbox tag's id,
# IE sanitized value, IE
# https://github.com/rails/rails/blob/5-0-stable/actionview/lib/action_view/helpers/tags/base.rb#L123-L125
"#{attribute}_#{value.to_s.gsub(/\s/, "_").gsub(/[^-[[:word:]]]/, "").mb_chars.downcase}"
end
def check_box_classes(attribute, options)
classes = Array(options[:class]) << "form-check-input"
classes << "is-invalid" if is_invalid?(attribute)
classes.flatten.compact
end
def check_box_label_class(attribute, bootstrap, options)
classes = Array("form-check-label") << bootstrap.additional_label_class
classes << "required" if is_field_required?(attribute, options)
classes << "is-invalid" if is_invalid?(attribute)
classes << bootstrap.hide_class if bootstrap.hide_label?
classes.flatten.compact
end
def check_box_wrapper_classes(bootstrap)
classes = Array("form-check")
classes << "form-switch" if bootstrap.switch?
classes << "form-check-inline" if bootstrap.inline?
classes.flatten.compact
end
def check_box_container_classes(bootstrap)
classes = Array(bootstrap.field_col_wrapper_class)
classes << field_offset_class(bootstrap.label_col_wrapper_class)
classes.flatten.compact
end
def bootstrap_check_box(attribute, value, options, bootstrap)
options[:class] = check_box_classes(attribute, options)
check_box_field = check_box_without_bootstrap(attribute, options, value, nil)
check_box_label = check_box_label(attribute, value, options, bootstrap)
check_box_field + check_box_label
end
private :check_box_label, :check_box_classes, :check_box_label_class,
:check_box_wrapper_classes, :check_box_container_classes,
:bootstrap_check_box
end
end
end
end