Module: OhioStatePerson::ClassMethods

Defined in:
lib/ohio_state_person.rb

Instance Method Summary collapse

Instance Method Details

#search(q, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ohio_state_person.rb', line 24

def search(q, options={})
  q = q.to_s
  h = ActiveSupport::OrderedHash.new
  if options[:fuzzy]
    h[/\A\s*\d+\s*\z/]      = lambda { where('emplid LIKE ?', "#{q.strip}%") }
    h[/\A\s*\D+\.\d*\s*\z/] = lambda { where('name_n LIKE ?', "#{q.strip}%") } if column_names.include? 'name_n'
  else
    h[/\A\s*\d+\s*\z/]      = lambda { where(:emplid => q.strip) }
    h[/\A\s*\D+\.\d*\s*\z/] = lambda { where(:name_n => q.strip) } if column_names.include? 'name_n'
  end
  h[/(\S+),\s*(\S*)/]     = lambda { where('last_name LIKE ? AND first_name LIKE ?', $1, "#{$2}%") }
  h[/(\S+)\s+(\S*)/]      = lambda { where('first_name LIKE ? AND last_name LIKE ?', $1, "#{$2}%") }
  h[/\S/]                 = lambda { where('last_name LIKE ?', "#{q}%") }
  h[//]                   = lambda { where('1=2') }

  h.each do |regex, where_clause|
    return where_clause.call if q =~ regex
  end

end