Skip to content

Commit 06ca84a

Browse files
Add support for append and prepend on collection_select (fixes #428) (#641)
* Fix rubocop offenses * Add support for append and prepend on collection_select (fixes #428) * Add support for append and prepend on grouped_collection_select (fixes #428)
1 parent 5755a39 commit 06ca84a

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

demo/test/system/bootstrap_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class BootstrapTest < ApplicationSystemTestCase
7171
#{html}
7272
MD
7373
end
74-
augmented_readme.gsub!(/127.0.0.1:\d+/,'test.host')
74+
augmented_readme.gsub!(/127.0.0.1:\d+/, "test.host")
7575
File.write(File.expand_path("../../../README.md", __dir__), augmented_readme)
7676
end
7777

lib/bootstrap_form/inputs/collection_select.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module CollectionSelect
1212
def collection_select_with_bootstrap(method, collection, value_method, text_method, options={}, html_options={})
1313
html_options = html_options.reverse_merge(control_class: "form-select")
1414
form_group_builder(method, options, html_options) do
15-
input_with_error(method) do
15+
prepend_and_append_input(method, options) do
1616
collection_select_without_bootstrap(method, collection, value_method, text_method, options, html_options)
1717
end
1818
end

lib/bootstrap_form/inputs/grouped_collection_select.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def grouped_collection_select_with_bootstrap(method, collection, group_method,
1414
option_value_method, options={}, html_options={})
1515
html_options = html_options.reverse_merge(control_class: "form-select")
1616
form_group_builder(method, options, html_options) do
17-
input_with_error(method) do
17+
prepend_and_append_input(method, options) do
1818
grouped_collection_select_without_bootstrap(method, collection, group_method,
1919
group_label_method, option_key_method,
2020
option_value_method, options, html_options)

test/bootstrap_selects_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,24 @@ def options_range(start: 1, stop: 31, selected: nil, months: false)
229229
@builder.collection_select(:status, [], :id, :name, { prompt: "Please Select" }, class: "my-select")
230230
end
231231

232+
test "collection_selects with addons are wrapped correctly" do
233+
expected = <<~HTML
234+
<div class="mb-3">
235+
<label class="form-label" for="user_status">Status</label>
236+
<div class="input-group">
237+
<span class="input-group-text">Before</span>
238+
<select class="form-select" id="user_status" name="user[status]">
239+
<option value="">Please Select</option>
240+
</select>
241+
<span class="input-group-text">After</span>
242+
</div>
243+
</div>
244+
HTML
245+
assert_equivalent_xml expected,
246+
@builder.collection_select(:status, [], :id, :name,
247+
{ prepend: "Before", append: "After", prompt: "Please Select" })
248+
end
249+
232250
test "grouped_collection_selects are wrapped correctly" do
233251
expected = <<~HTML
234252
<div class="mb-3">
@@ -293,6 +311,24 @@ def options_range(start: 1, stop: 31, selected: nil, months: false)
293311
class: "my-select")
294312
end
295313

314+
test "grouped_collection_selects with addons are wrapped correctly" do
315+
expected = <<~HTML
316+
<div class="mb-3">
317+
<label class="form-label" for="user_status">Status</label>
318+
<div class="input-group">
319+
<span class="input-group-text">Before</span>
320+
<select class="form-select" id="user_status" name="user[status]">
321+
<option value="">Please Select</option>
322+
</select>
323+
<span class="input-group-text">After</span>
324+
</div>
325+
</div>
326+
HTML
327+
assert_equivalent_xml expected,
328+
@builder.grouped_collection_select(:status, [], :last, :first, :to_s, :to_s,
329+
{ prepend: "Before", append: "After", prompt: "Please Select" })
330+
end
331+
296332
test "date selects are wrapped correctly" do
297333
travel_to(Time.utc(2012, 2, 3)) do
298334
expected = <<~HTML

0 commit comments

Comments
 (0)