Skip to content

Commit

Permalink
Omit form-control class from file_field
Browse files Browse the repository at this point in the history
The form-control class is not used for `<input type=file>` in the
Bootstrap examples. Adding this class introduces an undesirable
border to the choose file control.

This commit introduces an `:omit_control_class` option to the
general-purpose field builder that is `true` by default for
`file_field`.
  • Loading branch information
mattbrictson committed Feb 20, 2014
1 parent 82cddf9 commit e128ddb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 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:
- control-label class was not being added unless the form was horizontal (@slave2zeros)
- Wrapped date & time helpers inside control-specific css classes to prevent stacking
- i18n fix for checkbox labels (@i10a)
- omit form-control class from file_field (@mbrictson)

Features:

Expand Down
15 changes: 11 additions & 4 deletions lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class FormBuilder < ActionView::Helpers::FormBuilder
range_field search_field telephone_field text_area text_field time_field
url_field week_field}

FIELDS_WITHOUT_CONTROL_CLASS = %{file_field}

DATE_SELECT_HELPERS = %w{date_select time_select datetime_select}

delegate :content_tag, :capture, :concat, to: :@template
Expand All @@ -23,6 +25,9 @@ def initialize(object_name, object, template, options, proc=nil)

FIELD_HELPERS.each do |method_name|
define_method(method_name) do |name, options = {}|
if FIELDS_WITHOUT_CONTROL_CLASS.include?(method_name)
options.reverse_merge!(:omit_control_class => true)
end
form_group_builder(name, options) do
prepend_and_append_input(options) do
super(name, options)
Expand Down Expand Up @@ -154,10 +159,12 @@ def form_group_builder(method, options, html_options = nil)
options.symbolize_keys!
html_options.symbolize_keys! if html_options

if html_options
html_options[:class] = "#{control_class} #{html_options[:class]}".rstrip
else
options[:class] = "#{control_class} #{options[:class]}".rstrip
unless options.delete(:omit_control_class)
if html_options
html_options[:class] = "#{control_class} #{html_options[:class]}".rstrip
else
options[:class] = "#{control_class} #{options[:class]}".rstrip
end
end

label = options.delete(:label)
Expand Down
2 changes: 1 addition & 1 deletion test/bootstrap_form_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def setup
end

test "file fields are wrapped correctly" do
expected = %{<div class="form-group"><label class="control-label" for="user_misc">Misc</label><input class="form-control" id="user_misc" name="user[misc]" type="file" /></div>}
expected = %{<div class="form-group"><label class="control-label" for="user_misc">Misc</label><input id="user_misc" name="user[misc]" type="file" /></div>}
assert_equal expected, @builder.file_field(:misc)
end

Expand Down

0 comments on commit e128ddb

Please sign in to comment.