application_controller.rb 944 Bytes
Newer Older
1
# Configures the application.
Zac Wood's avatar
Zac Wood committed
2
class ApplicationController < ActionController::Base
Zac Wood's avatar
Zac Wood committed
3
  protect_from_forgery with: :null_session
Zac Wood's avatar
Zac Wood committed
4
5
6
  before_action :set_semester, :set_cookies, :set_cart

  def set_semester
7
8
9
10
11
12
13
14
    if params.key?(:semester_id)
      cookies[:semester_id] = params[:semester_id]
      @semester = Semester.find_by_id params[:semester_id]
    elsif cookies[:semester_id].nil?
      redirect_to(url_for(params.permit(params.keys).merge(semester_id: Semester.first.id)))
    else
      redirect_to(url_for(params.permit(params.keys).merge(semester_id: cookies[:semester_id])))
    end
Zac Wood's avatar
Zac Wood committed
15
  end
16
17

  def set_cart
18
    @cart = JSON.parse(cookies[:cart])
19
20
    @cart = @cart.reject { |id| CourseSection.find_by_id(id).nil? }
    cookies[:cart] = @cart.to_json
Zac Wood's avatar
Zac Wood committed
21
  end
22

Zac Wood's avatar
Zac Wood committed
23
24
  def set_cookies
    cookies[:crns] = "" if cookies[:crns].nil?
Zac Wood's avatar
Zac Wood committed
25
    cookies[:section_ids] = "" if cookies[:section_ids].nil?
26
    cookies[:cart] = "[]" if cookies[:cart].nil?
27
  end
Zac Wood's avatar
Zac Wood committed
28
end