Module: Rails3JQueryAutocomplete::Helpers
- Included in:
- ActionController::Base
- Defined in:
- lib/rails3-jquery-autocomplete/helpers.rb
Overview
Contains utility methods used by autocomplete
Instance Method Summary collapse
-
#get_autocomplete_items(parameters) ⇒ Object
Can be overriden to return or filter however you like the objects to be shown by autocomplete.
-
#get_autocomplete_limit(options) ⇒ Object
Returns a limit that will be used on the query.
-
#get_autocomplete_order(implementation, method, options) ⇒ Object
Returns the order parameter to be used in the query created by get_items.
-
#get_implementation(object) ⇒ Object
Returns a symbol representing what implementation should be used to query the database and raises NotImplementedError if ORM implementor can not be found.
-
#get_items(parameters) ⇒ Object
DEPRECATED.
-
#get_limit(options) ⇒ Object
DEPRECATED.
-
#get_object(model_sym) ⇒ Object
Returns parameter model_sym as a constant.
-
#get_order(implementation, method, options) ⇒ Object
DEPRECATED.
-
#json_for_autocomplete(items, method) ⇒ Object
Returns a three keys hash actually used by the Autocomplete jQuery-ui Can be overriden to show whatever you like.
Instance Method Details
#get_autocomplete_items(parameters) ⇒ Object
Can be overriden to return or filter however you like the objects to be shown by autocomplete
items = get_autocomplete_items(:model => get_object(object), :options => , :term => term, :method => method)
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 84 def get_autocomplete_items(parameters) model = parameters[:model] method = parameters[:method] = parameters[:options] term = parameters[:term] is_full_search = [:full] limit = get_autocomplete_limit() implementation = get_implementation(model) order = get_autocomplete_order(implementation, method, ) case implementation when :mongoid search = (is_full_search ? '.*' : '^') + term + '.*' items = model.where(method.to_sym => /#{search}/i).limit(limit).order_by(order) when :activerecord items = model.where(["LOWER(#{method}) LIKE ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"]) \ .limit(limit).order(order) end end |
#get_autocomplete_limit(options) ⇒ Object
Returns a limit that will be used on the query
68 69 70 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 68 def get_autocomplete_limit() [:limit] ||= 10 end |
#get_autocomplete_order(implementation, method, options) ⇒ Object
Returns the order parameter to be used in the query created by get_items
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 43 def get_autocomplete_order(implementation, method, ) order = [:order] case implementation when :mongoid then if order order.split(',').collect do |fields| sfields = fields.split [sfields[0].downcase.to_sym, sfields[1].downcase.to_sym] end else [[method.to_sym, :asc]] end when :activerecord then order || "#{method} ASC" end end |
#get_implementation(object) ⇒ Object
Returns a symbol representing what implementation should be used to query the database and raises NotImplementedError if ORM implementor can not be found
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 25 def get_implementation(object) ancestors_ary = object.ancestors.collect(&:to_s) if ancestors_ary.include?('ActiveRecord::Base') :activerecord elsif ancestors_ary.include?('Mongoid::Document') :mongoid else raise NotImplementedError end end |
#get_items(parameters) ⇒ Object
DEPRECATED
73 74 75 76 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 73 def get_items(parameters) warn 'Rails3JQueryAutocomplete#get_items is has been DEPRECATED, you should use #get_autocomplete_items instead' get_autocomplete_items(parameters) end |
#get_limit(options) ⇒ Object
DEPRECATED
62 63 64 65 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 62 def get_limit() warn 'Rails3JQueryAutocomplete#get_limit is has been DEPRECATED, please use #get_autocomplete_limit instead' get_autocomplete_limit() end |
#get_object(model_sym) ⇒ Object
Returns parameter model_sym as a constant
get_object(:actor)
# returns a Actor constant supposing it is already defined
19 20 21 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 19 def get_object(model_sym) object = model_sym.to_s.camelize.constantize end |
#get_order(implementation, method, options) ⇒ Object
DEPRECATED
37 38 39 40 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 37 def get_order(implementation, method, ) warn 'Rails3JQueryAutocomplete#get_order is has been DEPRECATED, please use #get_autocomplete_order instead' get_autocomplete_order(implementation, method, ) end |
#json_for_autocomplete(items, method) ⇒ Object
Returns a three keys hash actually used by the Autocomplete jQuery-ui Can be overriden to show whatever you like
10 11 12 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 10 def json_for_autocomplete(items, method) items.collect {|item| {"id" => item.id, "label" => item.send(method), "value" => item.send(method)}} end |