Skip to content

Commit

Permalink
Add support for append and prepend on collection_select (fixes #428) (#…
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
judylovesruby authored Jun 1, 2022
1 parent 5755a39 commit 06ca84a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion demo/test/system/bootstrap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class BootstrapTest < ApplicationSystemTestCase
#{html}
MD
end
augmented_readme.gsub!(/127.0.0.1:\d+/,'test.host')
augmented_readme.gsub!(/127.0.0.1:\d+/, "test.host")
File.write(File.expand_path("../../../README.md", __dir__), augmented_readme)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bootstrap_form/inputs/collection_select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module CollectionSelect
def collection_select_with_bootstrap(method, collection, value_method, text_method, options={}, html_options={})
html_options = html_options.reverse_merge(control_class: "form-select")
form_group_builder(method, options, html_options) do
input_with_error(method) do
prepend_and_append_input(method, options) do
collection_select_without_bootstrap(method, collection, value_method, text_method, options, html_options)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/bootstrap_form/inputs/grouped_collection_select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def grouped_collection_select_with_bootstrap(method, collection, group_method,
option_value_method, options={}, html_options={})
html_options = html_options.reverse_merge(control_class: "form-select")
form_group_builder(method, options, html_options) do
input_with_error(method) do
prepend_and_append_input(method, options) do
grouped_collection_select_without_bootstrap(method, collection, group_method,
group_label_method, option_key_method,
option_value_method, options, html_options)
Expand Down
36 changes: 36 additions & 0 deletions test/bootstrap_selects_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,24 @@ def options_range(start: 1, stop: 31, selected: nil, months: false)
@builder.collection_select(:status, [], :id, :name, { prompt: "Please Select" }, class: "my-select")
end

test "collection_selects with addons are wrapped correctly" do
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_status">Status</label>
<div class="input-group">
<span class="input-group-text">Before</span>
<select class="form-select" id="user_status" name="user[status]">
<option value="">Please Select</option>
</select>
<span class="input-group-text">After</span>
</div>
</div>
HTML
assert_equivalent_xml expected,
@builder.collection_select(:status, [], :id, :name,
{ prepend: "Before", append: "After", prompt: "Please Select" })
end

test "grouped_collection_selects are wrapped correctly" do
expected = <<~HTML
<div class="mb-3">
Expand Down Expand Up @@ -293,6 +311,24 @@ def options_range(start: 1, stop: 31, selected: nil, months: false)
class: "my-select")
end

test "grouped_collection_selects with addons are wrapped correctly" do
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_status">Status</label>
<div class="input-group">
<span class="input-group-text">Before</span>
<select class="form-select" id="user_status" name="user[status]">
<option value="">Please Select</option>
</select>
<span class="input-group-text">After</span>
</div>
</div>
HTML
assert_equivalent_xml expected,
@builder.grouped_collection_select(:status, [], :last, :first, :to_s, :to_s,
{ prepend: "Before", append: "After", prompt: "Please Select" })
end

test "date selects are wrapped correctly" do
travel_to(Time.utc(2012, 2, 3)) do
expected = <<~HTML
Expand Down

0 comments on commit 06ca84a

Please sign in to comment.