class AttendanceTrackerController < ApplicationController before_action :refresh_toggl def overview @courses = Course.active.sort_by(&:title) @current_lecture = get_current_lecture end def today @courses = Course.active.sort_by(&:title) @date = Date.today @current_lecture = get_current_lecture @lectures = @courses.flat_map { |course| course.lectures.filter { |a| a.start_time.to_date == @date } }.sort_by { |l| l.start_time } end def course_focus @course = Course.find(params[:id]) @current_lecture = get_current_lecture end private def get_current_lecture current_time_entry = Toggl.current_time_entry return nil if current_time_entry.nil? # Match both 'Lecture 1' and 'Lecture 1: Prep', etc lecture_title = current_time_entry['description'].split(':')[0] lecture = Lecture .joins(:course) .find_by(title: lecture_title, course: { toggl_project: current_time_entry['project_id'] }) return lecture if lecture.present? start_time = Time.new(current_time_entry['start']) lecture = Lecture.joins(:course) .find_by(start_time: (start_time - 10.minutes)..(start_time + 10.minutes), course: { toggl_project: current_time_entry['project_id'] }) return lecture if lecture.present? nil end def refresh_toggl ScrapeTogglJob.perform_later(Course.active.ids) end end