Commit 63cb1356 authored by Zac Wood's avatar Zac Wood

Remove unnecessary redirect from application_controller

parent 7b640f2a
......@@ -16,3 +16,6 @@ Metrics/BlockLength:
Style/ClassAndModuleChildren:
Enabled: false
Style/GuardClause:
Enabled: false
# Configures the application.
class ApplicationController < ActionController::Base
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
if params.key?(:semester_id)
cookies[:semester_id] = params[:semester_id]
@semester = Semester.find_by_id params[:semester_id]
cookies[:semester_id] = @semester.id
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
redirect_to(url_for(params.permit(params.keys).merge(semester_id: cookies[:semester_id])))
@semester = Semester.find_by_id cookies[:semester_id]
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
# 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])
# get rid of any invalid CRNs
@cart = @cart.reject { |crn| CourseSection.find_by_crn(crn).nil? }
cookies.permanent[:cart] = @cart.to_json
end
def set_cookies
cookies.permanent[:cart] = "[]" if cookies.permanent[:cart].nil?
# set the cookie to the JSON-encoded list of valid sections
cookies.permanent[:cart] = @cart.to_json
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