module LRD
module FormHelper
def self.included(arg)
p "LRD::FormHelper included in #{arg}"
ActionView::Helpers::FormBuilder.send(:include, LRD::FormBuilder)
end
# Returns a
containing a label, an input, and an option comment
# block, pre-styled in LRD style.
#
# pass :label => false to suppress the label text. (A label tag is still emitted.)
# pass :required => true to dispay as a required field
# pass :text => "foo" to override the label text
# pass :class => 'foo' to add 'foo' to the CSS class of the
# pass :comment => "text" to append a span.comment with text after the input
# pass :input_type => 'password' } to use a password_field instead of a text_field
# (also supported: text, passsword, hidden, file, text_area, search, telephone, url
# email, range, submit)
#
# ==== Examples (in HAML):
# - form_for(@user) do
# = f.labeled_input(:login)
# # =>
# # =>
# # =>
# # =>
def labeled_input(object_name, method, options = {}, &block)
divclass = labeled_input_divclass(options)
comment = comment_for_labeled_input(options.delete(:comment))
if block_given?
input = capture(&block)
else
input = input_for_labeled_input(object_name, method, options)
end
if object_name.blank? or method.blank?
label = "".html_safe
elsif text = options.delete(:text)
label = label(object_name, method, text, options)
else
label = label(object_name, method, options)
end
content_tag(:div, (label + input + comment), { :class => divclass })
end
def comment_for_labeled_input(text)
if text
content_tag( :span, { :class => 'comment' } ) { text }
else
""
end
end
def labeled_input_divclass(options)
cssclass = "labeled_input"
cssclass += " required" if options[:required]
cssclass += " #{options[:class]}" if options[:class]
cssclass
end
def input_for_labeled_input(object_name, method, options)
case input_type = options.delete(:input_type).to_s
when "text", ""
input = text_field( object_name, method, options)
when "password"
input = password_field( object_name, method, options)
when "hidden"
input = hidden_field( object_name, method, options)
when "file"
input = file_field( object_name, method, options)
when "text_area"
input = text_area( object_name, method, options)
when "search"
input = search_field( object_name, method, options)
when "telephone"
input = telephone_field(object_name, method, options)
when "url"
input = url_field( object_name, method, options)
when "email"
input = email_field( object_name, method, options)
when "number"
input = number_field( object_name, method, options)
when "range"
input = range_field( object_name, method, options)
when "submit"
input = submit_tag( options[:submit_text], options)
else
raise "labeled_input input_type #{input_type} is not a valid type!"
end
input
end
def unlabeled_input(object_name, method, options)
labeled_input(object_name, method, options.merge!(:label => false))
end
# creates a submit button that lines up with a bunch of labeled_input fields
def unlabeled_submit(text = nil)
labeled_input(nil, nil, :input_type => :submit, :submit_text => text)
end
end
end
# # pass { :nolabel => true } to replace the label with a spacer
# # pass { :required => true } to dispay as a required field
# # pass { :text => "foo" } to override the label text
# # pass { :class => 'foo'} to add 'foo' to the CSS class of the