forked from bootstrap-ruby/bootstrap_form
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabels.rb
More file actions
58 lines (49 loc) · 2.06 KB
/
labels.rb
File metadata and controls
58 lines (49 loc) · 2.06 KB
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
# frozen_string_literal: true
module BootstrapForm
module Components
module Labels
extend ActiveSupport::Concern
private
def generate_label(id, name, options, custom_label_col, group_layout)
return if options.blank?
prepare_label_options(id, name, options, custom_label_col, group_layout)
label(name, label_text(name, options[:text]), options.except(:text))
end
def label_classes(name, options, custom_label_col, group_layout)
classes = [options[:class] || label_layout_classes(custom_label_col, group_layout)]
add_class = options.delete(:add_class)
classes.prepend(add_class) if add_class
classes << "required" if required_field_options(options, name)[:required]
options.delete(:required)
classes << "text-danger" if label_errors && error?(name)
classes.flatten.compact
end
def label_layout_classes(custom_label_col, group_layout)
if layout_horizontal?(group_layout)
["col-form-label", custom_label_col || label_col]
elsif layout_inline?(group_layout)
%w[form-label me-sm-2]
else
"form-label"
end
end
def label_text(name, text)
label = text || object&.class&.try(:human_attribute_name, name)&.html_safe # rubocop:disable Rails/OutputSafety, Style/SafeNavigationChainLength
if label_errors && error?(name)
(" ".html_safe + get_error_messages(name)).prepend(label)
else
label
end
end
def prepare_label_options(id, name, options, custom_label_col, group_layout)
# id is the caller's options[:id] at the only place this method is called.
# The options argument is a small subset of the options that might have
# been passed to generate_label's caller, and definitely doesn't include
# :id.
options[:for] = id if acts_like_form_tag
options[:class] = label_classes(name, options, custom_label_col, group_layout)
options.delete(:class) if options[:class].none?
end
end
end
end