-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracks_controller.rb
132 lines (110 loc) · 4.23 KB
/
tracks_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
123
124
125
126
127
128
129
130
131
132
class TracksController < ApplicationController
before_action :authenticate_user!, :only => [:edit, :update, :destroy]
before_action :authenticate_admin, :only => [:edit, :update, :destroy]
before_action :set_track, :only => [:show, :edit, :update, :destroy]
respond_to :html, :json
# GET /tracks or /tracks.json
def index
if current_season.nil?
@tracks = Track.all
else
@tracks = current_season.tracks
end
@tracks = Kaminari.paginate_array(@tracks).page(params[:page]).per(12)
respond_with @tracks do |format|
format.json { render :layout => false }
end
end
# GET /tracks/1 or /tracks/1.json
def show
respond_with @track do |format|
format.json { render :layout => false }
end
end
# GET /tracks/new
def new
@track = Track.new
end
# GET /tracks/1/edit
def edit; end
# POST /tracks or /tracks.json
def create
@track = Track.new(track_params)
respond_to do |format|
if @track.save
format.html { redirect_to track_url(@track), :notice => t("rva.tracks.controller.create") }
format.json { render :show, :status => :created, :location => @track, :layout => false }
else
format.html { render :new, :status => :unprocessable_entity }
format.json { render :json => @track.errors, :status => :unprocessable_entity, :layout => false }
end
end
end
# PATCH/PUT /tracks/1 or /tracks/1.json
def update
respond_to do |format|
if @track.update(track_params)
format.html { redirect_to track_url(@track), :notice => t("rva.tracks.controller.update") }
format.json { render :show, :status => :ok, :location => @track, :layout => false }
else
format.html { render :edit, :status => :unprocessable_entity }
format.json { render :json => @track.errors, :status => :unprocessable_entity, :layout => false }
end
end
end
def import
require 'csv_import_tracks_service'
file = params[:file]
if file.nil?
respond_to do |format|
format.html { redirect_to new_track_path, :notice => t("misc.controller.import.select") }
format.json { render :json => t("misc.controller.import.select"), :status => :bad_request, :layout => false }
end and return
end
unless SYS::CSV_TYPES.include?(file.content_type)
respond_to do |format|
format.html { redirect_to new_track_path, :notice => t("misc.controller.import.upload") }
format.json { render :json => t("misc.controller.import.upload"), :status => :bad_request, :layout => false }
end and return
end
if params[:season].nil? || params[:season].empty?
respond_to do |format|
format.html { redirect_to new_track_path, :notice => t("misc.controller.import.season") }
format.json { render :json => t("misc.controller.import.season"), :status => :bad_request, :layout => false }
end and return
end
@tracks = CsvImportTracksService.new(file, params[:season]).call
respond_to do |format|
if @tracks.empty?
format.html { redirect_to new_track_path, :notice => t("rva.tracks.controller.import.exists") }
format.json { render :show, :status => :ok, :layout => false }
end and return
@tracks.each do |track|
if track.save!
format.html { redirect_to new_track_path, :notice => t("rva.tracks.controller.import.success") }
format.json { render :show, :status => :created, :location => track, :layout => false }
else
format.html { render :new, :status => :unprocessable_entity }
format.json { render :json => track.errors, :status => :unprocessable_entity, :layout => false }
end
end
end
end
# DELETE /tracks/1 or /tracks/1.json
def destroy
@track.destroy
respond_to do |format|
format.html { redirect_to tracks_url, :notice => t("rva.tracks.controller.destroy") }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_track
@track = Track.find(params[:id])
end
# Only allow a list of trusted parameters through.
def track_params
params.require(:track).permit(:name, :short_name, :difficulty, :length, :folder_name, :season)
end
end