Skip to content

Instantly share code, notes, and snippets.

@straydogstudio
Created May 30, 2013 16:34
Show Gist options
  • Save straydogstudio/5679275 to your computer and use it in GitHub Desktop.
Save straydogstudio/5679275 to your computer and use it in GitHub Desktop.
Rails helpers for Bootstrap styled fields. I use these in conjunction with simple_form to provide non model fields.
module BootstrapHelper
def bootstrap_best_in_place_if(condition, object, field, options = {})
content_tag(:div, class: "control-group string optional #{options.delete(:wrapper_class)}".strip) do
label_tag(field, options.delete(:label), class: 'string optional control-label') +
content_tag(:div, class: 'controls') do
best_in_place_if condition, object, field, options
end
end
end
def bootstrap_check_box_tag(field, value = "1", checked = false, options = {})
content_tag(:div, class: "control-group boolean optional #{options.delete(:wrapper_class)}".strip) do
label_tag(field, options.delete(:label), class: 'boolean optional control-label') +
content_tag(:div, class: 'controls') do
hidden_field_tag(field, 0) +
label_tag(nil, nil, class: 'checkbox') do
check_box_tag field, value, checked, options
end
end
end
end
def bootstrap_collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
content_tag(:div, class: "control-group select optional #{html_options.delete(:wrapper_class)}".strip) do
label_tag(method, options.delete(:label), class: 'select optional control-label') +
content_tag(:div, class: 'controls') do
collection_select(object, method, collection, value_method, text_method, options, html_options)
end
end
end
def bootstrap_select(object, method, choices, options = {}, html_options = {})
content_tag(:div, class: "control-group select optional #{html_options.delete(:wrapper_class)}".strip) do
label_tag(method, options.delete(:label), class: 'select optional control-label') +
content_tag(:div, class: 'controls') do
select(object, method, choices, options, html_options) +
(html_options[:hint] ? content_tag(:p, html_options[:hint], class: 'help-block') : '')
end
end
end
def bootstrap_select_tag(name, option_tags = nil, options = {})
content_tag(:div, class: "control-group select optional #{options.delete(:wrapper_class)}".strip, id: options.delete(:wrapper_id)) do
label_tag(name, options.delete(:label), class: 'select optional control-label') +
content_tag(:div, class: 'controls') do
select_tag(name, option_tags, options) +
(options[:hint] ? content_tag(:p, options[:hint], class: 'help-block') : '')
end
end
end
def bootstrap_text_field_tag(field, value = nil, options = {})
content_tag(:div, class: "control-group string optional #{options.delete(:wrapper_class)}".strip) do
label = label_tag(field, options.delete(:label), class: 'string optional control-label')
if options[:datepicker] || options[:class] && options[:class] =~ /datepicker/ || value.class.name =~ /Date|Time/
options[:class].gsub!(/datepicker/,'') if options[:class]
label + content_tag(:div, class: 'controls input-append datepicker') do
text_field_tag(field, value, options) +
"<span class='add-on'><i class='icon-calendar'></i></span>".html_safe
end
else
label + content_tag(:div, class: 'controls') do
text_field_tag field, value, options
end
end
end
end
def bootstrap_datepicker_tag(field, value = nil, options = {})
content_tag(:div, class: "input-append datepicker #{options.delete(:wrapper_class)}".strip) do
text_field_tag(field, value, options) +
"<span class='add-on'><i class='icon-calendar'></i></span>".html_safe
end
end
def bootstrap_text_area_tag(field, content = nil, options = {})
content_tag(:div, class: "control-group text optional #{options.delete(:wrapper_class)}".strip) do
label_tag(field, options.delete(:label), class: 'text optional control-label') +
content_tag(:div, class: 'controls') do
text_area_tag field, content, options
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment