Commit 676b28b8 authored by Zac Wood's avatar Zac Wood
Browse files

Merge branch 'remove-redirect' into 'master'

Remove redirect

See merge request !39
parents c746bdc7 26a0735f
Pipeline #3846 failed with stages
in 26 minutes and 41 seconds
...@@ -16,3 +16,6 @@ Metrics/BlockLength: ...@@ -16,3 +16,6 @@ Metrics/BlockLength:
Style/ClassAndModuleChildren: Style/ClassAndModuleChildren:
Enabled: false Enabled: false
Style/GuardClause:
Enabled: false
# Configures the application. # Configures the application.
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
protect_from_forgery with: :null_session protect_from_forgery with: :null_session
before_action :set_semester, :set_cookies, :set_cart
# On each request, set the semester and cart.
before_action :set_semester, :set_cart
# Every page needs to know what semester it should load data from.
# set_semester checks both the semester_id query parameter and the user's cookies
# to look for a semester id and loads whatever it finds into @semester.
#
# By default, load the most recent semester.
def set_semester def set_semester
if params.key?(:semester_id) if params.key?(:semester_id)
cookies[:semester_id] = params[:semester_id]
@semester = Semester.find_by_id params[:semester_id] @semester = Semester.find_by_id params[:semester_id]
cookies[:semester_id] = @semester.id
elsif cookies[:semester_id].nil? elsif cookies[:semester_id].nil?
redirect_to(url_for(params.permit(params.keys).merge(semester_id: Semester.first.id))) @semester = Semester.first
cookies[:semester_id] = @semester.id
else else
redirect_to(url_for(params.permit(params.keys).merge(semester_id: cookies[:semester_id]))) @semester = Semester.find_by_id cookies[:semester_id]
end end
end end
# The user's cart is stored as a JSON-encoded list of CRNs.
# set_cart sets the @cart variable, which is a list of the sections represented by the CRNs.
def set_cart def set_cart
# set the cart cookie to be empty if it doesn't already exist
cookies.permanent[:cart] = "[]" if cookies.permanent[:cart].nil?
# decode the JSON list into an array
@cart = JSON.parse(cookies.permanent[:cart]) @cart = JSON.parse(cookies.permanent[:cart])
# get rid of any invalid CRNs
@cart = @cart.reject { |crn| CourseSection.find_by_crn(crn).nil? } @cart = @cart.reject { |crn| CourseSection.find_by_crn(crn).nil? }
cookies.permanent[:cart] = @cart.to_json
end
def set_cookies # set the cookie to the JSON-encoded list of valid sections
cookies.permanent[:cart] = "[]" if cookies.permanent[:cart].nil? cookies.permanent[:cart] = @cart.to_json
end end
end end
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 %>
......
require 'test_helper' require 'test_helper'
class ApplicationControllerTest < ActionDispatch::IntegrationTest class ApplicationControllerTest < ActionDispatch::IntegrationTest
test "redirects without semester id" do test "gets home page" do
get "/" get("/")
assert_response :redirect assert_response(:success)
assert_match /\?semester_id=#{semesters(:fall2018).id}/, @response.redirect_url
end end
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