Skip to content

Commit

Permalink
DRY up the label printing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle committed Nov 29, 2011
1 parent e89018d commit 8765ba7
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions lib/twitter_bootstrap_form_for/form_builder.rb
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 8765ba7

Please sign in to comment.