-
Notifications
You must be signed in to change notification settings - Fork 0
/
seasons_controller.rb
122 lines (97 loc) · 3.48 KB
/
seasons_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
class SeasonsController < ApplicationController
before_action :authenticate_user!, :only => [:edit, :update, :destroy]
before_action :authenticate_admin, :only => [:edit, :update, :destroy]
before_action :set_season, :only => [:show, :edit, :update, :destroy]
respond_to :html, :json
# GET /seasons or /seasons.json
def index
@seasons = Season.all
respond_with @seasons do |format|
format.json { render :layout => false }
end
end
# GET /seasons/1 or /seasons/1.json
def show
@racer_entries = @season.racer_result_entries
@racer_entries = Kaminari.paginate_array(@racer_entries).page(params[:page]).per(16)
@count = ((@racer_entries.current_page - 1) * @racer_entries.limit_value) + 1
@team_entries = @season.team_result_entries
respond_with @season do |format|
format.json { render :layout => false }
end
end
# GET /seasons/new
def new
@season = Season.new
end
# GET /seasons/1/edit
def edit; end
# POST /seasons or /seasons.json
def create
@season = Season.new(season_params)
respond_to do |format|
if @season.save!
6.times do |n|
@season.rankings << Ranking.new({ :number => n + 1, :season => @season })
end
format.html { redirect_to season_url(@season), :notice => 'Season was successfully created.' }
format.json { render :show, :status => :created, :location => @season, :layout => false }
Rails.cache.delete('current_season')
else
format.html { render :new, :status => :unprocessable_entity }
format.json { render :json => @season.errors, :status => :unprocessable_entity, :layout => false }
end
end
end
# PATCH/PUT /seasons/1 or /seasons/1.json
def update
respond_to do |format|
if @season.update(season_params)
format.html { redirect_to season_url(@season), :notice => 'Season was successfully updated.' }
format.json { render :show, :status => :ok, :location => @season, :layout => false }
else
format.html { render :edit, :status => :unprocessable_entity }
format.json { render :json => @season.errors, :status => :unprocessable_entity, :layout => false }
end
end
end
# DELETE /seasons/1 or /seasons/1.json
def destroy
require 'rva_calculate_results_service'
require 'stats_service'
require 'team_points_service'
@season.rankings.each do |r|
r.sessions.each do |s|
rva_results = RvaCalculateResultsService.new(s).call
if s.teams?
TeamPointsService.new(s, rva_results).remove_team_points
else
StatsService.new(s, rva_results).remove_stats
end
Rails.cache.delete("Session:#{s.id}")
s.destroy!
end
r.destroy!
end
@season.tracks.each(&:destroy)
@season.cars.each(&:destroy)
respond_to do |format|
if @season.destroy!
format.html { redirect_to seasons_url, :notice => 'Season was successfully deleted.' }
format.json { head :no_content }
else
format.html { render :edit, :status => :unprocessable_entity }
format.json { render :json => @season.errors, :status => :unprocessable_entity, :layout => false }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_season
@season = Season.find(params[:id])
end
# Only allow a list of trusted parameters through.
def season_params
params.require(:season).permit(:name, :start_date, :end_date)
end
end