Module: BivouacHelpers::FormView
- Defined in:
- lib/bivouac/helpers/view/goh/form.rb
Instance Method Summary collapse
-
#auto_complete_field(field_id, options = nil) ⇒ Object
Adds AJAX autocomplete functionality to the text input field with the DOM ID specified by
field_id
. -
#button_to_function(name, *args, &block) ⇒ Object
Returns a button that’ll trigger a JavaScript function using the onclick handler.
- #editable_content(options) ⇒ Object
-
#form_remote_tag(options = {}, &block) ⇒ Object
Returns a form tag that will submit using XMLHttpRequest in the background instead of the regular reloading POST arrangement.
-
#form_tag(url, options = {}, &block) ⇒ Object
Starts a form tag that points the action to an url.
-
#submit_to_remote(name, value, options = {}) ⇒ Object
Returns a button input tag that will submit form using XMLHttpRequest in the background instead of regular reloading POST arrangement.
-
#text_field(field_name, value = "", options = {}) ⇒ Object
Autocomplete options :.
Instance Method Details
#auto_complete_field(field_id, options = nil) ⇒ Object
Adds AJAX autocomplete functionality to the text input field with the DOM ID specified by field_id
.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/bivouac/helpers/view/goh/form.rb', line 6 def auto_complete_field( field_id, = nil ) if .has_key?( :indicator ) data = [:indicator] [:indicator] = "indicator_#{field_id}" span( data, :id => [:indicator], :style => "display: none" ) end choises_id = "choises_#{field_id}" div( :id => choises_id, :class => "autocomplete" ) do; end url = .delete( :url ) javascript_tag "new Ajax.Autocompleter( '#{field_id}', '#{choises_id}', '#{url}', #{()} );" end |
#button_to_function(name, *args, &block) ⇒ Object
Returns a button that’ll trigger a JavaScript function using the onclick handler.
The function argument can be omitted in favor of an update_page block, which evaluates to a string when the template is rendered (instead of making an Ajax request first).
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/bivouac/helpers/view/goh/form.rb', line 87 def (name, *args, &block) = args.last.is_a?(Hash) ? args.pop : {} function = args[0] || '' function = update_page(&block) if block_given? input( .merge({ :type => "button", :value => name, :onclick => ([:onclick] ? "#{[:onclick]}; " : "") + "#{function};" })) end |
#editable_content(options) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bivouac/helpers/view/goh/form.rb', line 20 def editable_content() [:content] = { :element => 'span' }.merge([:content]) # options[:url] = {}.merge(options[:url]) [:ajax] = { :okText => "'Save'", :cancelText => "'Cancel'"}.merge([:ajax] || {}) script = Array.new script << "new Ajax.InPlaceEditor(" script << " '#{[:content][:options][:id]}'," script << " '#{[:url]}'," script << " {" script << [:ajax].map{ |key, value| "#{key.to_s}: #{value}" }.join(", ") script << " }" script << ")" send( [:content][:element], [:content][:text], [:content][:options] ) javascript_tag( script.join("\n") ) end |
#form_remote_tag(options = {}, &block) ⇒ Object
Returns a form tag that will submit using XMLHttpRequest in the background instead of the regular reloading POST arrangement. Even though it’s using JavaScript to serialize the form elements, the form submission will work just like a regular submission as viewed by the receiving side. The options for specifying the target with :url and defining callbacks is the same as link_to_remote.
A “fall-through” target for browsers that doesn’t do JavaScript can be specified with the :action/:method options on :html.
Example:
form_remote_tag :html => { :url => R(SomePlace) }
The Hash passed to the :html key is equivalent to the options (2nd) argument in the form_tag method.
By default the fall-through action is the same as the one specified in the :url (and the default method is :post).
form_remote_tag takes a block, like form_tag:
form_remote_tag :url => '/posts' do
div do; input( :type => 'submit', :name => 'submit' :value => 'Save' ); end
end
183 184 185 186 187 188 189 190 191 192 |
# File 'lib/bivouac/helpers/view/goh/form.rb', line 183 def form_remote_tag( = {}, &block) [:form] = true [:html] ||= {} [:html][:onsubmit] = ([:html][:onsubmit] ? [:html][:onsubmit] + "; " : "") + "#{remote_function()}; return false;" form_tag( [:html].delete(:url), [:html], &block ) end |
#form_tag(url, options = {}, &block) ⇒ Object
Starts a form tag that points the action to an url. The method for the form defaults to POST.
Examples:
-
form_tag('/posts') => <form action="/posts" method="post">
-
form_tag('/upload', :multipart => true) => <form action="/upload" method="post" enctype="multipart/form-data">
Example:
form_tag R(Post) do
div do; input( :type => 'submit', :name => 'submit' :value => 'Save' ); end
end
Will output:
<form action="/posts" method="post"><input type="submit" name="submit" value="Save" /></div></form>
Options:
-
:multipart
- If set to true, the enctype is set to “multipart/form-data”. -
:method
- The method to use when submitting the form, usually either “get” or “post”.If "put", "delete", or another verb is used, a hidden input with name _method is added to simulate the verb over post.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/bivouac/helpers/view/goh/form.rb', line 133 def form_tag(url, = {}, &block) [:enctype] = "multipart/form-data" if .delete(:multipart) [:action] = url method_tag = false case method = .delete(:method).to_s when /^get$/i # must be case-insentive, but can't use downcase as might be nil [:method] = "get" when /^post$/i, "", nil [:method] = "post" else [:method] = "post" method_tag = true end if block_given? form( ) do yield( ) input( :type => "hidden", :name => "_method", :value => method ) if method_tag end else form( ) do; end end end |
#submit_to_remote(name, value, options = {}) ⇒ Object
Returns a button input tag that will submit form using XMLHttpRequest in the background instead of regular reloading POST arrangement. options
argument is the same as in form_remote_tag
.
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/bivouac/helpers/view/goh/form.rb', line 102 def submit_to_remote(name, value, = {}) [:with] ||= 'Form.serialize(this.form)' [:html] ||= {} [:html][:type] = 'button' [:html][:onclick] = "#{remote_function()}; return false;" [:html][:name] = name [:html][:value] = value input [:html] end |
#text_field(field_name, value = "", options = {}) ⇒ Object
Autocomplete options :
:url : URL to call for autocompletion results
:tokens:
:frequency :
:minChars :
:indicator : When sending the Ajax request Autocompleter shows this
:updateElement : Hook for a custom function called after the element has been updated (i.e. when the user has selected an entry).
:afterUpdateElement : Hook for a custom function called after the element has been updated (i.e. when the user has selected an entry).
:callback : This function is called just before the Request is actually made, allowing you to modify the querystring that is sent to the server.
:parameters : If you need to send any additional parameters through your search form, add them here, in the usual {field: 'value',another: 'value'} or 'field=value&another=value' manner.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/bivouac/helpers/view/goh/form.rb', line 54 def text_field(field_name, value = "", = {}) = nil if .has_key?( :autocomplete ) unless ["on", "off"].include?( [:autocomplete] ) = Hash.new( ) [:url] = .delete( :autocomplete ) # options[:autocomplete] = "off" end end [:tokens, :frequency, :minChars, :indicator, :updateElement, :afterUpdateElement, :callback, :parameters].each do |op| if .has_key?( op ) if .nil? .delete( op ) else [op] = .delete( op ) end end end = { :id => "default_#{Time.now.to_i}", :type => "text", :name => field_name, :value => value }.merge( ) input( ) auto_complete_field( [:id], ) unless .nil? end |