Module: RailsAutocomplete::Orm::ActiveRecord

Defined in:
lib/rails-autocomplete/orm/active_record.rb

Instance Method Summary collapse

Instance Method Details

#active_record_get_autocomplete_items(parameters) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails-autocomplete/orm/active_record.rb', line 15

def active_record_get_autocomplete_items(parameters)
  model   = parameters[:model]
  term    = parameters[:term]
  options = parameters[:options]
  method  = options[:hstore] ? options[:hstore][:method] : parameters[:method]
  scopes  = Array(options[:scopes])
  where   = options[:where]
  limit   = get_autocomplete_limit(options)
  order   = active_record_get_autocomplete_order(method, options, model)

  items = (::Rails::VERSION::MAJOR * 10 + ::Rails::VERSION::MINOR) >= 40 ? model.where(nil) : model.scoped

  scopes.each { |scope| items = items.send(scope) } unless scopes.empty?

  items = items.select(get_autocomplete_select_clause(model, method, options)) unless options[:full_model]
  items = items.where(get_autocomplete_where_clause(model, term, method, options)).
      limit(limit).order(order)
  items = items.where(where) unless where.blank?

  items
end

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



4
5
6
7
8
9
10
11
12
13
# File 'lib/rails-autocomplete/orm/active_record.rb', line 4

def active_record_get_autocomplete_order(method, options, model=nil)
  order = options[:order]

  table_prefix = model ? "#{model.table_name}." : ""
  if sqlite?
    order || "LOWER(#{method}) ASC"
  else
    order || "LOWER(#{table_prefix}#{method}) ASC"
  end
end

#get_autocomplete_select_clause(model, method, options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails-autocomplete/orm/active_record.rb', line 37

def get_autocomplete_select_clause(model, method, options)
  if sqlite?
    table_name = model.quoted_table_name
    ([
        "#{table_name}.#{model.connection.quote_column_name(model.primary_key)} as #{model.primary_key}",
        "#{table_name}.#{model.connection.quote_column_name(method)} as #{method}"
      ] + (options[:extra_data].blank? ? [] : options[:extra_data]))
  else
    table_name = model.table_name
    (["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + (options[:extra_data].blank? ? [] : options[:extra_data]))
  end
end

#get_autocomplete_where_clause(model, term, method, options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rails-autocomplete/orm/active_record.rb', line 50

def get_autocomplete_where_clause(model, term, method, options)
  table_name = model.table_name
  is_full_search = options[:full]
  is_case_sensitive_search = options[:case_sensitive]
  like_clause = (postgres?(model) && !is_case_sensitive_search ? 'ILIKE' : 'LIKE')
  column_transform = is_case_sensitive_search ? '' : 'LOWER'
  term = "#{(is_full_search ? '%' : '')}#{term.gsub(/([_%\\])/, '\\\\\1')}%"
  if options[:hstore]
    ["#{column_transform}(#{table_name}.#{method} -> '#{options[:hstore][:key]}') LIKE #{column_transform}(?)", term]
  elsif sqlite?
    ["#{column_transform}(#{method}) #{like_clause} #{column_transform}(?)", term]
  else
    ["#{column_transform}(#{table_name}.#{method}) #{like_clause} #{column_transform}(?)", term]
  end
end