From 7378d7665c02f4e5258a73417f96b82aaa8d326c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Rodrigo?= Date: Sat, 11 Jan 2014 21:59:34 +0100 Subject: [PATCH 1/5] Allow customization of default class --- README.markdown | 11 +++++++++++ lib/twitter_bootstrap_form_for/form_builder.rb | 12 ++++++++++-- lib/twitter_bootstrap_form_for/form_helpers.rb | 4 ++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/README.markdown b/README.markdown index 5597e3b..efe25d8 100644 --- a/README.markdown +++ b/README.markdown @@ -89,6 +89,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]) diff --git a/lib/twitter_bootstrap_form_for/form_builder.rb b/lib/twitter_bootstrap_form_for/form_builder.rb index 5d7b326..d2e1f53 100644 --- a/lib/twitter_bootstrap_form_for/form_builder.rb +++ b/lib/twitter_bootstrap_form_for/form_builder.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/twitter_bootstrap_form_for/form_helpers.rb b/lib/twitter_bootstrap_form_for/form_helpers.rb index da3e55a..b263eb3 100644 --- a/lib/twitter_bootstrap_form_for/form_helpers.rb +++ b/lib/twitter_bootstrap_form_for/form_helpers.rb @@ -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'} From 2471fe69fca49e7fe511d67dbd40ced40a95daae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Rodrigo?= Date: Sun, 26 Jan 2014 14:42:20 +0100 Subject: [PATCH 2/5] Support input-group into options --- README.markdown | 3 +++ lib/twitter_bootstrap_form_for/form_builder.rb | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index efe25d8..43dc4b2 100644 --- a/README.markdown +++ b/README.markdown @@ -46,6 +46,9 @@ Formtastic does), it only lightly wraps the existing Rails form tag helpers. = 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' diff --git a/lib/twitter_bootstrap_form_for/form_builder.rb b/lib/twitter_bootstrap_form_for/form_builder.rb index d2e1f53..7039112 100644 --- a/lib/twitter_bootstrap_form_for/form_builder.rb +++ b/lib/twitter_bootstrap_form_for/form_builder.rb @@ -137,7 +137,18 @@ def inline(label = nil, &block) classes << ('input-' + options.delete(:add_on).to_s) if options[:add_on] 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') { + template.concat tag + template.concat template.content_tag(:span, :class => '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') } From 85fc69857efd701771c05bf24bcfd5d656d700c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Rodrigo?= Date: Sun, 26 Jan 2014 14:50:19 +0100 Subject: [PATCH 3/5] add-ons, append by default --- README.markdown | 4 +++- lib/twitter_bootstrap_form_for/form_builder.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index 43dc4b2..cdf2739 100644 --- a/README.markdown +++ b/README.markdown @@ -42,7 +42,9 @@ 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 @ diff --git a/lib/twitter_bootstrap_form_for/form_builder.rb b/lib/twitter_bootstrap_form_for/form_builder.rb index 7039112..8069de1 100644 --- a/lib/twitter_bootstrap_form_for/form_builder.rb +++ b/lib/twitter_bootstrap_form_for/form_builder.rb @@ -134,7 +134,7 @@ 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') input_group = options.delete(:input_group) From 8d774e449d146afcfac4c0e72d9c970ab3b372dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Rodrigo?= Date: Tue, 26 May 2015 15:34:05 +0200 Subject: [PATCH 4/5] Use select2-bootstrap-append on input-group when select, used by select2-bootstrap-theme --- lib/twitter_bootstrap_form_for/form_builder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/twitter_bootstrap_form_for/form_builder.rb b/lib/twitter_bootstrap_form_for/form_builder.rb index 8069de1..7a799c1 100644 --- a/lib/twitter_bootstrap_form_for/form_builder.rb +++ b/lib/twitter_bootstrap_form_for/form_builder.rb @@ -140,7 +140,7 @@ def inline(label = nil, &block) input_group = options.delete(:input_group) tag = super(attribute, *(args << options)) if input_group - template.concat template.content_tag(:div, :class => '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-addon') { template.concat input_group From 5ec6e134eda8742009310d58f80adb88de3de56e Mon Sep 17 00:00:00 2001 From: Fabien Girard Date: Wed, 16 Mar 2016 17:52:56 +0100 Subject: [PATCH 5/5] Support input-group-btn --- lib/twitter_bootstrap_form_for/form_builder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/twitter_bootstrap_form_for/form_builder.rb b/lib/twitter_bootstrap_form_for/form_builder.rb index 7a799c1..49c0d82 100644 --- a/lib/twitter_bootstrap_form_for/form_builder.rb +++ b/lib/twitter_bootstrap_form_for/form_builder.rb @@ -142,7 +142,7 @@ def inline(label = nil, &block) 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-addon') { + template.concat template.content_tag(:span, :class => (input_group =~ /class[^=]*=['"]*[^'"=>]*btn[ '"]/) ? 'input-group-btn' : 'input-group-addon') { template.concat input_group } }