module Sinatra::FormHelpers

Constants

VERSION

Public Instance Methods

button(value, options = {}) click to toggle source

General purpose button, usually these need JavaScript hooks.

# File lib/sinatra/form_helpers.rb, line 102
def button(value, options = {})
  single_tag :input, {
    name:  "button",
    type:  "button",
    value: value,
    id:    css_id('button', value)
  }.merge(options)
end
checkbox(obj, field, values, options = {}) click to toggle source

Form checkbox. Specify an array of values to get a checkbox group.

# File lib/sinatra/form_helpers.rb, line 112
def checkbox(obj, field, values, options = {})
  join = options.delete(:join) || ' '
  labs = options.delete(:label)
  vals = param_or_default(obj, field, [])
  ary = values.is_a?(Array) && values.length > 1 ? '[]' : ''
  Array(values).collect do |val|
    id, text = id_and_text_from_value(val)
    single_tag(:input, options.merge(
      type:    "checkbox",
      id:      css_id(obj, field, id),
      name:    "#{obj}[#{field}]#{ary}",
      value:   id,
      checked: vals.include?(id) ? 'checked' : nil
    )) +
    (labs.nil? || labs == true ? label(obj, "#{field}_#{id.to_s.downcase}", text) : '')
  end.join(join)
end
css_id(*things) click to toggle source
# File lib/sinatra/form_helpers.rb, line 219
def css_id(*things)
  things.compact.map{|t| t.to_s}.join('_').downcase.gsub(/\W/,'_')
end
email(obj, field = nil, options = {}) click to toggle source

Email input field

# File lib/sinatra/form_helpers.rb, line 68
def email(obj, field = nil, options = {})
  input(obj, field, options.merge(type: 'email'))
