Skip to content

Commit

Permalink
Fix wrapper handling for helpers with html_options.
Browse files Browse the repository at this point in the history
  • Loading branch information
lcreid authored and mattbrictson committed Jul 4, 2018
1 parent 3032ba0 commit b41295e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Bugfixes

* Your contribution here!
* [#347](https://github.com/bootstrap-ruby/bootstrap_form/issues/347) Fix `wrapper_class` and `wrapper` options for helpers that have `html_options`.
* [#472](https://github.com/bootstrap-ruby/bootstrap_form/pull/472) Use `id` option value as `for` attribute of label for custom checkboxes and radio buttons.


Expand Down
6 changes: 4 additions & 2 deletions lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ def required_attribute?(obj, attribute)

def form_group_builder(method, options, html_options = nil)
options.symbolize_keys!

wrapper_class = options.delete(:wrapper_class)
wrapper_options = options.delete(:wrapper)

html_options.symbolize_keys! if html_options

# Add control_class; allow it to be overridden by :control_class option
Expand All @@ -416,8 +420,6 @@ def form_group_builder(method, options, html_options = nil)

options = convert_form_tag_options(method, options) if acts_like_form_tag

wrapper_class = css_options.delete(:wrapper_class)
wrapper_options = css_options.delete(:wrapper)
help = options.delete(:help)
icon = options.delete(:icon)
label_col = options.delete(:label_col)
Expand Down
52 changes: 52 additions & 0 deletions test/bootstrap_selects_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def options_range(start: 1, stop: 31, selected: nil, months: false)
assert_equivalent_xml expected, @builder.time_zone_select(:misc)
end

test "time zone selects are wrapped correctly with wrapper" do
expected = <<-HTML.strip_heredoc
<div class="form-group none-margin">
<label for="user_misc">Misc</label>
<select class="form-control" id="user_misc" name="user[misc]">#{time_zone_options_for_select}</select>
</div>
HTML
assert_equivalent_xml expected, @builder.time_zone_select(:misc, nil, wrapper: { class: "none-margin" })
end

test "time zone selects are wrapped correctly with error" do
@user.errors.add(:misc, "error for test")
expected = <<-HTML.strip_heredoc
Expand Down Expand Up @@ -165,6 +175,16 @@ def options_range(start: 1, stop: 31, selected: nil, months: false)
assert_equivalent_xml expected, @builder.collection_select(:status, [], :id, :name)
end

test "collection_selects are wrapped correctly with wrapper" do
expected = <<-HTML.strip_heredoc
<div class="form-group none-margin">
<label for="user_status">Status</label>
<select class="form-control" id="user_status" name="user[status]"></select>
</div>
HTML
assert_equivalent_xml expected, @builder.collection_select(:status, [], :id, :name, wrapper: { class: "none-margin" })
end

test "collection_selects are wrapped correctly with error" do
@user.errors.add(:status, "error for test")
expected = <<-HTML.strip_heredoc
Expand Down Expand Up @@ -214,6 +234,16 @@ def options_range(start: 1, stop: 31, selected: nil, months: false)
assert_equivalent_xml expected, @builder.grouped_collection_select(:status, [], :last, :first, :to_s, :to_s)
end

test "grouped_collection_selects are wrapped correctly with wrapper" do
expected = <<-HTML.strip_heredoc
<div class="form-group none-margin">
<label for="user_status">Status</label>
<select class="form-control" id="user_status" name="user[status]"></select>
</div>
HTML
assert_equivalent_xml expected, @builder.grouped_collection_select(:status, [], :last, :first, :to_s, :to_s, wrapper_class: "none-margin")
end

test "grouped_collection_selects are wrapped correctly with error" do
@user.errors.add(:status, "error for test")
expected = <<-HTML.strip_heredoc
Expand Down Expand Up @@ -275,6 +305,28 @@ def options_range(start: 1, stop: 31, selected: nil, months: false)
end
end

test "date selects are wrapped correctly with wrapper class" do
Timecop.freeze(Time.utc(2012, 2, 3)) do
expected = <<-HTML.strip_heredoc
<div class="form-group none-margin">
<label for="user_misc">Misc</label>
<div class="rails-bootstrap-forms-date-select">
<select class="form-control" id="user_misc_1i" name="user[misc(1i)]">
#{options_range(start: 2007, stop: 2017, selected: 2012)}
</select>
<select class="form-control" id="user_misc_2i" name="user[misc(2i)]">
#{options_range(start: 1, stop: 12, selected: 2, months: true)}
</select>
<select class="form-control" id="user_misc_3i" name="user[misc(3i)]">
#{options_range(start: 1, stop: 31, selected: 3)}
</select>
</div>
</div>
HTML
assert_equivalent_xml expected, @builder.date_select(:misc, wrapper_class: "none-margin")
end
end

test "date selects are wrapped correctly with error" do
@user.errors.add(:misc, "error for test")
Timecop.freeze(Time.utc(2012, 2, 3)) do
Expand Down

0 comments on commit b41295e

Please sign in to comment.