Skip to content

Commit 3913793

Browse files
committed
DRY up the code a bit and clarify some method names
1 parent 1bfe0f7 commit 3913793

File tree

2 files changed

+36
-46
lines changed

2 files changed

+36
-46
lines changed

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: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,13 @@ module InputsCollection
88
private
99

1010
def inputs_collection(name, collection, value, text, options={}, &)
11-
return group_inputs_collection(name, collection, value, text, options, &) if BootstrapForm.config.group_around_collections
12-
1311
options[:label] ||= { class: group_label_class(field_layout(options)) }
1412
options[:inline] ||= layout_inline?(options[:layout])
1513

16-
form_group_builder(name, options) do
17-
inputs = ActiveSupport::SafeBuffer.new
18-
19-
collection.each_with_index do |obj, i|
20-
input_value = value.respond_to?(:call) ? value.call(obj) : obj.send(value)
21-
input_options = form_group_collection_input_options(options, text, obj, i, input_value, collection)
22-
inputs << yield(name, input_value, input_options)
23-
end
14+
return group_inputs_collection(name, collection, value, text, options, &) if BootstrapForm.config.group_around_collections
2415

25-
inputs
16+
form_group_builder(name, options) do
17+
render_collection(name, collection, value, text, options, &)
2618
end
2719
end
2820

@@ -62,38 +54,30 @@ def form_group_collection_input_checked?(checked, obj, input_value)
6254
end
6355

6456
def group_inputs_collection(name, collection, value, text, options={}, &)
65-
options[:label] ||= { class: group_label_class(field_layout(options)) }
66-
options[:inline] ||= layout_inline?(options[:layout])
67-
6857
group_builder(name, options) do
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
58+
render_collection(name, collection, value, text, options, &)
7859
end
7960
end
8061

81-
def group_builder(method, options, html_options=nil, &)
82-
no_wrapper = options[:wrapper] == false
83-
84-
options = form_group_builder_options(options, method)
62+
def render_collection(name, collection, value, text, options={}, &)
63+
inputs = ActiveSupport::SafeBuffer.new
8564

86-
form_group_options = form_group_opts(options, form_group_css_options(method, html_options.try(:symbolize_keys!), options))
65+
collection.each_with_index do |obj, i|
66+
input_value = value.respond_to?(:call) ? value.call(obj) : obj.send(value)
67+
input_options = form_group_collection_input_options(options, text, obj, i, input_value, collection)
68+
inputs << yield(name, input_value, input_options)
69+
end
8770

88-
options.except!(
89-
:help, :icon, :label_col, :control_col, :add_control_col_class, :layout, :skip_label, :label, :label_class,
90-
:hide_label, :skip_required, :label_as_placeholder, :wrapper_class, :wrapper
91-
)
71+
inputs
72+
end
9273

93-
if no_wrapper
94-
yield
95-
else
96-
field_group(method, form_group_options, &)
74+
def group_builder(method, options, html_options=nil, &)
75+
form_group_builder_wrapper(method, options, html_options) do |form_group_options, no_wrapper|
76+
if no_wrapper
77+
yield
78+
else
79+
field_group(method, form_group_options, &)
80+
end
9781
end
9882
end
9983

@@ -108,18 +92,18 @@ def field_group(name, options, &)
10892
aria: { labelledby: options[:id] || default_id(name) },
10993
role: :group
11094
) do
111-
group_label = generate_group_label(name, options)
95+
group_label_div = generate_group_label_div(name, options)
11296
prepare_label_options(options[:id], name, options[:label], options[:label_col], options[:layout])
113-
form_group_content(group_label, generate_help(name, options[:help]), options, &)
97+
form_group_content(group_label_div, generate_help(name, options[:help]), options, &)
11498
end
11599
end
116100

117-
def generate_group_label(name, options)
118-
group_label_class = options.dig(:label, :class) || "form-label"
101+
def generate_group_label_div(name, options)
102+
group_label_div_class = options.dig(:label, :class) || "form-label"
119103
id = options[:id] || default_id(name)
120104

121105
tag.div(
122-
**{ class: group_label_class }.compact,
106+
**{ class: group_label_div_class }.compact,
123107
id:
124108
) { label_text(name, options.dig(:label, :text)) }
125109
end

0 commit comments

Comments
 (0)