Commit de5f9aca authored by Zac Wood's avatar Zac Wood
Browse files

Little bit of clean up

parent 63cb1356
Pipeline #3840 failed with stage
in 2 minutes and 40 seconds
class CoursesController < ApplicationController class CoursesController < ApplicationController
before_action :set_course
def show def show
@course = Course.find_by subject: @course.subject, course_number: @course.course_number, semester: @semester # Load the course with the id passed in the URL.
end @course = Course.find_by_id(params[:id])
# If the user changes the semester while looking at a course,
# we need to display the page for the course with the same subject and course number
# in that semester.
unless @course.semester == @semester
# find the course we should redirect to
@course = Course.find_by(subject: @course.subject, course_number: @course.course_number, semester: @semester)
private # redirect to the url for that course
redirect_to course_url(@course)
end
def set_course @sections = @course.course_sections
@course = Course.find_by_id params[:id]
end end
end end
# HomeController renders the homepage. It currently requires no logic.
class HomeController < ApplicationController class HomeController < ApplicationController
def index; end def index; end
end end
class InstructorsController < ApplicationController class InstructorsController < ApplicationController
before_action :set_instructor, only: [:show]
def index def index
@instructors = Instructor.all @instructors = Instructor.all
end end
def show def show
sections = CourseSection.where instructor: @instructor @instructor = Instructor.find_by_id(params[:id])
sections = sections.select do |s|
s.course.semester == @semester
end
# TODO: move this to a model somewhere # find the courses being taught this semester
@courses = [].to_set sections = CourseSection.where(instructor: @instructor).joins(course: :semester).where("semesters.id = ?", @semester.id)
sections.each do |s| @courses = Course.build_set(sections)
@courses.add s.course
end
# build the list of courses the instructor has taught in the past
@past = [] @past = []
@instructor.course_sections.map(&:course).each do |c| @instructor.course_sections.map(&:course).each do |c|
@past << c unless @past.select { |past| past.full_name == c.full_name }.count.positive? @past << c unless @past.select { |past| past.full_name == c.full_name }.count.positive?
end end
@past.sort_by!(&:full_name) @past.sort_by!(&:full_name)
end end
private
def set_instructor
@instructor = Instructor.find_by_id params[:id]
end
end end
class SearchController < ApplicationController class SearchController < ApplicationController
def index def index
redirect_to home_url unless params[:query].length > 1 redirect_to(home_url) unless params[:query].length > 1
results = SearchHelper::GenericItem.fetchall(String.new(params[:query]), semester: @semester).group_by(&:type) results = SearchHelper::GenericItem.fetchall(String.new(params[:query]), semester: @semester).group_by(&:type)
@instructors = results[:instructor]&.map(&:data) @instructors = results[:instructor]&.map(&:data)
......
class SessionsController < ApplicationController class SessionsController < ApplicationController
def update
update_cookie :crns
update_cookie :section_ids
update_cookie :semester_id
head :ok
end
def cart def cart
section_crn = params[:crn] section_crn = params[:crn]
...@@ -16,25 +8,18 @@ class SessionsController < ApplicationController ...@@ -16,25 +8,18 @@ class SessionsController < ApplicationController
@cart << section_crn @cart << section_crn
end end
puts @cart
cookies.permanent[:cart] = @cart.to_json cookies.permanent[:cart] = @cart.to_json
render json: @cart.to_json render json: @cart.to_json
end end
def add_bulk def add_bulk
crns = params[:crns].split(',') crns = params[:crns].split(',')
crns.each { |crn| crns.each do |crn|
s = CourseSection.latest_by_crn(crn) s = CourseSection.latest_by_crn(crn)
next if s.nil? next if s.nil?
@cart << crn.to_s unless @cart.include?(crn.to_s) @cart << crn.to_s unless @cart.include?(crn.to_s)
} end
cookies.permanent[:cart] = @cart.to_json cookies.permanent[:cart] = @cart.to_json
redirect_to schedule_path redirect_to(schedule_path)
end
private
def update_cookie(sym)
cookies[sym] = params[sym] unless params[sym].nil?
end end
end end
...@@ -52,4 +52,13 @@ class Course < ApplicationRecord ...@@ -52,4 +52,13 @@ class Course < ApplicationRecord
query query
end end
# build_set builds
def self.build_set(sections)
courses = [].to_set
sections.each do |s|
courses.add s.course
end
courses
end
end end
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
<div class="icon"> <div class="icon">
<i class="fa fa-bars"></i> <i class="fa fa-bars"></i>
</div> </div>
<%= @course.course_sections.count %> sections <%= @sections.count %> sections
</div> </div>
</div> </div>
</div> </div>
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
</div> </div>
<div class="col-12 col-lg"> <div class="col-12 col-lg">
<%= render partial: 'shared/section', collection: @course.course_sections %> <%= render partial: 'shared/section', collection: @sections %>
</div> </div>
</div> </div>
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<strong>Previously taught: </strong> <strong>Previously taught: </strong>
<ul> <ul>
<% @past.each do |c| %> <% @past.each do |c| %>
<li><%= link_to c.full_name, course_path(c) %></li> <li><%= link_to(c.full_name, course_path(c)) %></li>
<% end %> <% end %>
</ul> </ul>
<% end %> <% end %>
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
<div class ="col-lg-8 col-12"> <div class ="col-lg-8 col-12">
<h3><%= @semester.to_s %></h3> <h3><%= @semester.to_s %></h3>
<% if @courses.any? %> <% if @courses.any? %>
<%= render partial: 'shared/course', collection: @courses, locals: { expanded: true } %> <%= render(partial: 'shared/course', collection: @courses, locals: { expanded: true }) %>
<% else %> <% else %>
<p><%= @instructor.name %> is not teaching any courses this semester...</p> <p><%= @instructor.name %> is not teaching any courses this semester...</p>
<% end %> <% 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