From 8765ba72c75b60af4dc84b37695442a4f7b2eafa Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 29 Nov 2011 10:29:27 +0630 Subject: [PATCH] DRY up the label printing --- .../form_builder.rb | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/lib/twitter_bootstrap_form_for/form_builder.rb b/lib/twitter_bootstrap_form_for/form_builder.rb index a020f5b..72ae0d7 100644 --- a/lib/twitter_bootstrap_form_for/form_builder.rb +++ b/lib/twitter_bootstrap_form_for/form_builder.rb @@ -39,11 +39,8 @@ def inputs(legend = nil, options = {}, &block) # inside of here, and will not look correct unless they are. # def toggles(label = nil, &block) - div_wrapper do - template.concat template.content_tag(:label, label) - template.concat template.content_tag(:div, :class => "input") { - template.content_tag(:ul, :class => "inputs-list") { block.call } - } + div_wrapper_with_label do + template.content_tag(:ul, :class => "inputs-list") { block.call } end end @@ -70,18 +67,15 @@ def submit(value = nil, options = {}) # to the supplied block. # def inline(label = nil, &block) - div_wrapper do - template.concat template.content_tag(:label, label) if label.present? - template.concat template.content_tag(:div, :class => 'input') { - template.content_tag(:div, :class => 'inline-inputs') do - template.fields_for( - self.object_name, - self.object, - self.options.merge(:builder => ActionView::Base.default_form_builder), - &block - ) - end - } + div_wrapper_with_label(label) do + template.content_tag(:div, :class => 'inline-inputs') do + template.fields_for( + self.object_name, + self.object, + self.options.merge(:builder => ActionView::Base.default_form_builder), + &block + ) + end end end @@ -91,14 +85,11 @@ def inline(label = nil, &block) label = args.first.nil? ? '' : args.shift classes = [ 'input' ] classes << ('input-' + options.delete(:add_on).to_s) if options[:add_on] - - self.div_wrapper(attribute) do - template.concat self.label(attribute, label) if label - template.concat template.content_tag(:div, :class => classes.join(' ')) { - template.concat super(attribute, *(args << options)) - template.concat error_span(attribute) - block.call if block.present? - } + + self.div_wrapper_with_label(label,attribute,:input_wrapper_class=>classes.join(' ')) do + template.concat super(attribute, *(args << options)) + template.concat error_span(attribute) + block.call if block.present? end end end @@ -136,6 +127,18 @@ def div_wrapper(attribute=nil, options = {}, &block) template.content_tag :div, {:class=>'clearfix'}, &block end end + + def div_wrapper_with_label(label,attribute=nil, options={}, &block) + input_wrapper_class = options.delete(:input_wrapper_class) || 'input' + div_wrapper(attribute,options) do + if attribute + template.concat self.label(attribute, label) + else + template.concat template.content_tag(:label, label) if label.present? + end + template.concat template.content_tag(:div, {:class=>input_wrapper_class},&block) + end + end def error_span(attribute, options = {}) options[:class] ||= 'help-inline'