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, model = nil) ⇒ 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, extra_data = nil) ⇒ Object
Returns a hash with three keys actually used by the Autocomplete jQuery-ui Can be overriden to show whatever you like Hash also includes a key/value pair for each method in extra_data.
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)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 103 def get_autocomplete_items(parameters) model = parameters[:model] term = parameters[:term] method = parameters[:method] = parameters[:options] is_full_search = [:full] scopes = Array([:scopes]) limit = get_autocomplete_limit() implementation = get_implementation(model) order = get_autocomplete_order(implementation, method, , model) like_clause = (defined?(PGconn) ? 'ILIKE' : 'LIKE') implementation == :mongo_mapper ? (items = model.query) : items = model.scoped scopes.each { |scope| items = items.send(scope) } unless scopes.empty? case implementation when :mongoid search = (is_full_search ? '.*' : '^') + term + '.*' items = model.where(method.to_sym => /#{search}/i).limit(limit).order_by(order) when :mongo_mapper search = (is_full_search ? '.*' : '^') + term + '.*' items = model.where(method.to_sym => /#{search}/i).limit(limit).sort(order) when :activerecord table_name = model.table_name items = items.select(["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + ([:extra_data].blank? ? [] : [:extra_data])) unless [:full_model] items = items.where(["LOWER(#{table_name}.#{method}) #{like_clause} ?", "#{(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
87 88 89 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 87 def get_autocomplete_limit() [:limit] ||= 10 end |
#get_autocomplete_order(implementation, method, options, model = nil) ⇒ Object
Returns the order parameter to be used in the query created by get_items
52 53 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 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 52 def get_autocomplete_order(implementation, method, , model=nil) 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 :mongo_mapper 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 table_prefix = model ? "#{model.table_name}." : "" order || "#{table_prefix}#{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
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 32 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 elsif ancestors_ary.include?('MongoMapper::Document') :mongo_mapper else raise NotImplementedError end end |
#get_items(parameters) ⇒ Object
DEPRECATED
92 93 94 95 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 92 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
81 82 83 84 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 81 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
26 27 28 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 26 def get_object(model_sym) object = model_sym.to_s.camelize.constantize end |
#get_order(implementation, method, options) ⇒ Object
DEPRECATED
46 47 48 49 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 46 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, extra_data = nil) ⇒ Object
Returns a hash with three keys actually used by the Autocomplete jQuery-ui Can be overriden to show whatever you like Hash also includes a key/value pair for each method in extra_data
11 12 13 14 15 16 17 18 19 |
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 11 def json_for_autocomplete(items, method, extra_data=nil) items.collect do |item| hash = {"id" => item.id.to_s, "label" => item.send(method), "value" => item.send(method)} extra_data.each do |datum| hash[datum] = item.send(datum) end if extra_data hash end end |