Module: ScopedSearch::RailsHelper
- Defined in:
- lib/scoped_search/rails_helper.rb
Instance Method Summary (collapse)
- - (Object) a_link(name, href, html_options)
- - (Object) auto_complete_clear_value_button(field_id)
-
- (Object) auto_complete_field(field_id, options = {})
Adds AJAX auto complete functionality to the text input field with the DOM ID specified by field_id.
- - (Object) auto_complete_field_jquery(method, url, options = {})
-
- (Object) auto_complete_field_tag(method, val, tag_options = {}, completion_options = {})
Wrapper for text_field with added AJAX auto completion functionality.
-
- (Object) auto_complete_field_tag_jquery(method, val, tag_options = {}, completion_options = {})
Wrapper for text_field with added JQuery auto completion functionality.
-
- (Object) auto_complete_result(entries, phrase = nil)
Use this method in your view to generate a return for the AJAX auto complete requests.
-
- (Object) sort(field, options = {}, html_options = {})
Creates a link that alternates between ascending and descending.
Instance Method Details
- (Object) a_link(name, href, html_options)
170 171 172 173 174 |
# File 'lib/scoped_search/rails_helper.rb', line 170 def a_link(name, href, ) = () link = "<a href=\"#{href}\"#{}>#{name}</a>" return link.respond_to?(:html_safe) ? link.html_safe : link end |
- (Object) auto_complete_clear_value_button(field_id)
165 166 167 168 |
# File 'lib/scoped_search/rails_helper.rb', line 165 def (field_id) = {:tabindex => '-1',:class=>"auto_complete_clear",:title =>'Clear Search', :onclick=>"document.getElementById('#{field_id}').value = '';"} a_link("", "#", ) end |
- (Object) auto_complete_field(field_id, options = {})
Adds AJAX auto complete functionality to the text input field with the DOM ID specified by field_id.
Required options is:
:url |
URL to call for auto completion results in url_for format. |
Additional options are:
:update |
Specifies the DOM ID of the element whose innerHTML should be updated with the auto complete entries returned by the AJAX request. Defaults to field_id + '_auto_complete' |
:with |
A JavaScript expression specifying the parameters for the XMLHttpRequest. This defaults to 'fieldname=value'. |
:frequency |
Determines the time to wait after the last keystroke for the AJAX request to be initiated. |
:indicator |
Specifies the DOM ID of an element which will be displayed while auto complete is running. |
:tokens |
A string or an array of strings containing separator tokens for tokenized incremental auto completion. Example: :tokens => ',' would allow multiple auto completion entries, separated by commas. |
:min_chars |
The minimum number of characters that should be in the input field before an Ajax call is made to the server. |
:on_hide |
A Javascript expression that is called when the auto completion div is hidden. The expression should take two variables: element and update. Element is a DOM element for the field, update is a DOM element for the div from which the innerHTML is replaced. |
:on_show |
Like on_hide, only now the expression is called then the div is shown. |
:after_update_element |
A Javascript expression that is called when the user has selected one of the proposed values. The expression should take two variables: element and value. Element is a DOM element for the field, value is the value selected by the user. |
:select |
Pick the class of the element from which the value for insertion should be extracted. If this is not specified, the entire element is used. |
:method |
Specifies the HTTP verb to use when the auto completion request is made. Defaults to POST. |
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/scoped_search/rails_helper.rb', line 93 def auto_complete_field(field_id, = {}) function = "var #{field_id}_auto_completer = new Ajax.Autocompleter(" function << "'#{field_id}', " function << "'" + ([:update] || "#{field_id}_auto_complete") + "', " function << "'#{url_for([:url])}'" = {} [:tokens] = array_or_string_for_javascript([:tokens]) if [:tokens] [:callback] = "function(element, value) { return #{[:with]} }" if [:with] [:indicator] = "'#{[:indicator]}'" if [:indicator] [:select] = "'#{[:select]}'" if [:select] [:paramName] = "'#{[:param_name]}'" if [:param_name] [:frequency] = "#{[:frequency]}" if [:frequency] [:method] = "'#{[:method].to_s}'" if [:method] { :after_update_element => :afterUpdateElement, :on_show => :onShow, :on_hide => :onHide, :min_chars => :minChars }.each do |k,v| [v] = [k] if [k] end function << (', ' + () + ')') javascript_tag(function) end |
- (Object) auto_complete_field_jquery(method, url, options = {})
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 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 158 159 160 161 162 163 |
# File 'lib/scoped_search/rails_helper.rb', line 118 def auto_complete_field_jquery(method, url, = {}) function = <<-EOF $.widget( "custom.catcomplete", $.ui.autocomplete, { _renderMenu: function( ul, items ) { var self = this, currentCategory = ""; $.each( items, function( index, item ) { if ( item.category != undefined && item.category != currentCategory ) { ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" ); currentCategory = item.category; } if ( item.error != undefined ) { ul.append( "<li class='ui-autocomplete-error'>" + item.error + "</li>" ); } if( item.completed != undefined ) { $( "<li></li>" ).data( "item.autocomplete", item ) .append( "<a>" + "<strong class='ui-autocomplete-completed'>" + item.completed + "</strong>" + item.part + "</a>" ) .appendTo( ul ); } else { self._renderItem( ul, item ); } }); } }); $("##{method}") .catcomplete({ source: function( request, response ) { $.getJSON( "#{url}", { #{method}: request.term }, response ); }, minLength: #{[:min_length] || 0}, delay: #{[:delay] || 200}, select: function(event, ui) { $( this ).catcomplete( "search" , ui.item.value); }, search: function(event, ui) { $(".auto_complete_clear").hide(); }, open: function(event, ui) { $(".auto_complete_clear").show(); } }); $("##{method}").bind( "focus", function( event ) { if( $( this )[0].value == "" ) { $( this ).catcomplete( "search" ); } }); EOF javascript_tag(function) end |
- (Object) auto_complete_field_tag(method, val, tag_options = {}, completion_options = {})
Wrapper for text_field with added AJAX auto completion functionality.
In your controller, you'll need to define an action called auto_complete_method to respond the AJAX calls,
190 191 192 193 194 195 196 197 |
# File 'lib/scoped_search/rails_helper.rb', line 190 def auto_complete_field_tag(method, val, = {}, = {}) = { :url => { :action => "auto_complete_#{method}" } }.update() = .merge(:class => "auto_complete_input " << [:class].to_s) text_field_tag(method, val,) + (method) + content_tag("div", "", :id => "#{method}_auto_complete", :class => "auto_complete") + auto_complete_field(method, ) end |
- (Object) auto_complete_field_tag_jquery(method, val, tag_options = {}, completion_options = {})
Wrapper for text_field with added JQuery auto completion functionality.
In your controller, you'll need to define an action called auto_complete_method to respond the JQuery calls,
203 204 205 206 207 208 |
# File 'lib/scoped_search/rails_helper.rb', line 203 def auto_complete_field_tag_jquery(method, val, = {}, = {}) url = url_for(:action => "auto_complete_#{method}", :filter => [:filter]) = .merge(:class => "auto_complete_input " << [:class].to_s) text_field_tag(method, val, ) + (method) + auto_complete_field_jquery(method, url, ) end |
- (Object) auto_complete_result(entries, phrase = nil)
Use this method in your view to generate a return for the AJAX auto complete requests.
The auto_complete_result can of course also be called from a view belonging to the auto_complete action if you need to decorate it further.
180 181 182 183 184 |
# File 'lib/scoped_search/rails_helper.rb', line 180 def auto_complete_result(entries, phrase = nil) return unless entries items = entries.map { |entry| content_tag("li", phrase ? highlight(entry, phrase) : h(entry)) } content_tag("ul", items) end |
- (Object) sort(field, options = {}, html_options = {})
Creates a link that alternates between ascending and descending.
Examples:
sort @search, :by => :username
sort @search, :by => :created_at, :as => "Created"
This helper accepts the following options:
-
:by - the name of the named scope. This helper will prepend this value with “ascend_by_” and “descend_by_”
-
:as - the text used in the link, defaults to whatever is passed to :by
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/scoped_search/rails_helper.rb', line 15 def sort(field, = {}, = {}) unless [:as] id = field.to_s.downcase == "id" [:as] = id ? field.to_s.upcase : field.to_s.humanize end ascend = "#{field} ASC" descend = "#{field} DESC" ascending = params[:order] == ascend new_sort = ascending ? descend : ascend selected = [ascend, descend].include?(params[:order]) if selected css_classes = [:class] ? [:class].split(" ") : [] if ascending [:as] = "▲ #{[:as]}" css_classes << "ascending" else [:as] = "▼ #{[:as]}" css_classes << "descending" end [:class] = css_classes.join(" ") end = params.merge(:order => new_sort) [:as] = raw([:as]) if defined?(RailsXss) a_link([:as], html_escape(url_for()),) end |