search_helper.rb 2.87 KB
Newer Older
Zac Wood's avatar
Zac Wood committed
1
module SearchHelper
2
3
4
5
6
7
8
9
10
11
12
13
  class GenericQueryData
    attr_reader :semester
    attr_reader :sort_mode
    attr_reader :search_string
    
    def initialize(search_string, sort_mode, semester)
      @semester = semester
      @sort_mode = sort_mode
      @search_string = search_string
    end
  end
  
14
  class GenericItem
15
16
17
    attr_reader :data
    attr_reader :type
    
18
19
20
21
22
    def initialize(type, data)
      @type = type
      @data = data
    end
    
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
30
      build_list(models)
    end
    
31
    def self.fetch_instructors(query_data)
Zach Perkins's avatar
Zach Perkins committed
32
      Instructor.from_name(Instructor.select("instructors.*"), query_data.search_string)
33
34
    end
    
35
36
    def self.fetch_courses(query_data)
      query_string = query_data.search_string
37
38
      query_string.upcase!
      CourseReplacementHelper.replace!(query_string)
39
40
41
      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
42
                         .where("courses.semester_id = ?", query_data.semester)
43
                         .group("courses.id")
44
45
46
47
  
      subj = nil
      query_string.scan(/(?<= |^)([a-zA-Z]{2,4})(?=$| )/).each do |a|
        s = a[0]
48
        if get_count(Course.from_subject(base_query, s)).positive?
49
50
51
52
53
54
55
56
          subj = s
          base_query = Course.from_subject(base_query, subj)
          query_string.remove!(s)
        end
      end
   
      query_string.scan(/(?<= |^)(\d{3})(?=$| )/).each do |a|
        s = a[0]
57
58
        next unless !subj.nil? && get_count(Course.from_course_number(base_query, s)).positive?
        base_query = Course.from_course_number(base_query, s)
59
60
61
        return base_query.all
      end
      
62
63
64
65
66
67
68
69
70
      stripped_query_string = query_string.gsub(/ +/, " ").strip
      
      # There's more to parse
      if stripped_query_string.length.positive?
        base_query = Course.from_title(base_query, stripped_query_string)
                           .order("section_count DESC")
      else
        base_query = base_query.order("courses.course_number ASC")
      end
71
        
72
73
74
75
76
77
78
79
      base_query.all
    end
      
    
    # 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|
80
        list.push(GenericItem.new(model.class.name.underscore.to_sym, model))
81
82
83
84
      end
      
      list
    end
85
86
87
88
89
90
91
92
93
    
    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
    
    def to_s()
      @type
    end
94
  end
Zac Wood's avatar
Zac Wood committed
95
end