Module: BootstrapAutocompleteInput::Orm::Mongoid

Defined in:
lib/bootstrap_autocomplete_input/orm/mongoid.rb

Instance Method Summary collapse

Instance Method Details

#get_autocomplete_items(parameters) ⇒ Object



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
# File 'lib/bootstrap_autocomplete_input/orm/mongoid.rb', line 16

def get_autocomplete_items(parameters)
  model          = parameters[:model]
  method         = parameters[:method]
  options        = parameters[:options]
  scopes         = options[:scopes]
  is_full_search = options[:full]
  term           = parameters[:term]
  limit          = autocomplete_option_limit(options)
  order          = get_autocomplete_order(method, options)

  if is_full_search
    search = '.*' + Regexp.escape(term) + '.*'
  else
    search = '^' + Regexp.escape(term)
  end

  items = model.where(method.to_sym => /#{search}/i).limit(limit).order_by(order)

  case scopes
    when Symbol then
      items = items.send(scopes)
    when Array then
      scopes.each { |scope| items = items.send(scope) } unless scopes.empty?
  end
  items
end

#get_autocomplete_order(method, options, model = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/bootstrap_autocomplete_input/orm/mongoid.rb', line 4

def get_autocomplete_order(method, options, model=nil)
  order = options[:order]
  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
end