application_controller.rb 683 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
7
8
9
10
11
12
  before_action :set_semester, :set_cookies, :set_cart

  def set_semester
    @semester = if cookies.key?(:semester_id)
                  Semester.find_by(id: cookies[:semester_id])
                else
                  Semester.find_by(season: 'Spring', year: '2019')
                end
  end
13
14

  def set_cart
Zac Wood's avatar
Zac Wood committed
15
16
17
    @cart = cookies[:crns].split(',').map do |crn|
      s = CourseSection.find_by_crn(crn)
      s if s.course.semester == @semester
18
    end
Zac Wood's avatar
Zac Wood committed
19

Zac Wood's avatar
Zac Wood committed
20
    @cart.compact!
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?
25
  end
Zac Wood's avatar
Zac Wood committed
26
end