module Rails3JQueryAutocomplete::Orm::ActiveRecord

Public Instance Methods

active_record_get_autocomplete_items(parameters) click to toggle source
# File lib/rails3-jquery-autocomplete/orm/active_record.rb, line 11
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) click to toggle source
# File lib/rails3-jquery-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}." : ""
  order || "LOWER(#{table_prefix}#{method}) ASC"
end
get_autocomplete_select_clause(model, method, options) click to toggle source
# File lib/rails3-jquery-autocomplete/orm/active_record.rb, line 34
def get_autocomplete_select_clause(model, method, options)
  table_name = model.table_name
  (["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + (options[:extra_data].blank? ? [] : options[:extra_data]))
end
get_autocomplete_where_clause(model, term, method, options) click to toggle source
# File lib/rails3-jquery-autocomplete/orm/active_record.rb, line 39
def get_autocomplete_where_clause(model, term, method, options)
  table_name = model.table_name
  is_full_search = options[:full]
  like_clause = (postgres?(model) ? 'ILIKE' : 'LIKE')
  if options[:hstore]
    ["LOWER(#{table_name}.#{method} -> '#{options[:hstore][:key]}') LIKE ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"]
  else
    ["LOWER(#{table_name}.#{method}) #{like_clause} ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"]
  end
end
postgres?(model) click to toggle source
# File lib/rails3-jquery-autocomplete/orm/active_record.rb, line 50
def postgres?(model)
  # Figure out if this particular model uses the PostgreSQL adapter
  model.connection.class.to_s.match(/PostgreSQLAdapter/)
end