Skip to content

Commit 08207c8

Browse files
committed
Manually merging
1 parent 3fd5183 commit 08207c8

File tree

8 files changed

+2560
-789
lines changed

8 files changed

+2560
-789
lines changed

lib/bootstrap_form/components/labels.rb

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,8 @@ module Labels
1010
def generate_label(id, name, options, custom_label_col, group_layout)
1111
return if options.blank?
1212

13-
# id is the caller's options[:id] at the only place this method is called.
14-
# The options argument is a small subset of the options that might have
15-
# been passed to generate_label's caller, and definitely doesn't include
16-
# :id.
17-
options[:for] = id if acts_like_form_tag
18-
19-
options[:class] = label_classes(name, options, custom_label_col, group_layout)
20-
options.delete(:class) if options[:class].none?
21-
options[:id] = id.present? ? "#{id}_feedback" : field_id(name, :feedback) if error?(name) && label_errors
22-
23-
label(name, label_text(name, options), options.except(:text))
13+
prepare_label_options(id, name, options, custom_label_col, group_layout)
14+
label(name, label_text(name, options[:text]), options.except(:text))
2415
end
2516

2617
def label_classes(name, options, custom_label_col, group_layout)
@@ -43,14 +34,26 @@ def label_layout_classes(custom_label_col, group_layout)
4334
end
4435
end
4536

46-
def label_text(name, options)
47-
label = options[:text] || object&.class&.try(:human_attribute_name, name)&.html_safe # rubocop:disable Rails/OutputSafety, Style/SafeNavigationChainLength
37+
def label_text(name, text)
38+
label = text || object&.class&.try(:human_attribute_name, name)&.html_safe # rubocop:disable Rails/OutputSafety, Style/SafeNavigationChainLength
4839
if label_errors && error?(name)
4940
(" ".html_safe + get_error_messages(name)).prepend(label)
5041
else
5142
label
5243
end
5344
end
45+
46+
def prepare_label_options(id, name, options, custom_label_col, group_layout)
47+
# id is the caller's options[:id] at the only place this method is called.
48+
# The options argument is a small subset of the options that might have
49+
# been passed to generate_label's caller, and definitely doesn't include
50+
# :id.
51+
options[:for] = id if acts_like_form_tag
52+
53+
options[:class] = label_classes(name, options, custom_label_col, group_layout)
54+
options.delete(:class) if options[:class].none?
55+
options[:id] = id.present? ? "#{id}_feedback" : field_id(name, :feedback) if error?(name) && label_errors
56+
end
5457
end
5558
end
5659
end

lib/bootstrap_form/form_group_builder.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ module FormGroupBuilder
77
private
88

99
def form_group_builder(method, options, html_options=nil, &)
10+
form_group_builder_wrapper(method, options, html_options) do |form_group_options, no_wrapper|
11+
if no_wrapper
12+
yield
13+
else
14+
form_group(method, form_group_options, &)
15+
end
16+
end
17+
end
18+
19+
def form_group_builder_wrapper(method, options, html_options=nil)
1020
no_wrapper = options[:wrapper] == false
1121

1222
options = form_group_builder_options(options, method)
@@ -18,11 +28,7 @@ def form_group_builder(method, options, html_options=nil, &)
1828
:hide_label, :skip_required, :label_as_placeholder, :wrapper_class, :wrapper
1929
)
2030

21-
if no_wrapper
22-
yield
23-
else
24-
form_group(method, form_group_options, &)
25-
end
31+
yield(form_group_options, no_wrapper)
2632
end
2733

2834
def form_group_builder_options(options, method)

lib/bootstrap_form/inputs/inputs_collection.rb

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ module InputsCollection
77

88
private
99

10-
def inputs_collection(name, collection, value, text, options={})
10+
def inputs_collection(name, collection, value, text, options={}, &)
1111
options[:label] ||= { class: group_label_class(field_layout(options)) }
1212
options[:inline] ||= layout_inline?(options[:layout])
1313

14+
return group_inputs_collection(name, collection, value, text, options, &) if BootstrapForm.config.group_around_collections
15+
1416
form_group_builder(name, options) do
1517
inputs = ActiveSupport::SafeBuffer.new
1618

@@ -56,6 +58,63 @@ def form_group_collection_input_checked?(checked, obj, input_value)
5658
checked == input_value || Array(checked).try(:include?, input_value) ||
5759
checked == obj || Array(checked).try(:include?, obj)
5860
end
61+
62+
def group_inputs_collection(name, collection, value, text, options={}, &)
63+
group_builder(name, options) do
64+
render_collection(name, collection, value, text, options, &)
65+
end
66+
end
67+
68+
def render_collection(name, collection, value, text, options={}, &)
69+
inputs = ActiveSupport::SafeBuffer.new
70+
71+
collection.each_with_index do |obj, i|
72+
input_value = value.respond_to?(:call) ? value.call(obj) : obj.send(value)
73+
input_options = form_group_collection_input_options(options, text, obj, i, input_value, collection)
74+
inputs << yield(name, input_value, input_options)
75+
end
76+
77+
inputs
78+
end
79+
80+
def group_builder(method, options, html_options=nil, &)
81+
form_group_builder_wrapper(method, options, html_options) do |form_group_options, no_wrapper|
82+
if no_wrapper
83+
yield
84+
else
85+
field_group(method, form_group_options, &)
86+
end
87+
end
88+
end
89+
90+
def field_group(name, options, &)
91+
options[:class] = form_group_classes(options)
92+
93+
tag.div(
94+
**options.except(
95+
:add_control_col_class, :append, :control_col, :floating, :help, :icon, :id,
96+
:input_group_class, :label, :label_col, :layout, :prepend
97+
),
98+
aria: { labelledby: options[:id] || default_id(name) },
99+
role: :group
100+
) do
101+
group_label_div = generate_group_label_div(name, options)
102+
prepare_label_options(options[:id], name, options[:label], options[:label_col], options[:layout])
103+
form_group_content(group_label_div, generate_help(name, options[:help]), options, &)
104+
end
105+
end
106+
107+
def generate_group_label_div(name, options)
108+
group_label_div_class = options.dig(:label, :class) || "form-label"
109+
id = options[:id] || default_id(name)
110+
111+
tag.div(
112+
**{ class: group_label_div_class }.compact,
113+
id:
114+
) { label_text(name, options.dig(:label, :text)) }
115+
end
116+
117+
def default_id(name) = raw("#{object_name}_#{name}") # rubocop:disable Rails/OutputSafety
59118
end
60119
end
61120
end

0 commit comments

Comments
 (0)