end
fast_escape_html(text) click to toggle source
# File lib/sinatra/form_helpers.rb, line 186
def fast_escape_html(text)
  text.to_s.gsub(/\&/,'&amp;').gsub(/\"/,'&quot;').gsub(/>/,'&gt;').gsub(/</,'&lt;')
end
fieldset(obj, legend = nil) { |fieldset| ... } click to toggle source
# File lib/sinatra/form_helpers.rb, line 30
def fieldset(obj, legend = nil, &block)
  raise ArgumentError, "Missing block to fieldset()" unless block_given?
  out = yield Fieldset.new(self, obj)
  '<fieldset>' + (legend.nil? ? '' : "<legend>#{fast_escape_html(legend)}</legend>") + out.to_s + '</fieldset>'
end
form(action, method = :get, options = {}, &block) click to toggle source

FormHelpers are a suite of helper methods built to make building forms in Sinatra a breeze.

link “jackhq”, “www.jackhq.com

label :person, :first_name input :person, :first_name textarea :person, :notes

etc.

# File lib/sinatra/form_helpers.rb, line 14
def form(action, method = :get, options = {}, &block)
  method_input = ''
  # the docs suggest using ':create', ':update', or ':delete'
  # but you can use any symbol for the method value
  # allows for more than 3 forms on a single page
  if method.is_a? Symbol
    method_input = %Q(<input type="hidden" name="_method" value="#{method}" />)
    method = :post
  end
  action = "/#{action}" if action.is_a? Symbol

  out = tag(:form, nil, { action: action, method: method.to_s }.merge(options), false) + method_input
  out << fieldset(action, &block) + '</form>' if block_given?
  out
end
hash_to_html_attrs(options = {}) click to toggle source
# File lib/sinatra/form_helpers.rb, line 202
def hash_to_html_attrs(options = {})
  html_attrs = ""
  options.keys.sort.each do |key|
    next if options[key].nil? # do not include empty attributes
    html_attrs << %Q(#{key}="#{fast_escape_html(options[key])}" )
  end
  html_attrs.chop
end
hidden(obj, field = nil, options = {}) click to toggle source

Form hidden input. Specify value as value: ‘foo’

# File lib/sinatra/form_helpers.rb, line 164
def hidden(obj, field = nil, options = {})
  input(obj, field, options.merge(type: 'hidden'))
end
id_and_text_from_value(val) click to toggle source
# File lib/sinatra/form_helpers.rb, line 211
def id_and_text_from_value(val)
  if val.is_a? Array
    [val.first, val.last]
  else
    [val, val]
  end
end
image(src, options = {}) click to toggle source

Link to an image

# File lib/sinatra/form_helpers.rb, line 42
def image(src, options = {})
  single_tag :img, options.merge(src: src)
end
input(obj, field = nil, options = {}) click to toggle source

Form text input. Specify the value as value: ‘foo’

# File lib/sinatra/form_helpers.rb, line 52
def input(obj, field = nil, options = {})
  value = param_or_default(obj, field, options[:value])
  single_tag :input, options.merge(
    type:  options[:type] || "text",
    id:    css_id(obj, field),
    name:  field.nil? ? obj : "#{obj}[#{field}]",
    value: value
  )
end
label(obj, field, display = "", options = {}) click to toggle source

Form field label

# File lib/sinatra/form_helpers.rb, line 47
def label(obj, field, display = "", options = {})
  tag :label, (display.nil? || display == '') ? titleize(field.to_s) : display, options.merge(for: css_id(obj, field))
end
param_or_default(obj, field, default) click to toggle source
# File lib/sinatra/form_helpers.rb, line 194
def param_or_default(obj, field, default)
  if field
    params[obj] ? params[obj][field.to_s] || default : default
  else
    params[obj] || default
  end
end
password(obj, field = nil, options = {}) click to toggle source

Form password input. Specify the value as value: ‘foo’

# File lib/sinatra/form_helpers.rb, line 63
def password(obj, field = nil, options = {})
  input(obj, field, options.merge(type: 'password'))
end
radio(obj, field, values, options = {}) click to toggle source

Form radio input. Specify an array of values to get a radio group.

# File lib/sinatra/form_helpers.rb, line 131
def radio(obj, field, values, options = {})
  #content = @params[obj] && @params[obj][field.to_s] == value ? "true" : ""
  # , checked: content
  join = options.delete(:join) || ' '
  labs = options.delete(:label)
  vals = param_or_default(obj, field, [])
  Array(values).collect do |val|
    id, text = id_and_text_from_value(val)
    single_tag(:input, options.merge(
      type:    "radio",
      id:      css_id(obj, field, id),
      name:    "#{obj}[#{field}]",
      value:   id,
      checked: vals.include?(id) ? 'checked' : nil
    )) +
    (labs.nil? || labs == true ? label(obj, "#{field}_#{id.to_s.downcase}", text) : '')
  end.join(join)
end
reset(value = 'Reset', options = {}) click to toggle source

Form reset tag. Does anyone use these anymore?

# File lib/sinatra/form_helpers.rb, line 92
def reset(value = 'Reset', options = {})
  single_tag :input, {
    name:  "reset",
    type:  "reset",
    value: value,
    id:    css_id('button', value)
  }.merge(options)
end
select(obj, field, values, options = {}) click to toggle source

Form select dropdown. Currently only single-select (not multi-select) is supported.

# File lib/sinatra/form_helpers.rb, line 151
def select(obj, field, values, options = {})
  value = param_or_default(obj, field, options[:value])
  content = ""
  Array(values).each do |val|
    id, text = id_and_text_from_value(val)
    tag_options = { value: id }
    tag_options[:selected] = 'selected' if id == value
    content << tag(:option, text, tag_options)
  end
  tag :select, content, options.merge(id: css_id(obj, field), name: "#{obj}[#{field}]")
end
single_tag(name, options = {}) click to toggle source

Standard single closing tags single_tag :img, src: “images/google.jpg”

> <img src=“images/google.jpg” />

# File lib/sinatra/form_helpers.rb, line 182
def single_tag(name, options = {})
  "<#{name.to_s} #{hash_to_html_attrs(options)} />"
end
submit(value = 'Submit', options = {}) click to toggle source

Form submit tag.

# File lib/sinatra/form_helpers.rb, line 82
def submit(value = 'Submit', options = {})
  single_tag :input, {
    name:  "submit",
    type:  "submit",
    value: value,
    id:    css_id('button', value)
  }.merge(options)
end
tag(name, content, options = {}, close = true) click to toggle source

Standard open and close tags EX : tag :h1, “shizam”, title: “shizam”

> <h1 title=“shizam”>shizam</h1>

# File lib/sinatra/form_helpers.rb, line 171
def tag(name, content, options = {}, close = true)
  attributes = " #{ hash_to_html_attrs(options) }" if options.length > 0
  open_tag   = "<#{ name }#{ attributes }>"
  close_tag  = "</#{ name }>" if close

  "#{ open_tag }#{ content }#{ close_tag }"
end
textarea(obj, field = nil, content = '', options = {}) click to toggle source

Form textarea box.

# File lib/sinatra/form_helpers.rb, line 73
def textarea(obj, field = nil, content = '', options = {})
  content = param_or_default(obj, field, content)
  tag :textarea, content, options.merge(
    id:   css_id(obj, field),
    name: field.nil? ? obj : "#{obj}[#{field}]"
  )
end
titleize(text) click to toggle source
# File lib/sinatra/form_helpers.rb, line 190
def titleize(text)
  text.to_s.gsub(/_+/, ' ').gsub(/\b('?[a-z])/) { $1.capitalize }
end