Add public form to add completions
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
class ContestantsController < ApplicationController
|
||||
include CompletionsConcern
|
||||
include ContestantsConcern
|
||||
|
||||
before_action :set_contest
|
||||
before_action :set_contest, only: %i[ index edit new create update destroy import upload_csv convert_csv finalize_import export ]
|
||||
before_action :set_contestant, only: %i[ destroy edit update]
|
||||
before_action :set_completions, only: %i[edit update ]
|
||||
skip_before_action :require_authentication, only: %i[ get_public_completion post_public_completion public_completion_updated ]
|
||||
|
||||
def index
|
||||
authorize @contest
|
||||
@@ -118,6 +120,53 @@ class ContestantsController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def get_public_completion
|
||||
skip_authorization
|
||||
|
||||
@contestant = Contestant.find_by_token_for(:token, params[:token])
|
||||
if !@contestant
|
||||
not_found and return
|
||||
end
|
||||
@contest = @contestant.contest
|
||||
@puzzles = @contest.puzzles
|
||||
@completion = Completion.new
|
||||
@completion.completed = true
|
||||
@public = true
|
||||
|
||||
render "completions/_form", locals: { completion: @completion, submit_text: t("helpers.buttons.create"), method: :post, url: "/public/p/#{params[:token]}" }
|
||||
end
|
||||
|
||||
def post_public_completion
|
||||
skip_authorization
|
||||
|
||||
@contestant = Contestant.find_by_token_for(:token, params[:token])
|
||||
if !@contestant
|
||||
not_found and return
|
||||
end
|
||||
@contest = @contestant.contest
|
||||
|
||||
@completion = Completion.new(completion_params)
|
||||
@completion.contest = @contest
|
||||
@completion.contestant = @contestant
|
||||
if @completion.save
|
||||
extend_completions!(@completion.contestant)
|
||||
redirect_to "/public/p/#{params[:token]}/updated"
|
||||
else
|
||||
@puzzles = @contest.puzzles
|
||||
@public = true
|
||||
render "completions/_form", locals: { completion: @completion, submit_text: t("helpers.buttons.create"), method: :post, url: "/public/p/#{params[:token]}" }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def public_completion_updated
|
||||
skip_authorization
|
||||
|
||||
@contestant = Contestant.find_by_token_for(:token, params[:token])
|
||||
if !@contestant
|
||||
not_found and return
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_contest
|
||||
@@ -156,4 +205,8 @@ class ContestantsController < ApplicationController
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def completion_params
|
||||
params.expect(completion: [ :display_time_from_start, :completed, :missing_pieces, :remaining_pieces, :puzzle_id ])
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user