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 = ""
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 "#{errorText}" 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'] ||
""
("
" +
label +
"
" +
super_text_field(field, {:id => id, :placeholder => placeholder_text, :value => date_time.to_s}.merge(options[:text_field] || {})) +
errorSpan +
javascript +
"
" +
"
").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 "#{errorText}" end
("" +
label +
"
" +
super(field, options) +
errorSpan +
"
" +
"
"
).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 "#{errorText}" end
(""
).html_safe
end
end
end