Module: Useful::SinatraHelpers::Erb::Forms

Includes:
Helpers
Defined in:
lib/useful/sinatra_helpers/erb/forms.rb

Instance Method Summary collapse

Methods included from Helpers

#sinatra_erb_helper_capture, #sinatra_erb_helper_checked_option, #sinatra_erb_helper_disabled_option, #sinatra_erb_helper_hash_to_html_attrs, #sinatra_erb_helper_multipart_option, #sinatra_erb_helper_multiple_option, #sinatra_erb_helper_safe_id, #sinatra_erb_helper_with_output_buffer

Instance Method Details

#check_box_tag(name, label = nil, value = '1', checked = false, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/useful/sinatra_helpers/erb/forms.rb', line 54

def check_box_tag(name, label=nil, value='1', checked=false, options={})
  tag_name = options.delete(:tag) || :div
  if options.has_key?(:class)
    options[:class] += ' checkbox'
  else
    options[:class] = 'checkbox'
  end
  options[:id] ||= sinatra_erb_helper_safe_id(rand(1000).to_s)
  options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
  options[:checked] = sinatra_erb_helper_checked_option if checked
  input_str = input_tag('checkbox', name, value, options)
  if label.nil?
    input_str
  else
    tag(tag_name, :class => 'checkbox') { input_str + label_tag(options[:id], label)  }
  end
end

#field_set_tag(legend = nil, options = {}, &block) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/useful/sinatra_helpers/erb/forms.rb', line 25

def field_set_tag(legend=nil, options={}, &block)
  legend_html = legend.nil? ? '' : tag(:legend) { legend.to_s }
  if block_given?
    @_out_buf << tag(:fieldset, options) { legend_html + sinatra_erb_helper_capture(&block) }
  else
    tag(:fieldset, options) { legend_html }
  end
end

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



49
50
51
52
# File 'lib/useful/sinatra_helpers/erb/forms.rb', line 49

def file_field_tag(name, options={})
  options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
  input_tag('file', name, nil, options)
end

#form_tag(url, options = {}, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/useful/sinatra_helpers/erb/forms.rb', line 12

def form_tag(url, options={}, &block) 
  options[:method] = 'post' unless ['get','post','put','delete'].include?(options[:method])
  options.update :action => url
  if multipart = options.delete(:multipart)
    options[:enctype] = sinatra_erb_helper_multipart_option
  end
  if block_given?
    @_out_buf << tag(:form, options) { sinatra_erb_helper_capture(&block) }
  else
    tag(:form, options)
  end
end

#hidden_field_tag(name, value = nil, options = {}) ⇒ Object



40
41
42
# File 'lib/useful/sinatra_helpers/erb/forms.rb', line 40

def hidden_field_tag(name, value=nil, options={}) 
  input_tag('hidden', name, value, options)
end

#image_submit_tag(source, options = {}) ⇒ Object



119
120
121
122
123
124
# File 'lib/useful/sinatra_helpers/erb/forms.rb', line 119

def image_submit_tag(source, options={})
  options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
  options[:src] = source
  options[:alt] ||= 'Save'
  input_tag('image', nil, nil, options)
end

#label_tag(name, value = nil, options = {}) ⇒ Object



34
35
36
37
38
# File 'lib/useful/sinatra_helpers/erb/forms.rb', line 34

def label_tag(name, value=nil, options={})
  value ||= name.to_s.capitalize
  options.update :for => name.to_s
  tag(:label, options) { value }
end

#password_field_tag(name = "password", value = nil, options = {}) ⇒ Object



44
45
46
47
# File 'lib/useful/sinatra_helpers/erb/forms.rb', line 44

def password_field_tag(name="password", value=nil, options={}) 
  options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
  input_tag('password', name, value, options)
end

#radio_button_tag(name, value, label = nil, checked = false, options = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/useful/sinatra_helpers/erb/forms.rb', line 72

def radio_button_tag(name, value, label=nil, checked=false, options={}) 
  tag_name = options.delete(:tag) || :span
  if options.has_key?(:class)
    options[:class] += ' radiobutton'
  else
    options[:class] = 'radiobutton'
  end
  label ||= value.to_s.capitalize
  options[:id] ||= sinatra_erb_helper_safe_id(rand(1000).to_s)
  options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
  options[:checked] = sinatra_erb_helper_checked_option if checked
  input_str = input_tag('radio', name, value, options)
  if label.nil?
    input_str
  else
    label_tag(options[:id], input_str + tag(tag_name) { label }, :class => options.delete(:class))
  end
end

#select_tag(name, options = {}, &block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/useful/sinatra_helpers/erb/forms.rb', line 91

def select_tag(name, options={}, &block) 
  options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
  html_name = (options[:multiple] == true && !name.to_s[(name.to_s.length-2)..-1] == "[]") ? "#{name}[]" : name
  options[:multiple] = sinatra_erb_helper_multiple_option if options[:multiple] == true
  options[:tag] = 'select'
  if block_given?
    @_out_buf << input_tag(:select, html_name, nil, options) { sinatra_erb_helper_capture(&block) }
  else
    input_tag(:select, html_name, nil, options)
  end
end

#submit_tag(value = "Save", options = {}) ⇒ Object



114
115
116
117
# File 'lib/useful/sinatra_helpers/erb/forms.rb', line 114

def submit_tag(value="Save", options={}) 
  options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
  input_tag('submit', 'commit', value, options)
end

#text_area_tag(name, content = nil, options = {}) ⇒ Object



103
104
105
106
107
# File 'lib/useful/sinatra_helpers/erb/forms.rb', line 103

def text_area_tag(name, content=nil, options={}) 
  options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
  options[:tag] = 'textarea'
  input_tag(nil, name, nil, options) { content || '' }
end

#text_field_tag(name, value = nil, options = {}) ⇒ Object



109
110
111
112
# File 'lib/useful/sinatra_helpers/erb/forms.rb', line 109

def text_field_tag(name, value=nil, options={}) 
  options[:disabled] = sinatra_erb_helper_disabled_option if options[:disabled]
  input_tag('text', name, value, options)
end