-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Weekly Schedules & Tracklists (#86)
- Loading branch information
Showing
28 changed files
with
672 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#weekly-schedules { | ||
#weekly-schedules-table { | ||
tr { | ||
color: #beb8bc; | ||
} | ||
} | ||
} | ||
|
||
#weekly-schedule { | ||
#track-list-table { | ||
th { | ||
font-family: NissanOpti, serif; | ||
font-size: 20px; | ||
background-color: #003366; | ||
} | ||
|
||
tr { | ||
color: #fff; | ||
font-weight: bold; | ||
} | ||
|
||
.red { | ||
background-color: #800000; | ||
} | ||
|
||
.black { | ||
background-color: #000001; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
class WeeklySchedulesController < ApplicationController | ||
before_action :authenticate_user!, :except => [:index, :show] | ||
before_action :authenticate_organizer, :except => [:index, :show] | ||
before_action :set_weekly_schedule, :only => [:show, :edit, :update, :destroy] | ||
|
||
respond_to :html, :json | ||
|
||
# GET /weekly_schedules or /weekly_schedules.json | ||
def index | ||
@weekly_schedules = WeeklySchedule.all | ||
@count = 1 | ||
|
||
respond_with @weekly_schedules do |format| | ||
format.json { render :layout => false } | ||
end | ||
end | ||
|
||
# GET /weekly_schedules/1 or /weekly_schedules/1.json | ||
def show | ||
@count = 0 | ||
@table_count = 0 | ||
|
||
respond_with @weekly_schedule do |format| | ||
format.json { render :layout => false } | ||
end | ||
end | ||
|
||
# GET /weekly_schedules/new | ||
def new | ||
@weekly_schedule = WeeklySchedule.new | ||
end | ||
|
||
# GET /weekly_schedules/1/edit | ||
def edit | ||
end | ||
|
||
# POST /weekly_schedules or /weekly_schedules.json | ||
def create | ||
require 'rva_generate_weekly_schedule_service' | ||
|
||
@weekly_schedule = RvaGenerateWeeklyScheduleService.new(weekly_schedule_params).generate | ||
|
||
respond_to do |format| | ||
if @weekly_schedule.save | ||
format.html { redirect_to weekly_schedule_url(@weekly_schedule), :notice => "Weekly schedule was successfully created." } | ||
format.json { render :show, :status => :created, :location => @weekly_schedule } | ||
else | ||
format.html { render :new, :status => :unprocessable_entity } | ||
format.json { render :json => @weekly_schedule.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PATCH/PUT /weekly_schedules/1 or /weekly_schedules/1.json | ||
def update | ||
respond_to do |format| | ||
if @weekly_schedule.update(weekly_schedule_params) | ||
format.html { redirect_to weekly_schedule_url(@weekly_schedule), :notice => "Weekly schedule was successfully updated." } | ||
format.json { render :show, :status => :ok, :location => @weekly_schedule } | ||
else | ||
format.html { render :edit, :status => :unprocessable_entity } | ||
format.json { render :json => @weekly_schedule.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /weekly_schedules/1 or /weekly_schedules/1.json | ||
def destroy | ||
@weekly_schedule.destroy! | ||
|
||
respond_to do |format| | ||
format.html { redirect_to weekly_schedules_url, :notice => "Weekly schedule was successfully destroyed." } | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
private | ||
# Use callbacks to share common setup or constraints between actions. | ||
def set_weekly_schedule | ||
@weekly_schedule = WeeklySchedule.find(params[:id]) | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def weekly_schedule_params | ||
params.require(:weekly_schedule).permit(:season, :start_date) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module WeeklySchedulesHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import html2canvas from "html2canvas"; | ||
|
||
$(document).on('turbo:load', function () { | ||
for (let i = 0; i < 7; i++) { | ||
document.getElementById(`dl-png-${i}`).onclick = function () { | ||
const screenshotTarget = document.getElementById(`tracklist-${i}`); | ||
|
||
html2canvas(screenshotTarget).then((canvas) => { | ||
const base64image = canvas.toDataURL("image/png"); | ||
var anchor = document.createElement('a'); | ||
anchor.setAttribute("href", base64image); | ||
anchor.setAttribute("download", `tracklist-${i + 1}.png`); | ||
anchor.click(); | ||
anchor.remove(); | ||
}) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class TrackList | ||
include Mongoid::Document | ||
|
||
embedded_in :weekly_schedule | ||
|
||
embeds_many :track_list_entries | ||
|
||
field :category, :type => Integer | ||
field :track_count, :type => Integer | ||
|
||
validates_presence_of :category | ||
validates_presence_of :track_count | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class TrackListEntry | ||
include Mongoid::Document | ||
|
||
embedded_in :track_list | ||
|
||
field :track_name, :type => String | ||
field :stock, :type => Boolean | ||
field :lap_count, :type => Integer | ||
|
||
validates_presence_of :track_name | ||
validates_presence_of :stock | ||
validates_presence_of :lap_count | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class WeeklySchedule | ||
include Mongoid::Document | ||
include Mongoid::Timestamps | ||
include Mongoid::MultiParameterAttributes | ||
|
||
store_in :database => 'rv_weekly_schedules' | ||
|
||
embeds_many :track_lists | ||
|
||
accepts_nested_attributes_for :track_lists | ||
|
||
belongs_to :season | ||
|
||
field :start_date, :type => Date | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
class RvaGenerateWeeklyScheduleService | ||
include ApplicationHelper | ||
include SeasonsHelper | ||
|
||
def initialize(weekly_schedule_params) | ||
@season = Season.find(weekly_schedule_params[:season]) | ||
@start_date = weekly_schedule_params[:start_date] | ||
end | ||
|
||
def generate | ||
category_numbers = [ | ||
SYS::CATEGORY::ROOKIE, | ||
SYS::CATEGORY::AMATEUR, | ||
SYS::CATEGORY::ADVANCED, | ||
SYS::CATEGORY::SEMI_PRO, | ||
SYS::CATEGORY::PRO, | ||
SYS::CATEGORY::SUPER_PRO, | ||
SYS::CATEGORY::RANDOM | ||
].shuffle | ||
|
||
season_tracks = @season.tracks.clone.shuffle | ||
reverse_tracks = [] | ||
reverse = false | ||
|
||
track_lists = {} | ||
num_track_lists = 0 | ||
category_numbers.each do |n| | ||
season_tracks = @season.tracks.clone if season_tracks.length < 20 | ||
|
||
# pick 20 random tracks every 2 TrackLists | ||
track_list_entries = {} | ||
num_track_list_entries = 0 | ||
|
||
if reverse | ||
reverse_tracks.each do |t| | ||
track_entry_hash = { | ||
:track_name => "#{t.name} R", | ||
:stock => t.stock, | ||
:lap_count => t.lap_count(n) | ||
} | ||
|
||
track_list_entries = track_list_entries.merge(num_track_list_entries.to_s => track_entry_hash) | ||
|
||
num_track_list_entries += 1 | ||
end | ||
|
||
reverse_tracks = [] | ||
else | ||
20.times do | ||
track = season_tracks.pop.clone | ||
reverse_tracks << track | ||
|
||
track_entry_hash = { | ||
:track_name => track.name, | ||
:stock => track.stock, | ||
:lap_count => track.lap_count(n) | ||
} | ||
|
||
track_list_entries = track_list_entries.merge(num_track_list_entries.to_s => track_entry_hash) | ||
|
||
num_track_list_entries += 1 | ||
end | ||
end | ||
|
||
track_list_hash = { | ||
:category => n, | ||
:track_count => 20, | ||
:track_list_entries => track_list_entries | ||
} | ||
|
||
track_lists = track_lists.merge(num_track_lists.to_s => track_list_hash) | ||
|
||
num_track_lists += 1 | ||
reverse = !reverse | ||
end | ||
|
||
weekly_schedule_hash = { | ||
:season => @season, | ||
:start_date => @start_date, | ||
:track_lists => track_lists | ||
} | ||
|
||
WeeklySchedule.new(weekly_schedule_hash) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
= form_with(model: weekly_schedule) do |form| | ||
- if weekly_schedule.errors.any? | ||
%div{style: "color: red"} | ||
%h2 | ||
= pluralize(weekly_schedule.errors.count, "error") | ||
prohibited this weekly_schedule from being saved: | ||
%ul | ||
- weekly_schedule.errors.each do |error| | ||
%li= error.full_message | ||
%div | ||
= form.submit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
%div{id: "#{dom_id weekly_schedule}"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
json.extract! weekly_schedule, :id, :start_date, :track_lists, :created_at, :updated_at | ||
json.url weekly_schedule_url(weekly_schedule, format: :json) |
Oops, something went wrong.