module LRD::FormHelper

Public Instance Methods

comment_for_labeled_input(text) click to toggle source
# File lib/app/helpers/lrd_form_helper.rb, line 46
def comment_for_labeled_input(text)
  if text
    content_tag( :span, { :class => 'comment' } ) { text }
  else
    ""
  end
end
input_for_labeled_input(object_name, method, options) click to toggle source
# File lib/app/helpers/lrd_form_helper.rb, line 62
def input_for_labeled_input(object_name, method, options)
  if required = options.delete(:required)
    options[:class] = (options[:class] or '') + " required"
  end
  submit_text = options.delete(:submit_text)

  case input_type = options.delete(: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", "textarea"
    input = text_area(      object_name, method, options)
  when "search"
    input = search_field(   object_name, method, options)
  when "telephone", 'tel'
    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(     submit_text, options)
  else
    raise "labeled_input input_type #{input_type} is not a valid type!"
  end
  input
end
labeled_input(object_name, method, options = {}, &block) click to toggle source

Returns a <div> 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 (class required set on both the div and the input) pass :text => “foo” to override the label text pass :divclass => 'foo' to add 'foo' to the CSS class of the <div> pass :comment => “text” to append a span.comment with text after the input pass :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)
# => <div class='labeled_input'>
# =>   <label for='user_login'>login</label>
# =>   <input type='text' name='user[login]' id='user_login' value="#{@user.login}" />
# => </div>
# File lib/app/helpers/lrd_form_helper.rb, line 25
def labeled_input(object_name, method, options = {}, &block)
  divclass = options.delete(:divclass)
  div_final_class = labeled_input_div_class(options, divclass)
  comment = comment_for_labeled_input(options.delete(:comment))
  label_text = options.delete(:text)
  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 = "<label>&nbsp;</label>".html_safe
  elsif label_text =
    label = label(object_name, method, label_text, options)
  else
    label = label(object_name, method, options)
  end
  content_tag(:div, (label + input + comment), { :class => div_final_class })
end
labeled_input_div_class(options, divclass = nil) click to toggle source
# File lib/app/helpers/lrd_form_helper.rb, line 55
def labeled_input_div_class(options, divclass = nil)
  cssclass = "labeled_input"
  cssclass += " required" if options[:required]
  cssclass += " #{divclass}" if divclass
  cssclass
end
unlabeled_input(object_name, method, options) click to toggle source

Shortcut for a version of labeled_input that suppresses the label text. Just calls labeled_input with :label => false.

# File lib/app/helpers/lrd_form_helper.rb, line 101
def unlabeled_input(object_name, method, options)
  labeled_input(object_name, method, options.merge!(:label => false))
end
unlabeled_submit(text = nil, options={}) click to toggle source

creates a submit button that lines up with a bunch of labeled_input fields Pass a single argument to override the default text of the submit button.

Examples (in HAML):

- form_for(@user) do
  = f.unlabeled_submit("Click Me")
# => <div class='labeled_input'>
# =>   <label> </label>
# =>   <input type='submit' name='[]' value='Click Me' />
# => </div>
# File lib/app/helpers/lrd_form_helper.rb, line 115
def unlabeled_submit(text = nil, options={})
  labeled_input(nil, nil, options.merge!({:type => :submit, :submit_text => text}))
end