Skip to content

Commit 0cfd15f

Browse files
committed
Merge pull request #9 from carloslopes/control-group-help
Help tag rendered by #control_group method
2 parents 0cd5ec2 + 4aae6c3 commit 0cfd15f

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
bootstrap_form (0.2.4)
4+
bootstrap_form (0.3.0)
55

66
GEM
77
remote: http://rubygems.org/

lib/bootstrap_form/form_builder.rb

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ def initialize(object_name, object, template, options, proc)
1919
define_method(method_name) do |name, *args|
2020
options = args.extract_options!.symbolize_keys!
2121

22-
control_group(name, label: { text: options[:label] }) do
23-
help = object.errors[name].any? ? object.errors[name].join(', ') : options[:help]
24-
help = content_tag(@help_tag, class: @help_css) { help } if help
22+
label = options.delete(:label)
23+
help = options.delete(:help)
2524

26-
args << options.except(:label, :help, :prepend)
27-
element = super(name, *args) + help
25+
control_group(name, label: { text: label }, help: help) do
26+
27+
args << options.except(:prepend)
28+
element = super(name, *args)
2829

2930
if prepend = options.delete(:prepend)
3031
element = content_tag(:div, class: 'input-prepend') do
@@ -60,21 +61,31 @@ def radio_button(name, value, *args)
6061
end
6162

6263
def control_group(name = nil, options = {}, &block)
64+
has_name = !(name.nil? || object.errors[name].empty?)
65+
6366
options[:class] ||= 'control-group'
64-
options[:class] << ' error' if name && object.errors[name].any?
67+
options[:class] << ' error' if has_name
68+
69+
label = options.delete(:label)
70+
_help = options.delete(:help)
6571

66-
content_tag(:div, options.except(:label)) do
72+
content_tag(:div, options) do
6773
html = ''
6874

69-
if attrs = options.delete(:label)
70-
attrs[:class] ||= 'control-label'
71-
attrs[:for] ||= '' if name.nil?
75+
if label
76+
label[:class] ||= 'control-label'
77+
label[:for] ||= '' if name.nil?
7278

73-
html << label(name, attrs[:text], attrs.except(:text))
79+
html << label(name, label[:text], label.except(:text))
7480
end
7581

7682
html << content_tag(:div, class: 'controls') do
77-
block.call.html_safe
83+
controls = block.call
84+
85+
help = has_name ? object.errors[name].join(', ') : _help
86+
controls << content_tag(@help_tag, help, class: @help_css) if help
87+
88+
controls.html_safe
7889
end
7990

8091
html.html_safe

test/bootstrap_form_test.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,24 @@ def setup
198198
assert_equal expected, output
199199
end
200200

201-
test 'control_group renders the "error" class corrrectly when object is invalid' do
201+
test 'control_group renders the :help corrrectly' do
202+
output = @builder.control_group nil, help: 'Foobar' do
203+
'<span>custom control here</span>'
204+
end
205+
206+
expected = %{<div class="control-group"><div class="controls"><span>custom control here</span><span class="help-inline">Foobar</span></div></div>}
207+
assert_equal expected, output
208+
end
209+
210+
test 'control_group renders the "error" class and message corrrectly when object is invalid' do
202211
@user.email = nil
203212
@user.valid?
204213

205214
output = @builder.control_group :email do
206215
'<span>custom control here</span>'
207216
end
208217

209-
expected = %{<div class="control-group error"><div class="controls"><span>custom control here</span></div></div>}
218+
expected = %{<div class="control-group error"><div class="controls"><span>custom control here</span><span class="help-inline">can't be blank, is too short (minimum is 5 characters)</span></div></div>}
210219
assert_equal expected, output
211220
end
212221

0 commit comments

Comments
 (0)