Skip to content

Commit

Permalink
Merge pull request bootstrap-ruby#8 from carloslopes/multiple-selects…
Browse files Browse the repository at this point in the history
…-issue

Multiple selects issue
  • Loading branch information
potenza committed Jan 25, 2014
2 parents 2517012 + 5ecab77 commit 5053cbb
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Then:

`bundle`

Then require the CSS on your `application.css` file:

`//= require rails_bootstrap_forms`

## Usage

To get started, just use the **Rails Bootstrap Forms** form helper. Here's an example:
Expand Down Expand Up @@ -263,6 +267,22 @@ You can also create a static control that isn't based on a model attribute:
<% end %>
```

### Date helpers

The multiple selects that the date and time helpers (`date_select`,
`time_select`, `datetime_select`) generate are wrapped inside a
`div.rails-bootstrap-forms-multiple-selects` tag. This is because Boostrap
automatically stylizes ours controls as `block`s. This wrapper fix this defining
these selects as `inline-block`s.

```erb
<%= f.date_select :birthdate, { label: 'Your day' }, { style: 'width: 10%' } %>
```

Note that in the example above we passed `width: 10%` to be the style of the
generated selects. This is necessary because even the elements being
`inline-block`s they must have a width which enable them to stay side by side.

### Validation Errors

When a validation error is triggered, the field will be outlined and the error
Expand Down
8 changes: 8 additions & 0 deletions app/assets/stylesheets/rails_bootstrap_forms.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.rails-bootstrap-forms-date-select,
.rails-bootstrap-forms-time-select,
.rails-bootstrap-forms-datetime-select {
select {
display: inline-block;
width: 10%; /* for older browsers without nth-child support */
}
}
4 changes: 4 additions & 0 deletions lib/bootstrap_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
require 'bootstrap_form/helper'

module BootstrapForm
module Rails
class Engine < ::Rails::Engine
end
end
end

ActionView::Base.send :include, BootstrapForm::Helper
26 changes: 23 additions & 3 deletions lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ class FormBuilder < ActionView::Helpers::FormBuilder

FORM_HELPERS = %w{text_field password_field text_area file_field
number_field email_field telephone_field phone_field url_field
select collection_select date_select time_select datetime_select}
select collection_select}

DATE_HELPERS = %w{date_select time_select datetime_select}

delegate :content_tag, to: :@template
delegate :capture, to: :@template
Expand Down Expand Up @@ -34,6 +36,22 @@ def initialize(object_name, object, template, options, proc=nil)
end
end

DATE_HELPERS.each do |method_name|
define_method(method_name) do |name, options = {}, html_options = {}|
options.symbolize_keys!
html_options.symbolize_keys!

label = options.delete(:label)
label_class = hide_class if options.delete(:hide_label)
help = options.delete(:help)

form_group(name, label: { text: label, class: label_class }, help: help) do
html_options[:class] = "form-control #{html_options[:class]}".rstrip
content_tag(:div, super(name, options, html_options), class: control_specific_class(method_name))
end
end
end

def check_box(name, options = {}, checked_value = '1', unchecked_value = '0')
options = options.symbolize_keys!

Expand Down Expand Up @@ -136,8 +154,6 @@ def normalize_args!(method_name, args)
args << {} while args.length < 3
elsif method_name == "collection_select"
args << {} while args.length < 5
elsif method_name =~ /_select/
args << {} while args.length < 2
end
end

Expand All @@ -161,6 +177,10 @@ def static_class
"form-control-static"
end

def control_specific_class(method)
"rails-bootstrap-forms-#{method.gsub(/_/, '-')}"
end

def has_error?(name)
object.respond_to?(:errors) && !(name.nil? || object.errors[name].empty?)
end
Expand Down
Loading

0 comments on commit 5053cbb

Please sign in to comment.