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

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 => options, :term => term, :method => method)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 91

def get_autocomplete_items(parameters)
  model = relation = parameters[:model]
  method = parameters[:method]
  options = parameters[:options]
  term = parameters[:term]
  is_full_search = options[:full]

  limit = get_autocomplete_limit(options)
  implementation = get_implementation(model)
  order = get_autocomplete_order(implementation, method, options)

  case implementation
    when :mongoid
      search = (is_full_search ? '.*' : '^') + term + '.*'
      items = model.where(method.to_sym => /#{search}/i).limit(limit).order_by(order)
    when :activerecord
      relation = model.select([:id, method] + (options[:extra_data].blank? ? [] : options[:extra_data])) unless options[:full_model]
      items = relation.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



75
76
77
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 75

def get_autocomplete_limit(options)
  options[:limit] ||= 10
end

#get_autocomplete_order(implementation, method, options) ⇒ Object

Returns the order parameter to be used in the query created by get_items



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 50

def get_autocomplete_order(implementation, method, options)
  order = options[: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



32
33
34
35
36
37
38
39
40
41
# 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
  else
    raise NotImplementedError
  end
end

#get_items(parameters) ⇒ Object

DEPRECATED



80
81
82
83
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 80

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



69
70
71
72
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 69

def get_limit(options)
  warn 'Rails3JQueryAutocomplete#get_limit is has been DEPRECATED, please use #get_autocomplete_limit instead'
  get_autocomplete_limit(options)
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



44
45
46
47
# File 'lib/rails3-jquery-autocomplete/helpers.rb', line 44

def get_order(implementation, method, options)
  warn 'Rails3JQueryAutocomplete#get_order is has been DEPRECATED, please use #get_autocomplete_order instead'
  get_autocomplete_order(implementation, method, options)
end

#json_for_autocomplete(items, method, extra_data) ⇒ 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)
  items.collect do |item|
    hash = {"id" => item.id, "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