Skip to content

Commit 941edb9

Browse files
committed
Merge pull request bootstrap-ruby#229 from Stellenticket/select-block
Allow to pass block to select helper in Rails >= 4.1.0
2 parents 3955276 + af12c5d commit 941edb9

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Bugfixes:
77
Features:
88

99
- Allow primary button classes to be overridden (#183, @tanimichi)
10+
- Allow to pass a block to select helper in Rails >= 4.1.0 (#229, @doits)
1011

1112
## 2.3.0 (2015-02-17)
1213

lib/bootstrap_form/form_builder.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,17 @@ def file_field_with_bootstrap(name, options = {})
6666

6767
alias_method_chain :file_field, :bootstrap
6868

69-
def select_with_bootstrap(method, choices, options = {}, html_options = {})
70-
form_group_builder(method, options, html_options) do
71-
select_without_bootstrap(method, choices, options, html_options)
69+
if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new("4.1.0")
70+
def select_with_bootstrap(method, choices = nil, options = {}, html_options = {}, &block)
71+
form_group_builder(method, options, html_options) do
72+
select_without_bootstrap(method, choices, options, html_options, &block)
73+
end
74+
end
75+
else
76+
def select_with_bootstrap(method, choices, options = {}, html_options = {})
77+
form_group_builder(method, options, html_options) do
78+
select_without_bootstrap(method, choices, options, html_options)
79+
end
7280
end
7381
end
7482

test/bootstrap_selects_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ def setup
3232
assert_equal expected, @builder.select(:status, [['activated', 1], ['blocked', 2]], { prompt: "Please Select" }, class: "my-select")
3333
end
3434

35+
if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new("4.1.0")
36+
test "selects with block use block as content" do
37+
expected = %{<div class="form-group"><label class="control-label" for="user_status">Status</label><select class="form-control" name="user[status]" id="user_status"><option>Option 1</option><option>Option 2</option></select></div>}
38+
select = @builder.select(:status) do
39+
content_tag(:option) { 'Option 1' } +
40+
content_tag(:option) { 'Option 2' }
41+
end
42+
assert_equal expected, select
43+
end
44+
end
45+
3546
test "selects render labels properly" do
3647
expected = %{<div class="form-group"><label class="control-label" for="user_status">User Status</label><select class="form-control" id="user_status" name="user[status]"><option value="1">activated</option>\n<option value="2">blocked</option></select></div>}
3748
assert_equal expected, @builder.select(:status, [['activated', 1], ['blocked', 2]], label: "User Status")

0 commit comments

Comments
 (0)