Commit ca411789 authored by Zach Perkins's avatar Zach Perkins
Browse files

Added better model filtering

parent e8f25096
......@@ -22,7 +22,13 @@ class Course < ApplicationRecord
end
def self.from_course_number(base_query, course_number)
query = query.where("courses.course_number = ?", value)
query = base_query.where("courses.course_number = ?", course_number)
end
def self.from_title(base_query, title)
# Temporary really disgusting regex that I hate with all my heart
title = (title + " ").gsub(" 1", " I").gsub(" 2", " II").gsub(" 3", " III").upcase.gsub(/(I+) +/, '\1$').gsub(/ +/, "% ").gsub('$', ' ')
base_query.where("UPPER(courses.title) LIKE UPPER(?) or UPPER(courses.title) LIKE UPPER(?)", "%#{title.strip}", "%#{title}%").order('COUNT("course_sections".course_id)').where('"course_sections".course_id = "courses".id')
end
# Given a list of filters, collect a list of matching elements. This makes it
......@@ -34,13 +40,13 @@ class Course < ApplicationRecord
end
filters.each do |filter, value|
if Course.column_names.include? filter
case filter
when "subject"
query = from_subject(query, value)
when "course_number"
query = from_course_number(query, value)
end
case filter
when "subject"
query = from_subject(query, value)
when "course_number"
query = from_course_number(query, value)
when "title"
query = from_title(query, value)
end
end
......
......@@ -25,10 +25,6 @@ class CourseSection < ApplicationRecord
base_query.where("course_sections.course_id = ?")
end
def self.from_name(base_query, name)
base_query.where("name LIKE ?", "%#{name}%")
end
# Select all revelevant course sections given the provided filters
def self.fetch(filters)
query = CourseSection.joins(:course).select("course_sections.*")
......@@ -42,10 +38,12 @@ class CourseSection < ApplicationRecord
query = from_crn(query, value)
when "course_id"
query = from_course_id(query, value)
when "name"
query = from_name(query, value)
when "course_number"
query = Course.from_course_number(query, value)
when "subject"
query = Course.from_subject(query, value)
when "title"
query = Course.from_title(query, value)
end
end
......@@ -69,8 +67,8 @@ class CourseSection < ApplicationRecord
end
}
# If it's not a number, just assume it's the name
filters["name"] = query
# If it's not a number, just assume it's the title
filters["title"] = query
filters
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment