application_controller.rb 749 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
    sections = cookies[:section_ids].split(',').map do |id|
      CourseSection.find_by_id(id)
17
    end
Zac Wood's avatar
Zac Wood committed
18

Zac Wood's avatar
Zac Wood committed
19
20
21
    @cart = sections.group_by do |s|
      s.course.id
    end
Zac Wood's avatar
Zac Wood committed
22
  end
23

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