search_controller.rb 682 Bytes
Newer Older
Zac Wood's avatar
Zac Wood committed
1
class SearchController < ApplicationController
Zac Wood's avatar
Zac Wood committed
2
3
  include BySemester
  
Zac Wood's avatar
Zac Wood committed
4
  def index
Zac Wood's avatar
Zac Wood committed
5
    redirect_to(home_url) unless params[:query].length > 1
Zac Wood's avatar
Zac Wood committed
6

Zac Wood's avatar
Zac Wood committed
7
8
9
10
11
    if params[:query].casecmp('god').zero?
      bell = Instructor.find_by_name('Jonathan Bell')
      redirect_to(instructor_url(bell))
    end

12
    results = SearchHelper::GenericItem.fetchall(String.new(params[:query]), semester: @semester).group_by(&:type)
Zac Wood's avatar
Zac Wood committed
13
14
    @instructors = results[:instructor]&.map(&:data)
    @courses = results[:course]&.map(&:data)
Zac Wood's avatar
Zac Wood committed
15
16
17
18
19
20

    if @courses&.count == 1
      redirect_to course_url(@courses.first)
    elsif @instructors&.count == 1
      redirect_to instructor_url(@instructors.first)
    end
Zac Wood's avatar
Zac Wood committed
21
22
  end
end