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

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

Zac Wood's avatar
Zac Wood committed
10
11
    @instructors = nil
    @courses = nil
Zac Wood's avatar
Zac Wood committed
12

Zac Wood's avatar
Zac Wood committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    /[[:alpha:]]{2,4} \d{3}/.match(params[:query]) do |m|
      subj, num = m[0].split(' ')
      course = Course.find_by(subject: subj.upcase, course_number: num)
      redirect_to(course_url course) unless course.nil?
    end

    /[[:alpha:]]{2,4}/i.match(params[:query]) do |m|
      @courses = Course.where(subject: m[0].upcase)
        .joins(:course_sections)
        .merge(CourseSection.in_semester(@semester))
        .uniq

      if @courses.empty?
        @courses = Course.where("(courses.title LIKE ?)", "%#{params[:query]}%")
          .joins(:course_sections)
          .merge(CourseSection.in_semester(@semester))
          .uniq

        other = Course.where("(courses.description LIKE ?)", "%#{params[:query]}%")
          .joins(:course_sections)
          .merge(CourseSection.in_semester(@semester))
          .uniq

        @courses = [*@courses, *other].uniq

        @instructors = Instructor.named(params[:query])
      end
    end

    /[0-9]{5}/.match(params[:query]) do |m|
      redirect_to(course_url CourseSection.latest_by_crn(m[0]).course)
    end

    # results = SearchHelper::GenericItem.fetchall(String.new(params[:query]), semester: @semester).group_by(&:type)
    # @instructors = results[:instructor]&.map(&:data)
    # @courses = results[:course]&.map(&:data)

    if @courses&.count == 1 && @instructors&.count == 0
Zac Wood's avatar
Zac Wood committed
51
      redirect_to course_url(@courses.first)
Zac Wood's avatar
Zac Wood committed
52
    elsif @courses&.count == 0 && @instructors&.count == 1
Zac Wood's avatar
Zac Wood committed
53
54
      redirect_to instructor_url(@instructors.first)
    end
Zac Wood's avatar
Zac Wood committed
55
56
  end
end