Module: Useful::ErbHelpers::Forms

Includes:
Common
Defined in:
lib/useful/erb_helpers/forms.rb

Constant Summary

Constants included from Common

Common::OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#erb_helper_clear_output_buffer

Class Method Details

.included(receiver) ⇒ Object

Note: purposely left out:

> select_tag

> check_box_tag

> radio_button_tag

Not going to reinvent the inferior actionpack versions here.

> prefer, instead, the corresponding ‘proper’ versions

> see useful/erb_helpers/proper.rb (which is included in useful/rails_extensions/erb)



114
115
116
# File 'lib/useful/erb_helpers/forms.rb', line 114

def self.included(receiver)
  receiver.send :include, Useful::ErbHelpers::Tags
end

Instance Method Details

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



31
32
33
34
35
36
37
38
39
# File 'lib/useful/erb_helpers/forms.rb', line 31

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

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



59
60
61
# File 'lib/useful/erb_helpers/forms.rb', line 59

def file_field_tag(name, options={})
  input_tag('file', name, nil, options)
end

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



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/useful/erb_helpers/forms.rb', line 17

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] = OPTIONS[:multipart]
  end
  if block_given?
    @_out_buf ||= ''
    @_out_buf << tag(:form, options) { erb_helper_common_capture(&block) }
  else
    tag(:form, options)
  end
end

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



47
48
49
# File 'lib/useful/erb_helpers/forms.rb', line 47

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

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

Special options:

> :confirm - string

=> will add js confirm confirmation before submitting


80
81
82
83
84
85
86
87
88
# File 'lib/useful/erb_helpers/forms.rb', line 80

def image_submit_tag(source, options={})
  options[:src] = ['/'].include?(source[0..0]) ? source : "/images/#{source}"
  options[:alt] ||= OPTIONS[:default_submit_value]
  if options.has_key?(:confirm)
    options[:onclick] ||= 'return true;'
    options[:onclick] = "if (!#{erb_helper_confirm_javascript(options.delete(:confirm))}) return false; #{options[:onclick]}"
  end
  input_tag('image', nil, nil, options)
end

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



41
42
43
44
45
# File 'lib/useful/erb_helpers/forms.rb', line 41

def label_tag(name, value=nil, options={})
  value ||= name.to_s.gsub(/\[/, '_').gsub(/\]/, '').humanize
  options[:for] ||= erb_helper_common_safe_id(name)
  tag(:label, options) { value }
end

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



55
56
57
# File 'lib/useful/erb_helpers/forms.rb', line 55

def password_field_tag(name="password", value=nil, options={}) 
  input_tag('password', name, value, options)
end

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

Special options:

> :disable_with - string

=> will add js onclick event to first disable submit, setting text to value, and then submitting form

> :confirm - string

=> will add js confirm confirmation before submitting


68
69
70
71
72
73
74
75
# File 'lib/useful/erb_helpers/forms.rb', line 68

def submit_tag(value=OPTIONS[:default_submit_value], options={})
  options[:onclick] = erb_helper_disable_with_javascript(options.delete(:disabled_with)) if options.has_key?(:disabled_with)
  if options.has_key?(:confirm)
    options[:onclick] ||= 'return true;'
    options[:onclick] = "if (!#{erb_helper_confirm_javascript(options.delete(:confirm))}) return false; #{options[:onclick]}"
  end
  input_tag('submit', 'commit', value, options)
end

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

Special options:

> :size - A string specifying the dimensions (columns by rows) of the textarea (e.g., “25x10”).

> :rows - Specify the number of rows in the textarea

> :cols - Specify the number of columns in the textarea

> :escape - By default, the contents of the text input are HTML escaped. If you need unescaped contents, set this to false.



95
96
97
98
99
100
101
102
103
104
# File 'lib/useful/erb_helpers/forms.rb', line 95

def text_area_tag(name, content=nil, options={}) 
  options[:tag] = 'textarea'
  if size = options.delete(:size)
    options[:cols], options[:rows] = size.split("x") if size.respond_to?(:split)
  end
  unless options.has_key?(:escape) && options.delete(:escape).false?
    content = escape_html(content)
  end
  input_tag(nil, name, nil, options) { content || '' }
end

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



51
52
53
# File 'lib/useful/erb_helpers/forms.rb', line 51

def text_field_tag(name, value=nil, options={}) 
  input_tag('text', name, value, options)
end