by_semester.rb 810 Bytes
Newer Older
1
# BySemester contains logic for setting the current request's Semester.
2 3 4 5 6 7 8 9
module BySemester
  extend ActiveSupport::Concern

  included do
    before_action :set_semester
  end

  # This page needs to know what semester it should load data from.
10
  # set_semester checks both the semester_id query parameter and the current session
11 12 13 14 15
  # 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)
16 17 18
      @semester = Semester.find_by_id(params[:semester_id])
      session[:semester_id] = @semester.id
    elsif session[:semester_id].nil?
19
      @semester = Semester.first
20
      session[:semester_id] = @semester.id
21
    else
22
      @semester = Semester.find_by_id(session[:semester_id])
23 24 25
    end
  end
end