Created
December 7, 2010 07:37
-
-
Save robheittman/731562 to your computer and use it in GitHub Desktop.
Overrides Formtastic::SemanticFormBuilder to render hints below labels like Google Docs Forms. It's really a 3-line patch, but we need to override three whole methods to get the effect.
This file contains 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
class GoogleDocStyleBuilder < Formtastic::SemanticFormBuilder | |
# Overrides the input generator to suppress the normal hint | |
# Track source: https://github.com/justinfrench/formtastic/raw/master/lib/formtastic.rb | |
def input(method, options = {}) | |
options[:required] = method_required?(method) unless options.key?(:required) | |
options[:as] ||= default_input_type(method, options) | |
html_class = [ options[:as], (options[:required] ? :required : :optional) ] | |
html_class << 'error' if has_errors?(method, options) | |
wrapper_html = options.delete(:wrapper_html) || {} | |
wrapper_html[:id] ||= generate_html_id(method) | |
wrapper_html[:class] = (html_class << wrapper_html[:class]).flatten.compact.join(' ') | |
if options[:input_html] && options[:input_html][:id] | |
options[:label_html] ||= {} | |
options[:label_html][:for] ||= options[:input_html][:id] | |
end | |
input_parts = (self.class.custom_inline_order[options[:as]] || self.class.inline_order).dup | |
# PATCH: remove hints from sequence; we'll draw them as part of the label | |
input_parts = input_parts - [:hints] | |
input_parts = input_parts - [:errors, :hints] if options[:as] == :hidden | |
list_item_content = input_parts.map do |type| | |
send(:"inline_#{type}_for", method, options) | |
end.compact.join("\n") | |
return template.content_tag(:li, Formtastic::Util.html_safe(list_item_content), wrapper_html) | |
end | |
def label(method, options_or_text=nil, options=nil) | |
if options_or_text.is_a?(Hash) | |
return "" if options_or_text[:label] == false | |
options = options_or_text | |
text = options.delete(:label) | |
else | |
text = options_or_text | |
options ||= {} | |
end | |
text = localized_string(method, text, :label) || humanized_attribute_name(method) | |
text += required_or_optional_string(options.delete(:required)) | |
text = Formtastic::Util.html_safe(text) | |
# PATCH: add inline hints | |
text += inline_hints_for(method, options_for_label(options)) | |
# special case for boolean (checkbox) labels, which have a nested input | |
if options.key?(:label_prefix_for_nested_input) | |
text = options.delete(:label_prefix_for_nested_input) + text | |
end | |
input_name = options.delete(:input_name) || method | |
super(input_name, text, options) | |
end | |
def legend_tag(method, options = {}) | |
if options[:label] == false | |
Formtastic::Util.html_safe("") | |
else | |
text = localized_string(method, options[:label], :label) || humanized_attribute_name(method) | |
text += required_or_optional_string(options.delete(:required)) | |
text = Formtastic::Util.html_safe(text) | |
# PATCH: add inline hints | |
text += inline_hints_for(method, options_for_label(options)) | |
template.content_tag :legend, template.label_tag(nil, text, :for => nil), :class => :label | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment