Created
December 16, 2011 22:15
-
-
Save febuiles/1488262 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
module FormsHelper | |
def self.included(base) | |
ActionView::Base.default_form_builder = TwitterFormBuilder | |
end | |
class TwitterFormBuilder < ActionView::Helpers::FormBuilder | |
def legend(text) | |
content_tag :fieldset, content_tag(:legend, text) | |
end | |
def text_field_with_label(name, options={}) | |
if options[:disabled] | |
field = text_field(name, :disabled => true) | |
else | |
field = text_field(name) | |
end | |
field_with_label(field, name, options) | |
end | |
def text_area_with_label(name, options={}) | |
area = text_area(name, :class => "xxlarge") | |
field_with_label(area, name, options) | |
end | |
def file_field_with_label(name, options={}) | |
file = file_field(name) | |
field_with_label(file, name, options) | |
end | |
def select_with_label(name, values, options={ }) | |
blank = options[:include_blank] | |
size = options[:class] || "medium" | |
field = select(name, values, { :include_blank => blank }, :class => size) | |
field_with_label(field, name, options) | |
end | |
def boolean_field_with_label(name, options={}) | |
span_true = content_tag(:span, "Sí") | |
span_false = content_tag(:span, "No") | |
button_true = radio_button(name, true) | |
button_false = radio_button(name, false) | |
label_true = content_tag(:label, button_true + span_true) | |
label_false = content_tag(:label, button_false + span_false) | |
li_true = content_tag(:li, label_true) | |
li_false = content_tag(:li, label_false) | |
ul = content_tag(:ul, li_true + li_false, :class => "inputs-list") | |
field_with_label(ul, name, options) | |
end | |
def field_with_label(content, name, options={}) | |
if required?(name) | |
label = label(name, nil, :class => "required") | |
else | |
label = label(name) | |
end | |
help = content_tag(:span, options[:help], :class => "help-block") | |
content << help unless options[:help].nil? | |
input = content_tag(:div, content, :class => "input") | |
content << help unless help.empty? | |
content_tag(:div, label + input, :class => "clearfix") | |
end | |
def content_tag(field, content, options={}) | |
@template.content_tag(field, content, options) | |
end | |
def required?(field) | |
@object.class.validators_on(field).any? do |validator| | |
validator.kind_of?(ActiveModel::Validations::PresenceValidator) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment