Class: UcbRails::UserTypeahead

Inherits:
Object
  • Object
show all
Defined in:
app/models/ucb_rails/user_typeahead.rb

Overview

Class for getting results for ldap person search typeahead fields.

By default it will search the UcbRails::User table, but this is configurable:

Examples:

class MyController < ApplicationController

  def search
    uta = UserTypeahead.new
    render :json uta.results('art')
  end

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ UcbRails::UserTypeahead

Constructor

Examples:

UserTypeahead.new
UserTypeahead.new(klass: Faculty, search_column: 'full_name', uid_column: 'net_id', limit: 25)

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :klass (Object) — default: UcbRails::User

    the (ActiveRecord) class to search

  • :search_column (Symbol) — default: :first_last_name

    the column to search

  • :uid_column (Symbol) — default: :uid

    the column holding the uid

  • :first_last_name (Symbol) — default: search_column

    the column holding the first/last names

  • :limit (FixNum) — default: 10

    number of rows to return



32
33
34
35
36
37
38
39
# File 'app/models/ucb_rails/user_typeahead.rb', line 32

def initialize(options={})
  self.klass = options.delete(:klass) || UcbRails::User
  self.search_column = options.delete(:search_column) || :first_last_name
  self.limit = options.delete(:limit) || 10
  self.uid_column = options.delete(:uid_column) || :uid
  self.first_last_name_column = options.delete(:first_last_name_column) || search_column
  validate_options(options)
end

Instance Attribute Details

#first_last_name_columnObject

Returns the value of attribute first_last_name_column.



18
19
20
# File 'app/models/ucb_rails/user_typeahead.rb', line 18

def first_last_name_column
  @first_last_name_column
end

#klassObject

Returns the value of attribute klass.



18
19
20
# File 'app/models/ucb_rails/user_typeahead.rb', line 18

def klass
  @klass
end

#limitObject

Returns the value of attribute limit.



18
19
20
# File 'app/models/ucb_rails/user_typeahead.rb', line 18

def limit
  @limit
end

#search_columnObject

Returns the value of attribute search_column.



18
19
20
# File 'app/models/ucb_rails/user_typeahead.rb', line 18

def search_column
  @search_column
end

#uid_columnObject

Returns the value of attribute uid_column.



18
19
20
# File 'app/models/ucb_rails/user_typeahead.rb', line 18

def uid_column
  @uid_column
end

Instance Method Details

#results(query) ⇒ Array(Hash)

Returns the data matching query.

Examples:

uta = UserTypeahead.new
uta.results('art')        #=> [{uid: '1', first_last_name: 'Art Andrews'}, ...]

Parameters:

  • query (String)

    search term to match name fields on

Returns:

  • (Array(Hash))


47
48
49
50
51
52
# File 'app/models/ucb_rails/user_typeahead.rb', line 47

def results(query)
  klass
    .where(where(query))
    .limit(limit)
    .map { |row| row_to_hash(row) }
end