Commit 080037ca authored by Zac Wood's avatar Zac Wood

More redesign

parent f2ec598d
......@@ -18,6 +18,7 @@
#semester-select {
min-width: 100px;
margin-right: 8px;
}
#cart-button {
......
class SearchController < ApplicationController
include BySemester
def index
redirect_to(home_url) unless params[:query].length > 1
......@@ -9,13 +7,49 @@ class SearchController < ApplicationController
redirect_to(instructor_url(bell))
end
results = SearchHelper::GenericItem.fetchall(String.new(params[:query]), semester: @semester).group_by(&:type)
@instructors = results[:instructor]&.map(&:data)
@courses = results[:course]&.map(&:data)
@instructors = nil
@courses = nil
if @courses&.count == 1
/[[: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
redirect_to course_url(@courses.first)
elsif @instructors&.count == 1
elsif @courses&.count == 0 && @instructors&.count == 1
redirect_to instructor_url(@instructors.first)
end
end
......
......@@ -9,44 +9,4 @@ class Course < ApplicationRecord
def full_name
"#{subject} #{course_number}"
end
def self.from_subject(base_query, subject)
base_query.where("courses.subject = ?", subject.upcase)
end
def self.from_course_number(base_query, course_number)
base_query.where("courses.course_number = ?", course_number)
end
def self.from_title(base_query, title)
puts title
# Temporary really disgusting regex that I hate with all my heart
title = (title + " ").upcase.gsub(/(I+) +/, '\1$').gsub(/ +/, "% ").tr('$', ' ')
base_query.where("UPPER(courses.title) LIKE UPPER(?) or UPPER(courses.title) LIKE UPPER(?)", "%#{title.strip}", "%#{title}%")
end
# Given a list of filters, collect a list of matching elements. This makes it
# so you can just pass the arguments straight thru
def self.fetch(filters)
# join with course_sections so that we can get a section count for each course and then sort by that
query = Course.left_outer_joins(:course_sections)
.select("courses.*, COUNT(course_sections.id) AS section_count")
.group("courses.id")
.order("section_count DESC")
filters.each do |filter, value|
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)
when "instructor"
query = Instructor.from_name(query.joins("INNER JOIN instructors ON course_sections.instructor_id = instructors.id"), value)
end
end
query
end
end
......@@ -16,6 +16,8 @@ class CourseSection < ApplicationRecord
serialize :rating_questions, Array
scope :in_semester, ->(semester) { where(semester: semester) }
def teaching_rating
if rating_questions.empty?
nil
......
class Instructor < ApplicationRecord
has_many :course_sections
scope :named, ->(name) {
name.split(' ').reduce(all) do |query, comp|
query.where("upper(instructors.name) LIKE ?", "%#{comp.upcase}%")
end
}
def self.from_name(base_query, name)
base_query.where("upper(instructors.name) LIKE ?", "%#{name.upcase}%")
end
......
<% unless @instructors.nil? %>
<h2>Instructors</h2>
<div class="row">
<% @instructors.each do |instructor| %>
<div class="col"><%= render partial: 'shared/instructor', object: instructor %></div>
<% end %>
<div class="row">
<div class="col-4">
<input class="input" placeholder="Filter..." /> <br />
Hello there<input type="checkbox" />
</div>
<hr />
<% end %>
<% unless @courses.nil? %>
<h2>Courses</h2>
<% @courses.each do |course| %>
<div class="col"><%= render partial: 'shared/course', object: course %></div>
<div class="col-8"><% unless @instructors.nil? %>
<h2>Instructors</h2>
<div class="row">
<% @instructors.each do |instructor| %>
<div class="col"><%= render partial: 'shared/instructor', object: instructor %></div>
<% end %>
</div>
<hr />
<% end %>
<% end %>
<% if @courses.nil? && @instructors.nil? %>
<h1>Sorry, we couldn't find anything matching your search.</h1>
<p>Please try again!</p>
<% end %>
<% unless @courses.nil? %>
<h2>Courses</h2>
<% @courses.each do |course| %>
<div class="col"><%= render partial: 'shared/course', object: course %></div>
<% end %>
<% end %>
<% if @courses.nil? && @instructors.nil? %>
<h1>Sorry, we couldn't find anything matching your search.</h1>
<p>Please try again!</p>
<% end %></div>
</div>
<%= javascript_pack_tag 'search' %>
<%= stylesheet_link_tag 'search' %>
......@@ -15,24 +15,21 @@
<% end %>
</div>
</div>
<h5><em><%= course.title %></em></h5>
</div>
<div class="card-body">
<div class="attr-list justify-content-start">
<div class="attr">
<div class="icon">
<i class="fa fa-book"></i>
</div>
<%= course.credits %> credits
</div>
&nbsp;&nbsp;&nbsp;
<div class="attr">
<div class="icon">
<i class="fa fa-bars"></i>
<div class="d-md-flex justify-content-between">
<h5><em><%= course.title %></em></h5>
<div class="attr-list justify-content-start">
<div class="attr">
<div class="icon">
<i class="fa fa-book"></i>
</div>
<%= course.credits %> credits
</div>
<%= sections.count %> sections
&nbsp;&nbsp;&nbsp;
</div>
</div>
</div>
<div class="card-body">
<p class="description"><%= course.description %></p>
<% unless course.prereqs.nil? || course.prereqs.empty? %>
......@@ -59,7 +56,7 @@
<!-- List of Course Sections -->
<div class="list-group list-group-flush sections" style="display: <%= expanded ? "flex" : "none" %>">
<%= render partial: 'shared/section', collection: sections, locals: { course: course } %>
</div>
</div>
</div>
......@@ -66,9 +66,23 @@
<li class="nav-item">
<a href="/schedule" class="nav-link">Your Schedule (<span id="cart-counter"><%= @cart.length %></span>)</a>
</li>
<li class="nav-item">
<a href="/about" class="nav-link">About Schedules</a>
</li>
</ul>
<form action="/search" class="form-inline">
<select id="semester-select" class="custom-select">
<% Semester.all.each do |semester| %>
<option
value="<%= semester.id %>"
<% if @semester == semester %> selected <% end %>
>
<%= "#{semester.season} #{semester.year}" %>
</option>
<% end %>
</select>
<input
id="searchbar"
value="<%= params[:query] %>"
......@@ -79,6 +93,7 @@
aria-describedby="basic-addon2"
autocomplete="off"
>
<button class="btn btn-secondary my-2 my-sm-0" type="submit">Search</button>
</form>
......
......@@ -25,23 +25,7 @@
<% else %>
<%= link_to section.instructor.name, section.instructor %>
<% unless section.instructor.rating.nil? %>
<div class="star-rating">
<div class="back-stars">
<i class="far fa-star" aria-hidden="true"></i>
<i class="far fa-star" aria-hidden="true"></i>
<i class="far fa-star" aria-hidden="true"></i>
<i class="far fa-star" aria-hidden="true"></i>
<i class="far fa-star" aria-hidden="true"></i>
<div class="front-stars" style="width: <%= (section.instructor.rating[0] / 5 * 100).to_i %>%">
<i class="fa fa-star" aria-hidden="true"></i>
<i class="fa fa-star" aria-hidden="true"></i>
<i class="fa fa-star" aria-hidden="true"></i>
<i class="fa fa-star" aria-hidden="true"></i>
<i class="fa fa-star" aria-hidden="true"></i>
</div>
</div>
</div>
<%= render partial: 'shared/stars', locals: { percent: (section.instructor.rating[0] / 5 * 100).to_i }%>
<% end %>
<% end %>
<br/>
......
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