Module: LRD::FormHelper

Defined in:
lib/app/helpers/lrd_form_helper.rb

Instance Method Summary collapse

Instance Method Details

#comment_for_labeled_input(text) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/app/helpers/lrd_form_helper.rb', line 46

def comment_for_labeled_input(text)
  if text
    ( :span, { :class => 'comment' } ) { text }
  else
    ""
  end
end

#input_for_labeled_input(object_name, method, options) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object

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>


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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
  (:div, (label + input + comment), { :class => div_final_class })
end

#labeled_input_div_class(options, divclass = nil) ⇒ Object



55
56
57
58
59
60
# 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) ⇒ Object

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



101
102
103
# 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 = {}) ⇒ Object

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>


115
116
117
# 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