Skip to content

Commit

Permalink
fixing bug when both input and append text are specified
Browse files Browse the repository at this point in the history
  • Loading branch information
potenza committed Dec 8, 2013
1 parent 9ad6fe7 commit a7eb01b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
24 changes: 9 additions & 15 deletions lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,8 @@ def initialize(object_name, object, template, options, proc=nil)
form_group(name, label: { text: label, class: label_class }, help: help) do
options[:class] = "form-control #{options[:class]}".rstrip
args << options.except(:prepend, :append)
element = super(name, *args)

if prepend = options.delete(:prepend)
element = content_tag(:div, class: 'input-group') do
content_tag(:span, prepend, class: 'input-group-addon') + element
end
end

if append = options.delete(:append)
element = content_tag(:div, class: 'input-group') do
element + content_tag(:span, append, class: 'input-group-addon')
end
end

element
input = super(name, *args)
prepend_and_append_input(input, options[:prepend], options[:append])
end
end
end
Expand Down Expand Up @@ -121,6 +108,13 @@ def has_error?(name)
object.respond_to?(:errors) && !(name.nil? || object.errors[name].empty?)
end

def prepend_and_append_input(input, prepend, append)
input = content_tag(:span, prepend, class: 'input-group-addon') + input if prepend
input << content_tag(:span, append, class: 'input-group-addon') if append
input = content_tag(:div, input, class: 'input-group') if prepend || append
input
end

def generate_label(name, options)
if options
options[:class] = "#{options[:class]} #{left_class}".lstrip if horizontal?
Expand Down
5 changes: 5 additions & 0 deletions test/bootstrap_form_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ def setup
assert_equal expected, @builder.text_field(:email, append: '.00')
end

test "adding both prepend and append text" do
expected = %{<div class="form-group"><label for="user_email">Email</label><div class="input-group"><span class="input-group-addon">$</span><input class="form-control" id="user_email" name="user[email]" type="text" value="[email protected]" /><span class="input-group-addon">.00</span></div></div>}
assert_equal expected, @builder.text_field(:email, prepend: '$', append: '.00')
end

test "help messages for default forms" do
expected = %{<div class="form-group"><label for="user_email">Email</label><input class="form-control" id="user_email" name="user[email]" type="text" value="[email protected]" /><span class="help-block">This is required</span></div>}
assert_equal expected, @builder.text_field(:email, help: 'This is required')
Expand Down

0 comments on commit a7eb01b

Please sign in to comment.