search_helper.rb 3.17 KB
Newer Older
Zac Wood's avatar
Zac Wood committed
1
module SearchHelper
2
3
4
5
  class GenericQueryData
    attr_reader :semester
    attr_reader :sort_mode
    attr_reader :search_string
6

7
8
9
10
11
12
    def initialize(search_string, sort_mode, semester)
      @semester = semester
      @sort_mode = sort_mode
      @search_string = search_string
    end
  end
13

14
  class GenericItem
15
16
    attr_reader :data
    attr_reader :type
17

18
19
20
21
    def initialize(type, data)
      @type = type
      @data = data
    end
22

Zach Perkins's avatar
Zach Perkins committed
23
    def self.fetchall(search_string, sort_mode: :auto, semester: :fall2018)
24
      query_data = GenericQueryData.new(search_string, sort_mode, semester)
25
      models = []
26
27
      models += fetch_instructors query_data
      models += fetch_courses query_data
28
29
      build_list(models)
    end
30

31
    def self.fetch_instructors(query_data)
32
33
34
35
      Instructor.from_name(Instructor.select("instructors.*, COUNT(courses.id) AS section_count").from("course_sections"), query_data.search_string)
                .joins("LEFT OUTER JOIN instructors ON instructors.id = course_sections.instructor_id")
                .joins("LEFT OUTER JOIN courses ON courses.id = course_sections.course_id AND courses.semester_id = #{query_data.semester.id}")
                .group("instructors.id").all
36
    end
37

38
39
    def self.fetch_courses(query_data)
      query_string = query_data.search_string
40
41
      query_string.upcase!
      CourseReplacementHelper.replace!(query_string)
42
43
44
      base_query = Course.select("courses.*, count(course_sections.id) AS section_count")
                         .left_outer_joins(:course_sections)
                         .having("section_count > 0")
Zach Perkins's avatar
Zach Perkins committed
45
                         .where("courses.semester_id = ?", query_data.semester)
46
                         .group("courses.id")
47

48
49
50
      subj = nil
      query_string.scan(/(?<= |^)([a-zA-Z]{2,4})(?=$| )/).each do |a|
        s = a[0]
51
52
53
54
        next unless get_count(Course.from_subject(base_query, s)).positive?
        subj = s
        base_query = Course.from_subject(base_query, subj)
        query_string.remove!(s)
55
      end
56

57
58
      query_string.scan(/(?<= |^)(\d{3})(?=$| )/).each do |a|
        s = a[0]
59
60
        next unless !subj.nil? && get_count(Course.from_course_number(base_query, s)).positive?
        base_query = Course.from_course_number(base_query, s)
61
62
        return base_query.all
      end
63

64
      stripped_query_string = query_string.gsub(/ +/, " ").strip
65

66
      # There's more to parse
67
68
      base_query = if stripped_query_string.length.positive?
                     Course.from_title(base_query, stripped_query_string)
69
                           .order("section_count DESC")
70
71
72
73
                   else
                     base_query.order("courses.course_number ASC")
                   end

74
75
      base_query.all
    end
76

77
78
79
80
    # Given a set of models, create a list of GenericItems for each model's data
    def self.build_list(models)
      list = []
      models.each do |model|
81
        list.push(GenericItem.new(model.class.name.underscore.to_sym, model))
82
      end
83

84
85
      list
    end
86

87
88
89
90
    def self.get_count(base_query)
      # I think I finally hit a limit of active record
      ActiveRecord::Base.connection.execute("SELECT COUNT(*) AS count FROM (#{base_query.to_sql})")[0]["count"]
    end
91
92

    def to_s
93
94
      @type
    end
95
  end
Zac Wood's avatar
Zac Wood committed
96
end