Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default class, input-group options #90

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ Formtastic does), it only lightly wraps the existing Rails form tag helpers.
/ a field with a custom label
= user.password_field :password_confirmation, 'Confirm Password'

/ input fields with custom add-ons
/ input fields with custom add-ons, append by default
= user.text_field :twitter_id, 'Twitter', :class => 'medium' do
%span.add-on @
= user.text_field :twitter_id, 'Twitter', :class => 'medium', :add_on => :prepend do
%span.add-on @

/ input fields with input-group
= user.text_field :twitter_id, 'Twitter', :class => 'medium', :input-group => '€'

/ select fields now have the second parameter as a label
= user.date_select :born_on, 'Born on', {}, :class => 'small'

Expand Down Expand Up @@ -89,6 +94,17 @@ simple:
* the last options hash accepts an `:add_on` key
* if a block is passed, the HTML it outputs is placed immediately after the input

## Configuration ##

Default class applied on label and div can be customized. Setup a file `config/initializers/twitter_bootstrap_form_for.rb` e.g.:

```ruby
TwitterBootstrapFormFor::FormBuilder.label_class = 'col-md-2 control-label'
TwitterBootstrapFormFor::FormBuilder.div_class = 'col-md-6'
TwitterBootstrapFormFor::FormBuilder.div_labelless_class = 'col-md-offset-2 col-md-6'
TwitterBootstrapFormFor::FormBuilder.action_class = 'col-md-offset-2 col-md-6'
```

## Known Bugs ##

- inline fields don't receive error markup ([issue #28])
Expand Down
27 changes: 23 additions & 4 deletions lib/twitter_bootstrap_form_for/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
require 'action_view/helpers'

class TwitterBootstrapFormFor::FormBuilder < ActionView::Helpers::FormBuilder

cattr_accessor :label_class, :div_class, :div_labelless_class, :action_class

self.label_class = 'col-lg-2 control-label'
self.div_class = 'col-lg-10'
self.div_labelless_class = 'col-lg-offset-2 col-lg-10'
self.action_class = 'col-lg-offset-2 col-lg-10'

include TwitterBootstrapFormFor::FormHelpers

attr_reader :template
Expand Down Expand Up @@ -57,7 +65,7 @@ def toggles(*args, &block)
template.concat self.label(nil, label, :class => label_class) if label.present?

if @options[:layout] == :horizontal
html_class = label.present? ? @options[:default_div_class] : 'col-lg-offset-2 col-lg-10'
html_class = label.present? ? @options[:default_div_class] : self.div_labelless_class
end
template.concat template.content_tag(:div, :class => html_class) { block.call }
end
Expand All @@ -69,7 +77,7 @@ def toggles(*args, &block)
def actions(*args, &block)
if @options[:layout] == :horizontal
options = args.extract_options!
options[:class] ||= 'col-lg-offset-2 col-lg-10'
options[:class] ||= self.action_class
self.div_wrapper(:div, :class => 'form-group') do
template.content_tag(:div, :class => options[:class], &block)
end
Expand Down Expand Up @@ -126,10 +134,21 @@ def inline(label = nil, &block)
elsif @options[:default_div_class].present?
classes << @options[:default_div_class]
end
classes << ('input-' + options.delete(:add_on).to_s) if options[:add_on]
classes << ('input-' + (options.delete(:add_on) || :append).to_s) if block.present?
template.concat template.content_tag(:div, :class => classes.join(' ')) {
block.call if block.present? and classes.include?('input-prepend')
template.concat super(attribute, *(args << options))
input_group = options.delete(:input_group)
tag = super(attribute, *(args << options))
if input_group
template.concat template.content_tag(:div, :class => 'input-group' + (input == :select ? ' select2-bootstrap-append' : '')) {
template.concat tag
template.concat template.content_tag(:span, :class => (input_group =~ /class[^=]*=['"]*[^'"=>]*btn[ '"]/) ? 'input-group-btn' : 'input-group-addon') {
template.concat input_group
}
}
else
template.concat tag
end
template.concat error_span(attribute)
block.call if block.present? and classes.include?('input-append')
}
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter_bootstrap_form_for/form_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module TwitterBootstrapFormFor::FormHelpers
raise "Specified form layout #{options[:layout].to_s} is invalid. Must be one of :vertical, :horizontal, or :inline." unless [:vertical, :horizontal, :inline].include?(options[:layout])
options[:default_toggle_style] = :stacked
if options[:layout] == :horizontal
options[:default_div_class] ||= 'col-lg-10'
options[:default_label_class] ||= 'col-lg-2 control-label'
options[:default_div_class] ||= TwitterBootstrapFormFor::FormBuilder.div_class
options[:default_label_class] ||= TwitterBootstrapFormFor::FormBuilder.label_class
end
if options[:html].nil?
options[:html] = {:role => 'form'}
Expand Down