Skip to content

Instantly share code, notes, and snippets.

@jamiepenney
Created September 23, 2011 01:47
Show Gist options
  • Save jamiepenney/1236567 to your computer and use it in GitHub Desktop.
Save jamiepenney/1236567 to your computer and use it in GitHub Desktop.
Form builder for Twitter Bootstrap form elements.
class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
def get_error_text(object, field, options)
if object.nil? || options[:hide_errors]
""
else
errors = object.errors[field.to_sym]
if errors.empty? then "" else errors.first end
end
end
def jquery_datetime_select(field, options = {})
datetime_picker_script = "<script type='text/javascript'>" +
"$( function() { " +
"$('##{id}')" +
".datetimepicker( $.datepicker.regional[ 'en-NZ' ] )" +
".datetimepicker( 'setDate', new Date('#{date_time}') ); } );" +
"</script>"
return basic_datetime_select(field, options.merge({javascript: datetime_picker_script}))
end
def basic_datetime_select(field, options = {})
input_class = options[:class] || 'date'
placeholder_text = options[:placeholder_text] || 'Date'
object = @template.instance_variable_get("@#{@object_name}")
id = options[:id] || object.class.name.underscore + '_' + field.to_s
errorText = get_error_text(object, field, options)
wrapperClass = 'clearfix' + (errorText.empty? ? '' : ' error')
errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
label = label(field, options[:label])
date_time =
if options['start_time']
options['start_time']
elsif object.nil?
DateTime.now.utc
else
object.send(field.to_sym)
end
javascript = options['javascript'] ||
"<script>$(function() { $('##{id}').val(new Date($('##{id}').val()).toString('dd MMM, yyyy HH:mm')) });</script>"
("<div class='#{wrapperClass}'>" +
label +
"<div class='input'>" +
super_text_field(field, {:id => id, :placeholder => placeholder_text, :value => date_time.to_s}.merge(options[:text_field] || {})) +
errorSpan +
javascript +
"</div>" +
"</div>").html_safe
end
basic_helpers = %w{text_field text_area select email_field password_field check_box}
multipart_helpers = %w{date_select datetime_select}
basic_helpers.each do |name|
# First alias old method
class_eval("alias super_#{name.to_s} #{name}")
define_method(name) do |field, *args|
options = args.last.is_a?(Hash) ? args.pop : {}
object = @template.instance_variable_get("@#{@object_name}")
label = label(field, options[:label])
errorText = get_error_text(object, field, options)
wrapperClass = 'clearfix' + (errorText.empty? ? '' : ' error')
errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
("<div class='#{wrapperClass}'>" +
label +
"<div class='input'>" +
super(field, options) +
errorSpan +
"</div>" +
"</div>"
).html_safe
end
end
multipart_helpers.each do |name|
define_method(name) do |field, *args|
options = args.last.is_a?(Hash) ? args.pop : {}
object = @template.instance_variable_get("@#{@object_name}")
label = label(field, options[:label])
errorText = get_error_text(object, field, options)
wrapperClass = 'clearfix' + (errorText.empty? ? '' : ' error')
errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
("<div class='#{wrapperClass}'>" +
label +
"<div class='input'>" +
"<div class='inline-inputs'>" +
super(field, *args) +
errorSpan +
"</div>" +
"</div>" +
"</div>"
).html_safe
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment