Module: TentSteakFeatures::FormView

Defined in:
lib/tent_steak/form.rb

Overview

TentSteak view methods for :form feature. Provides form HTML helpers for text, selection, checkbox, and radio button fields, plus a simple drop-down selection form, and a file upload form.

Instance Method Summary collapse

Instance Method Details

#input_checkbox(field_name, label = nil, checked = false, options = {}) ⇒ Object

Render an input checkbox HTML field with an optional label. If checked is true, check the checkbox by default.



56
57
58
59
60
# File 'lib/tent_steak/form.rb', line 56

def input_checkbox(field_name, label = nil, checked = false, options = {})
  attrs = {:type => "checkbox", :name => field_name}
  attrs.merge!({:checked => "checked"}) if checked
  input(label || "", attrs.merge(options))
end

#input_file(field_name, label_text = nil, options = {}) ⇒ Object

Helper to generate an HTML file <input> field.

  • field_name is the HTML field name for the file field

  • label_text is the optional text description for the field

  • width is the optional character width of the text field



120
121
122
123
124
125
# File 'lib/tent_steak/form.rb', line 120

def input_file(field_name, label_text = nil, options = {})
  if label_text
    label label_text; br
  end
  input({ :type => "file", :name => field_name }.merge(options))
end

#input_hidden(field_name, value, options = {}) ⇒ Object

Render a hidden HTML field with the given name and value. Useful for passing static state info into a POST method.



50
51
52
# File 'lib/tent_steak/form.rb', line 50

def input_hidden(field_name, value, options = {})
  input({:type => "hidden", :name => field_name, :value => value}.merge(options))
end

#input_password(field_name, options = {}) ⇒ Object

Render an input password HTML field.



39
40
41
# File 'lib/tent_steak/form.rb', line 39

def input_password(field_name, options = {})
  input({:type => "password", :name => field_name}.merge(options))
end

#input_submit(button_text, options = {}) ⇒ Object

Render an HTML form submit button with the given button text.



44
45
46
# File 'lib/tent_steak/form.rb', line 44

def input_submit(button_text, options = {})
  input({:type => "submit", :value => button_text}.merge(options))
end

#input_text(field_name, value = "", options = {}) ⇒ Object

Render an input text HTML field with the given name and default value.



34
35
36
# File 'lib/tent_steak/form.rb', line 34

def input_text(field_name, value = "", options = {})
  input({:type => "text", :name => field_name, :value => value}.merge(options))
end

#radio_button(field_name, group_name, label_text = nil, checked = false, options = {}) ⇒ Object

Helper to generate an HTML radio button.

  • field_name is the HTML field name of the radio button (i.e. the HTML value attribute), which should be unique among other buttons within the same group_name.

  • group_name is the radio button selection group (i.e. the HTML name attribute).

  • label_text is the optional text description for the field; defaults to field_name.titlecase.

  • checked is the selected state of the radio button; should only be true for one radio button within a group_name.



71
72
73
74
75
# File 'lib/tent_steak/form.rb', line 71

def radio_button(field_name, group_name, label_text = nil, checked = false, options = {})
  props = {:type => "radio", :name => group_name, :value => field_name}
  props.merge!({:checked => "checked"}) if checked
  input label_text || field_name.titlecase, props.merge(options)
end

#radio_group(group_name, fields, checked_field = nil, line_breaks = true) ⇒ Object

Helper to generate a related group of HTML radio buttons.

  • group_name is the radio button selection group (i.e. the HTML name attribute)

  • fields is an array of radio button fields to create.

  • checked_field is the field_name of a radio button in the group to select by default; this parameter should match a field name in fields.

  • line_breaks is true to insert a <br/> tag between each radio button.

The fields array can hold a simple array of field names, in which case the label_text of each radio_button is set to field_name.titlecase.

radio_group "my_group", %w{field_one field_two}, "field_two"

This generates the following HTML, with the second radio button preselected:

<input type="radio" value="field_one" name="my_group">Field One</input>
<br/>
<input type="radio" value="field_two" name="my_group" checked="checked">Field Two</input>
<br/>

To customize the label text, pass in an array of [field_name, label_text] in place of the field name:

radio_group "my_group", ["field_one", ["field_two", "Field Two (Default)"]], "field_two"

This results in the following HTML:

<input type="radio" value="field_one" name="my_group">Field One</input>
<br/>
<input type="radio" value="field_two" name="my_group" checked="checked">Field Two (Default)</input>
<br/>


108
109
110
111
112
113
# File 'lib/tent_steak/form.rb', line 108

def radio_group(group_name, fields, checked_field = nil, line_breaks = true)
  fields.each do |field_name, label_text|
    radio_button(field_name, group_name, label_text, field_name == checked_field)
    br if line_breaks
  end
end

#select_form(action, field_name, option_list, label_text = nil, button_text = "Submit") ⇒ Object

Helper to generate an HTML form with a single select box.

  • action is the form action to perform; can be a URL or R(), for example R(MyController)

  • field_name is the HTML field name for the dropdown list

  • option_list is an array of dropdown list items (see select_menu)

  • label_text is the optional width of the text field

  • button_text is the text for the form submit button



180
181
182
183
184
185
186
# File 'lib/tent_steak/form.rb', line 180

def select_form(action, field_name, option_list, label_text = nil, button_text = "Submit")
  form :action => action, :method => "post" do
    select_menu(field_name, option_list, label_text)
    br;br
    input_submit button_text
  end
end

#select_menu(field_name, option_list, label_text = nil, checked_field = nil) ⇒ Object

Helper to generate an HTML <select> field for a dropdown list.

  • field_name is the HTML field name for the dropdown list

  • option_list is an array of dropdown list items

  • label_text is the optional width of the text field

A simple string array in option_list produces a list where the display value is identical to the field value, e.g.

select_menu "my_field", ["One", "Two"]

produces this HTML:

<select name="my_field">
  <option value="One">One</option>
  <option value="Two">Two</option>
</select>

To create a dropdown list with different display and field values, create an option_list of nested 2-element arrays with the field value first and the display value second. Thus

select_menu "my_field", [ ["field1", "Display One"], ["field2", "Display Two"] ]

generates the following HTML:

<select name="my_field">
  <option value="field1">Display One</option>
  <option value="field2">Display Two</option>
</select>

To pre-select a list value, pass that value to checked_field.



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/tent_steak/form.rb', line 159

def select_menu(field_name, option_list, label_text = nil, checked_field = nil)
  if label_text
    label label_text; br
  end
  select :name => field_name do
    option_list.each do |value, text|
      attrs = {:value => value}
      attrs.merge!(:selected => "selected") if checked_field == value
      option(text || value, attrs)
    end
  end
end

#upload_form(form_attrs) ⇒ Object

call-seq:

upload_form(form_attrs) { form_block }

Helper to generate an HTML form for uploading files. Sets the form’s method and enctype attributes for you. Pass additional attributes for the <form> element as the form_attrs parameter; pass the contents of the form as a block.

This example uploads a file my_file to the HandleUpload Camping controller.

upload_form :action => R(HandleUpload) do
  input_file "my_file", "Take it, take it!"
end

The contents of the file will reside as a tempfile at input.my_file.tempfile for at least the duration of the HTML POST operation. Here’s an example Controller:

module MyApp::Controllers
  class HandleUpload < R '/upload'
    def post
      @contents = input.my_file.tempfile.read
    end
  end
end


211
212
213
# File 'lib/tent_steak/form.rb', line 211

def upload_form(form_attrs) # :yields: 
  form({:method => "post", :enctype => "multipart/form-data"}.merge(form_attrs)) { yield }
end