Module: Formz::Helpers
- Defined in:
- lib/formz/helpers.rb
Overview
Formz::Helpers
The Formz:Helpers module provides unbound form related helper methods such as #checkbox, #password and others.
Defined Under Namespace
Modules: Delegates
Instance Method Summary collapse
-
#buttons(attrs = {}, &block) ⇒ Object
Wrap form buttons defined within block in a div.
-
#fieldset(name, legend = nil, attrs = {}, &block) ⇒ Object
Return a fieldset with optional legend.
-
#form(name, attrs = {}, &block) ⇒ Object
Return a form.
-
#group(name, &block) ⇒ Object
Wrap form elements in div name.
-
#legend(contents, attrs = {}, &block) ⇒ Object
Return a legend with contents.
-
#normalize_select_prompt(values, options = {}) ⇒ Object
Normalize select prompt.
-
#option_selected?(key, selected) ⇒ Boolean
Check if option key is selected.
-
#select(name, options, attrs = {}) ⇒ Object
Return select element name with options hash.
-
#select_option(value, contents, attrs = {}) ⇒ Object
Return select option contents with value.
-
#select_options(values, options = {}) ⇒ Object
Return select option elements from values with options passed.
-
#textarea(name, contents = nil, attrs = {}, &block) ⇒ Object
Return a textarea.
Instance Method Details
#buttons(attrs = {}, &block) ⇒ Object
Wrap form buttons defined within block in a div.
52 53 54 |
# File 'lib/formz/helpers.rb', line 52 def attrs = {}, &block Tagz.tag :div, { :class => 'form-buttons' }.merge(attrs), &block end |
#fieldset(name, legend = nil, attrs = {}, &block) ⇒ Object
Return a fieldset with optional legend.
29 30 31 32 |
# File 'lib/formz/helpers.rb', line 29 def fieldset name, legend = nil, attrs = {}, &block attrs, legend = legend, nil if legend.is_a? Hash Tagz.tag :fieldset, legend ? legend(legend) : nil, { :id => "fieldset-#{name}" }.merge(attrs), &block end |
#form(name, attrs = {}, &block) ⇒ Object
Return a form.
15 16 17 |
# File 'lib/formz/helpers.rb', line 15 def form name, attrs = {}, &block Tagz.tag :form, { :id => "form-#{name}" }.merge(attrs), &block end |
#group(name, &block) ⇒ Object
Wrap form elements in div name.
45 46 47 |
# File 'lib/formz/helpers.rb', line 45 def group name, &block Tagz.tag :div, { :class => "group-#{name}" }, &block end |
#legend(contents, attrs = {}, &block) ⇒ Object
Return a legend with contents.
22 23 24 |
# File 'lib/formz/helpers.rb', line 22 def legend contents, attrs = {}, &block Tagz.tag :legend, contents, attrs, &block end |
#normalize_select_prompt(values, options = {}) ⇒ Object
Normalize select prompt.
-
When options contains a :prompt string it is assigned as the prompt
-
When :prompt is true the default of ‘- Select -’ will become the prompt
-
The prompt is selected unless a specific option is explicitly selected.
135 136 137 138 139 140 |
# File 'lib/formz/helpers.rb', line 135 def normalize_select_prompt values, = {} return unless :prompt.in? prompt = .delete :prompt [:selected] = '' unless :selected.in? values[''] = String === prompt ? prompt : '- Select -' end |
#option_selected?(key, selected) ⇒ Boolean
Check if option key is selected.
117 118 119 120 121 122 123 124 125 |
# File 'lib/formz/helpers.rb', line 117 def option_selected? key, selected if Array === selected selected.any? do |value| key.to_s == value.to_s end else key.to_s == selected.to_s end end |
#select(name, options, attrs = {}) ⇒ Object
Return select element name with options hash.
79 80 81 82 83 |
# File 'lib/formz/helpers.rb', line 79 def select name, , attrs = {} = , attrs attrs.delete :selected Tagz.tag :select, , { :name => name }.merge(attrs) end |
#select_option(value, contents, attrs = {}) ⇒ Object
Return select option contents with value.
88 89 90 |
# File 'lib/formz/helpers.rb', line 88 def select_option value, contents, attrs = {} Tagz.tag :option, contents, { :value => value }.merge(attrs) end |
#select_options(values, options = {}) ⇒ Object
Return select option elements from values with options passed.
Options
:selected string, symbol, or array of options selected
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/formz/helpers.rb', line 101 def values, = {} normalize_select_prompt values, values.map do |key, value| if value.is_a? Hash Tagz.tag :optgroup, (value, ), :label => key elsif option_selected? key, [:selected] select_option key, value, :selected => true else select_option key, value end end.join end |
#textarea(name, contents = nil, attrs = {}, &block) ⇒ Object
Return a textarea.
37 38 39 40 |
# File 'lib/formz/helpers.rb', line 37 def textarea name, contents = nil, attrs = {}, &block attrs, contents = contents, nil if contents.is_a? Hash Tagz.tag :textarea, contents, { :name => name }.merge(attrs), &block end |