Add public form to add completions
Some checks failed
CI / scan_ruby (push) Successful in 23s
CI / scan_js (push) Successful in 15s
CI / lint (push) Successful in 16s
CI / test (push) Failing after 43s

This commit is contained in:
sto
2025-11-20 10:24:26 +01:00
parent b87800f6bd
commit 3e071f9281
8 changed files with 78 additions and 9 deletions

View File

@@ -24,7 +24,7 @@ class CompletionsController < ApplicationController
authorize @contest
@completion = Completion.new(completion_params)
@completion.contest_id = @contest.id
@completion.contest = @contet
if @completion.save
extend_completions!(@completion.contestant)
if @contestant && !params[:completion].key?(:message_id)

View File

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

View File

@@ -31,6 +31,8 @@ class Contestant < ApplicationRecord
validates :name, presence: true
validates :time_seconds, presence: true
generates_token_for :token
def form_name
if email.present?
"#{name} - #{email}"

View File

@@ -9,14 +9,19 @@
= @message.author
br
= @message.text
.row
.row.mb-2
.col
h4 = t("completions.singular").capitalize
.row.mb-3
.col
.form-floating
= form.select :contestant_id, @contestants.map { |contestant| [contestant.form_name, contestant.id] }, {}, class: "form-select"
= form.label :contestant_id
h4
- if @public
= t("completions.form.validate_name", name: @contestant.name)
- else
= t("completions.singular").capitalize
- if @contestants.present?
.row.mb-3
.col
.form-floating
= form.select :contestant_id, @contestants.map { |contestant| [contestant.form_name, contestant.id] }, {}, class: "form-select"
= form.label :contestant_id
- if @closest_contestant
javascript:
el = document.querySelector('select[name="completion[contestant_id]"]');

View File

@@ -0,0 +1,2 @@
h4
= "Puzzle validated for #{@contestant.name}!"