Events: implement creation of new contests
This commit is contained in:
@@ -10,7 +10,7 @@ class ContestsController < ApplicationController
|
||||
def index
|
||||
authorize :contest
|
||||
|
||||
@contests = current_user.contests.includes([ :contestants, puzzles: [ :image_attachment ] ]).order(created_at: :desc)
|
||||
@contests = current_user.contests.where(event: nil).includes([ :contestants, puzzles: [ :image_attachment ] ]).order(created_at: :desc)
|
||||
@title = I18n.t("contests.index.title", username: current_user.username)
|
||||
end
|
||||
|
||||
@@ -125,6 +125,9 @@ class ContestsController < ApplicationController
|
||||
authorize :contest
|
||||
|
||||
@contest = Contest.new
|
||||
if params.key?(:event)
|
||||
@contest.event_id = params[:event]
|
||||
end
|
||||
@title = I18n.t("contests.new.title")
|
||||
@nonav = true
|
||||
end
|
||||
@@ -136,6 +139,18 @@ class ContestsController < ApplicationController
|
||||
@contest.lang = @current_user.lang
|
||||
@contest.ranking_mode = "actual"
|
||||
@contest.user_id = current_user.id
|
||||
|
||||
# Check if the provided event_id exists and belongs to the current user. If not, remove
|
||||
# the link to the event.
|
||||
begin
|
||||
event = Event.find(@contest.event_id)
|
||||
if current_user != event.user
|
||||
@contest.event_id = nil
|
||||
end
|
||||
rescue
|
||||
@contest.event_id = nil
|
||||
end
|
||||
|
||||
if @contest.save
|
||||
redirect_to "/contests/#{@contest.id}/settings/general", notice: t("contests.new.notice")
|
||||
else
|
||||
@@ -274,7 +289,7 @@ class ContestsController < ApplicationController
|
||||
end
|
||||
|
||||
def new_contest_params
|
||||
params.expect(contest: [ :name, :duration ])
|
||||
params.expect(contest: [ :name, :duration, :event_id ])
|
||||
end
|
||||
|
||||
def settings_general_params
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
= form_with model: @contest do |form|
|
||||
= form.hidden_field :event_id
|
||||
.row.mb-3
|
||||
.col
|
||||
.form-floating
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
.row
|
||||
.col
|
||||
h4.mb-3
|
||||
= t("contests.index.manage_contests")
|
||||
= t("events.index.manage_contests")
|
||||
.float-end
|
||||
a.btn.btn-primary.mb-4 href=new_contest_path
|
||||
= t("contests.index.new_contest")
|
||||
a.btn.btn-primary.mb-4 href=new_contest_path(event: @event.id)
|
||||
= t("events.index.new_contest")
|
||||
|
||||
.row.row-cols-1.row-cols-md-3.g-4
|
||||
- @contests.each do |contest|
|
||||
|
||||
Reference in New Issue
Block a user