Module: Snails::FormHelpers

Defined in:
lib/snails/app.rb

Instance Method Summary collapse

Instance Method Details



298
299
300
301
302
303
# File 'lib/snails/app.rb', line 298

def delete_link(options = {})
  post_button(options[:text], options[:path], options.merge({
    input_html: "<input type='hidden' name='_method' value='delete' />",
    css_class: options[:css_class] || 'danger'
  }))
end

#form_checkbox(object, field, options = {}) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/snails/app.rb', line 232

def form_checkbox(object, field, options = {})
  id, name, index, label = input_base(object, field, options)

  type  = options[:type] || :checkbox
  value = options[:value] ? "value='#{options[:value]}'" : ''

  if type.to_sym == :radio
    checked = object.send(field) == options[:value]
    value += checked ? " selected='selected'" : ''
  else
    checked = object.send(field)
    value += checked ? " checked='true'" : ''
  end

  label + raw_input(index, type, id, name, options[:placeholder], value)
end

#form_input(object, field, options = {}) ⇒ Object



219
220
221
222
223
224
225
226
# File 'lib/snails/app.rb', line 219

def form_input(object, field, options = {})
  id, name, index, label = input_base(object, field, options)
  type  = (options[:type] || :text).to_sym
  value = options[:value] ? "value=\"#{options[:value]}\"" : (type == :password ? '' : "value=\"#{object.send(field)}\"")

  classes = object.errors[field].any? ? 'has-errors' : ''
  label + raw_input(index, type, id, name, value, options[:placeholder], classes, options[:required])
end

#form_password(object, field, options = {}) ⇒ Object



228
229
230
# File 'lib/snails/app.rb', line 228

def form_password(object, field, options = {})
  form_input(object, field, {:type => 'password'}.merge(options))
end

#form_putObject



270
271
272
# File 'lib/snails/app.rb', line 270

def form_put
  "<input type='hidden' name='_method' value='put' />"
end

#form_select(object, field, option_list, options = {}) ⇒ Object



255
256
257
258
259
260
# File 'lib/snails/app.rb', line 255

def form_select(object, field, option_list, options = {})
  id, name, index, label = input_base(object, field, options)
  style = options[:style] ? "style='#{options[:style]}'" : ''
  options = form_select_options(option_list, object.send(field))
  label + "<select #{style} tabindex='#{index}' id='#{id}' name='#{name}'>#{options}</select>"
end

#form_select_options(list, selected = nil) ⇒ Object



262
263
264
265
266
267
268
# File 'lib/snails/app.rb', line 262

def form_select_options(list, selected = nil)
  list.map do |name, val|
    val = name if val.nil?
    sel = val == selected ? 'selected="selected"' : ''
    "<option #{sel} value=\"#{val}\">#{name}</option>"
  end.join("\n")
end

#form_submit(text = 'Actualizar', classes = '') ⇒ Object



274
275
276
277
# File 'lib/snails/app.rb', line 274

def form_submit(text = 'Actualizar', classes = '')
  @tabindex = @tabindex ? @tabindex + 1 : 1
  "<button tabindex='#{@tabindex}' class='primary #{classes}' type='submit'>#{text}</button>"
end

#form_textarea(object, field, options = {}) ⇒ Object



249
250
251
252
253
# File 'lib/snails/app.rb', line 249

def form_textarea(object, field, options = {})
  id, name, index, label = input_base(object, field, options)
  style = options[:style] ? "style='#{options[:style]}'" : ''
  label + "<textarea #{style} tabindex='#{index}' id='#{id}' name='#{name}'>#{object.send(field)}</textarea>"
end

#post_button(text, path, opts = {}) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/snails/app.rb', line 279

def post_button(text, path, opts = {})
  form_id      = opts.delete(:form_id) || path.gsub(/[^a-z0-9]/, '_').gsub(/_+/, '_')
  css_class    = opts.delete(:css_class) || ''
  input_html   = opts.delete(:input_html) || ''
  submit_val   = opts.delete(:value) || text
  style        = "style='display:inline; #{opts[:style]}'"

  onsubmit = if str = opts[:onsubmit]
    "onsubmit=\"#{str}\""
  else
    confirm_text = opts.delete(:confirm_text) || 'Seguro?'
    "onsubmit='return confirm(\"#{confirm_text}\");'"
  end

  form_tag = "<form id='#{form_id}' method='post' action='#{url(path)}' #{style} #{onsubmit}>"
  button = "<button name='submit' type='submit' class='button #{css_class}' value='#{submit_val}'>#{text}</button>"
  form_tag + input_html + button + '</form>'
end