Module: Sinatra::FormHelpers

Defined in:
lib/sinatra/form_helpers.rb

Defined Under Namespace

Classes: Fieldset

Instance Method Summary collapse

Instance Method Details

#button(value, options = {}) ⇒ Object

General purpose button, usually these need JavaScript hooks.



95
96
97
98
# File 'lib/sinatra/form_helpers.rb', line 95

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 = {}) ⇒ Object

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



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sinatra/form_helpers.rb', line 101

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?(val) ? 'checked' : nil)) +
    (labs.nil? || labs == true ? label(obj, "#{field}_#{id.to_s.downcase}", text) : '')
  end.join(join)
end

#css_id(*things) ⇒ Object



200
201
202
# File 'lib/sinatra/form_helpers.rb', line 200

def css_id(*things)
  things.compact.map{|t| t.to_s}.join('_').downcase.gsub(/\W/,'_')
end

#fast_escape_html(text) ⇒ Object



167
168
169
# File 'lib/sinatra/form_helpers.rb', line 167

def fast_escape_html(text)
  text.to_s.gsub(/\&/,'&amp;').gsub(/\"/,'&quot;').gsub(/>/,'&gt;').gsub(/</,'&lt;')
end

#fieldset(obj, legend = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
# File 'lib/sinatra/form_helpers.rb', line 32

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 + '</fieldset>'
end

#form(action, method = :get, options = {}, &block) ⇒ Object

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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sinatra/form_helpers.rb', line 14

def form(action, method=:get, options={}, &block)
  method_input = ''
  if method.is_a? Symbol
    case method.to_s.downcase
    when 'delete', 'update'
      method_input = %Q(<input type="hidden" name="_method" value="#{method}" />)
      method = :post
    when 'create'
      method = :post
    end
  end
  action = "/#{action}" if action.is_a? Symbol

  out = tag(:form, nil, {:action => action, :method => method.to_s.upcase}.merge(options)) + method_input
  out << fieldset(action, &block) + '</form>' if block_given?
  out
end

#hash_to_html_attrs(options = {}) ⇒ Object



183
184
185
186
187
188
189
190
# File 'lib/sinatra/form_helpers.rb', line 183

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 = "", options = {}) ⇒ Object

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



146
147
148
149
# File 'lib/sinatra/form_helpers.rb', line 146

def hidden(obj, field="", options={})
  content = param_or_default(obj, field, options[:value])
  single_tag :input, options.merge(:type => "hidden", :id => css_id(obj, field), :name => "#{obj}[#{field}]")      
end

#id_and_text_from_value(val) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/sinatra/form_helpers.rb', line 192

def id_and_text_from_value(val)
  if val.is_a? Array
    [val.first, val.last]
  else
    [val, val]
  end
end

#image(src, options = {}) ⇒ Object

Link to an image



44
45
46
# File 'lib/sinatra/form_helpers.rb', line 44

def image(src, options={})
  single_tag :img, options.merge(:src => src)
end

#input(obj, field = nil, options = {}) ⇒ Object

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



54
55
56
57
58
59
60
61
# File 'lib/sinatra/form_helpers.rb', line 54

def input(obj, field=nil, options={})
  value = param_or_default(obj, field, options[:value])
  single_tag :input, options.merge(
    :type => "text", :id => css_id(obj, field),
    :name => field.nil? ? obj : "#{obj}[#{field}]",
    :value => value
  )
end

#label(obj, field, display = "", options = {}) ⇒ Object

Form field label



49
50
51
# File 'lib/sinatra/form_helpers.rb', line 49

def label(obj, field, display = "", options={})
  tag :label, (display.nil? || display == '') ? titleize(field.to_s) : display, options.merge(:for => "#{obj}_#{field}")
end

Link to a URL



39
40
41
# File 'lib/sinatra/form_helpers.rb', line 39

def link(content, href=content, options={})
  tag :a, content, options.merge(:href => href)  
end

#param_or_default(obj, field, default) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/sinatra/form_helpers.rb', line 175

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 = {}) ⇒ Object

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



64
65
66
67
68
69
70
71
# File 'lib/sinatra/form_helpers.rb', line 64

def password(obj, field=nil, options={})
  value = param_or_default(obj, field, options[:value])
  single_tag :input, options.merge(
    :type => "password", :id => css_id(obj, field),
    :name => field.nil? ? obj : "#{obj}[#{field}]",
    :value => value
  )
end

#radio(obj, field, values, options = {}) ⇒ Object

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sinatra/form_helpers.rb', line 116

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?(val) ? 'checked' : nil)) +
    (labs.nil? || labs == true ? label(obj, "#{field}_#{id.to_s.downcase}", text) : '')
  end.join(join)
end

#reset(value = 'Reset', options = {}) ⇒ Object

Form reset tag. Does anyone use these anymore?



89
90
91
92
# File 'lib/sinatra/form_helpers.rb', line 89

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 = {}) ⇒ Object

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



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/sinatra/form_helpers.rb', line 132

def select(obj, field, values, options={})
  content = ""
  Array(values).each do |val|
    id, text = id_and_text_from_value(val)
    if val.is_a? Array
      content << tag(:option, text, :value => id)
    else
      content << tag(:option, text, :value => id)
    end
  end
  tag :select, content, options.merge(:id => css_id(obj, field), :name => "#{obj}[#{field}]")
end

#single_tag(name, options = {}) ⇒ Object

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

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



163
164
165
# File 'lib/sinatra/form_helpers.rb', line 163

def single_tag(name, options={})
  "<#{name.to_s} #{hash_to_html_attrs(options)} />"
end

#submit(value = 'Submit', options = {}) ⇒ Object

Form submit tag.



83
84
85
86
# File 'lib/sinatra/form_helpers.rb', line 83

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 = {}) ⇒ Object

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

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



154
155
156
157
158
# File 'lib/sinatra/form_helpers.rb', line 154

def tag(name, content, options={})
  "<#{name.to_s}" +
    (options.length > 0 ? " #{hash_to_html_attrs(options)}" : '') + 
    (content.nil? ? '>' : ">#{content}</#{name}>")
end

#textarea(obj, field = nil, content = '', options = {}) ⇒ Object

Form textarea box.



74
75
76
77
78
79
80
# File 'lib/sinatra/form_helpers.rb', line 74

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) ⇒ Object



171
172
173
# File 'lib/sinatra/form_helpers.rb', line 171

def titleize(text)
  text.to_s.gsub(/_+/, ' ').gsub(/\b('?[a-z])/) { $1.capitalize }
end