Skip to content

Instantly share code, notes, and snippets.

@reu
Created January 2, 2012 22:55
Show Gist options
  • Save reu/1552513 to your computer and use it in GitHub Desktop.
Save reu/1552513 to your computer and use it in GitHub Desktop.
class TwitterBootstrapFormBuilder < ActionView::Helpers::FormBuilder
delegate :capture, :content_tag, :tag, :to => :@template
%w[text_field text_area password_field collection_select].each do |method_name|
define_method(method_name) do |name, *args|
errors = object.errors[name].any? ? " error" : ""
error_msg = object.errors[name].any? ? content_tag(:span, object.errors[name].join(","), :class => "help-inline") : ""
content_tag :div, :class => "clearfix#{errors}" do
field_label(name, *args) + content_tag(:div, :class => "input#{errors}") do
super(name, *args) + " " + error_msg
end
end
end
end
def check_box(name, *args)
content_tag :div, :class => "clearfix" do
content_tag(:div, class => "input") do
content_tag(:ul, :class => "inputs-list") do
content_tag(:li) do
content_tag(:label) do
super(name, *args) + content_tag(:span) do
field_label(name, *args)
end
end
end
end
end
end
end
def actions(*args, &block)
options = args.extract_options!
content = block_given? ? capture(&block) : ""
content_tag(:div, content, :class => options[:class])
end
def submit(*args)
super(*args, :class => "btn primary")
end
private
def field_label(name, *args)
options = args.extract_options!
label(name, options[:label])
end
def objectify_options(options)
super.except(:label)
end
end
module TwitterBootstrapHelper
def twitter_bootstrap_form_for(object, options = {}, &block)
options[:builder] = TwitterBootstrapFormBuilder
form_for(object, options, &block)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment