Add public form to add completions
This commit is contained in:
@@ -24,7 +24,7 @@ class CompletionsController < ApplicationController
|
|||||||
authorize @contest
|
authorize @contest
|
||||||
|
|
||||||
@completion = Completion.new(completion_params)
|
@completion = Completion.new(completion_params)
|
||||||
@completion.contest_id = @contest.id
|
@completion.contest = @contet
|
||||||
if @completion.save
|
if @completion.save
|
||||||
extend_completions!(@completion.contestant)
|
extend_completions!(@completion.contestant)
|
||||||
if @contestant && !params[:completion].key?(:message_id)
|
if @contestant && !params[:completion].key?(:message_id)
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
class ContestantsController < ApplicationController
|
class ContestantsController < ApplicationController
|
||||||
|
include CompletionsConcern
|
||||||
include ContestantsConcern
|
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_contestant, only: %i[ destroy edit update]
|
||||||
before_action :set_completions, only: %i[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
|
def index
|
||||||
authorize @contest
|
authorize @contest
|
||||||
@@ -118,6 +120,53 @@ class ContestantsController < ApplicationController
|
|||||||
end
|
end
|
||||||
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
|
private
|
||||||
|
|
||||||
def set_contest
|
def set_contest
|
||||||
@@ -156,4 +205,8 @@ class ContestantsController < ApplicationController
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def completion_params
|
||||||
|
params.expect(completion: [ :display_time_from_start, :completed, :missing_pieces, :remaining_pieces, :puzzle_id ])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ class Contestant < ApplicationRecord
|
|||||||
validates :name, presence: true
|
validates :name, presence: true
|
||||||
validates :time_seconds, presence: true
|
validates :time_seconds, presence: true
|
||||||
|
|
||||||
|
generates_token_for :token
|
||||||
|
|
||||||
def form_name
|
def form_name
|
||||||
if email.present?
|
if email.present?
|
||||||
"#{name} - #{email}"
|
"#{name} - #{email}"
|
||||||
|
|||||||
@@ -9,9 +9,14 @@
|
|||||||
= @message.author
|
= @message.author
|
||||||
br
|
br
|
||||||
= @message.text
|
= @message.text
|
||||||
.row
|
.row.mb-2
|
||||||
.col
|
.col
|
||||||
h4 = t("completions.singular").capitalize
|
h4
|
||||||
|
- if @public
|
||||||
|
= t("completions.form.validate_name", name: @contestant.name)
|
||||||
|
- else
|
||||||
|
= t("completions.singular").capitalize
|
||||||
|
- if @contestants.present?
|
||||||
.row.mb-3
|
.row.mb-3
|
||||||
.col
|
.col
|
||||||
.form-floating
|
.form-floating
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
h4
|
||||||
|
= "Puzzle validated for #{@contestant.name}!"
|
||||||
@@ -170,6 +170,8 @@ en:
|
|||||||
edit:
|
edit:
|
||||||
notice: Completion updated
|
notice: Completion updated
|
||||||
title: Edit completion
|
title: Edit completion
|
||||||
|
form:
|
||||||
|
validate_name: "Validate a puzzle for %{name}"
|
||||||
new:
|
new:
|
||||||
notice: Completion added
|
notice: Completion added
|
||||||
title: New completion
|
title: New completion
|
||||||
|
|||||||
@@ -141,6 +141,8 @@ fr:
|
|||||||
edit:
|
edit:
|
||||||
notice: Complétion modifiée
|
notice: Complétion modifiée
|
||||||
title: Modifier la complétion
|
title: Modifier la complétion
|
||||||
|
form:
|
||||||
|
validate_name: "Valider un puzzle pour %{name}"
|
||||||
new:
|
new:
|
||||||
notice: Complétion ajoutée
|
notice: Complétion ajoutée
|
||||||
title: Ajout d'une complétion
|
title: Ajout d'une complétion
|
||||||
|
|||||||
@@ -44,4 +44,7 @@ Rails.application.routes.draw do
|
|||||||
get "public/:id/offline/:token", to: "contests#offline_edit"
|
get "public/:id/offline/:token", to: "contests#offline_edit"
|
||||||
patch "public/:id/offline/:token", to: "contests#offline_update"
|
patch "public/:id/offline/:token", to: "contests#offline_update"
|
||||||
get "public/:id/offline/:token/completed", to: "contests#offline_completed"
|
get "public/:id/offline/:token/completed", to: "contests#offline_completed"
|
||||||
|
get "public/p/:token", to: "contestants#get_public_completion"
|
||||||
|
post "public/p/:token", to: "contestants#post_public_completion"
|
||||||
|
get "public/p/:token/updated", to: "contestants#public_completion_updated"
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user