Events: implement creation of new contests
Some checks failed
CI / scan_ruby (push) Has been cancelled
CI / scan_js (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled

This commit is contained in:
sto
2026-07-28 13:13:46 +02:00
parent 75784fec65
commit d52a058328
5 changed files with 25 additions and 5 deletions

View File

@@ -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