Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
bootstrap_form (0.2.4)
bootstrap_form (0.3.0)

GEM
remote: http://rubygems.org/
Expand Down
35 changes: 23 additions & 12 deletions lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ def initialize(object_name, object, template, options, proc)
define_method(method_name) do |name, *args|
options = args.extract_options!.symbolize_keys!

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

args << options.except(:label, :help, :prepend)
element = super(name, *args) + help
control_group(name, label: { text: label }, help: help) do

args << options.except(:prepend)
element = super(name, *args)

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

def control_group(name = nil, options = {}, &block)
has_name = !(name.nil? || object.errors[name].empty?)

options[:class] ||= 'control-group'
options[:class] << ' error' if name && object.errors[name].any?
options[:class] << ' error' if has_name

label = options.delete(:label)
_help = options.delete(:help)

content_tag(:div, options.except(:label)) do
content_tag(:div, options) do
html = ''

if attrs = options.delete(:label)
attrs[:class] ||= 'control-label'
attrs[:for] ||= '' if name.nil?
if label
label[:class] ||= 'control-label'
label[:for] ||= '' if name.nil?

html << label(name, attrs[:text], attrs.except(:text))
html << label(name, label[:text], label.except(:text))
end

html << content_tag(:div, class: 'controls') do
block.call.html_safe
controls = block.call

help = has_name ? object.errors[name].join(', ') : _help
controls << content_tag(@help_tag, help, class: @help_css) if help

controls.html_safe
end

html.html_safe
Expand Down
13 changes: 11 additions & 2 deletions test/bootstrap_form_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,24 @@ def setup
assert_equal expected, output
end

test 'control_group renders the "error" class corrrectly when object is invalid' do
test 'control_group renders the :help corrrectly' do
output = @builder.control_group nil, help: 'Foobar' do
'<span>custom control here</span>'
end

expected = %{<div class="control-group"><div class="controls"><span>custom control here</span><span class="help-inline">Foobar</span></div></div>}
assert_equal expected, output
end

test 'control_group renders the "error" class and message corrrectly when object is invalid' do
@user.email = nil
@user.valid?

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

expected = %{<div class="control-group error"><div class="controls"><span>custom control here</span></div></div>}
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>}
assert_equal expected, output
end

Expand Down