Messages to completions conversion
This commit is contained in:
@@ -27,8 +27,6 @@ class CompletionsController < ApplicationController
|
||||
extend_completions!(@completion.contestant)
|
||||
redirect_to contest_path(@contest)
|
||||
else
|
||||
logger = Logger.new(STDOUT)
|
||||
logger.info(@completion.errors)
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
@@ -36,9 +34,7 @@ class CompletionsController < ApplicationController
|
||||
def update
|
||||
authorize @contest
|
||||
|
||||
if params[:contestant_id]
|
||||
@completion.contestant_id = params[:contestant_id]
|
||||
end
|
||||
@completion.contestant_id = params[:contestant_id] if params[:contestant_id]
|
||||
if @completion.update(completion_params)
|
||||
extend_completions!(@completion.contestant)
|
||||
redirect_to @contest
|
||||
@@ -74,6 +70,6 @@ class CompletionsController < ApplicationController
|
||||
end
|
||||
|
||||
def completion_params
|
||||
params.expect(completion: [ :time_seconds, :contestant_id, :puzzle_id ])
|
||||
params.expect(completion: [ :display_time_from_start, :contestant_id, :puzzle_id ])
|
||||
end
|
||||
end
|
||||
|
@@ -17,7 +17,7 @@ class ContestsController < ApplicationController
|
||||
@action_path = edit_contest_path(@contest)
|
||||
@contestants = @contest.contestants.order(:name)
|
||||
@puzzles = @contest.puzzles.order(:id)
|
||||
@messages = @contest.messages.order(:id)
|
||||
@messages = @contest.messages.order(:time_seconds)
|
||||
set_badges
|
||||
end
|
||||
|
||||
|
@@ -1,8 +1,15 @@
|
||||
class MessagesController < ApplicationController
|
||||
include CompletionsConcern
|
||||
|
||||
skip_before_action :verify_authenticity_token, only: %i[ create ]
|
||||
|
||||
before_action :set_contest, only: %i[ destroy ]
|
||||
before_action :set_message, only: %i[ destroy ]
|
||||
before_action :set_contest, only: %i[ convert destroy ]
|
||||
before_action :set_message, only: %i[ convert destroy ]
|
||||
before_action :set_data, only: %i[ convert ]
|
||||
|
||||
def self.local_prefixes
|
||||
super + [ "completions" ]
|
||||
end
|
||||
|
||||
def create
|
||||
allow_unauthenticated_access
|
||||
@@ -10,7 +17,8 @@ class MessagesController < ApplicationController
|
||||
|
||||
@message_params = message_params
|
||||
@contest = Contest.find_by_token_for(:token, params[:token])
|
||||
@message = Message.new(text: params[:text], time_seconds: params[:time_seconds], contest: @contest)
|
||||
@message = Message.new(text: params[:text], time_seconds: params[:time_seconds],
|
||||
display_time: display_time(params[:time_seconds]), contest: @contest)
|
||||
if @contest && @message.save
|
||||
respond_to do |format|
|
||||
format.json { render json: {}, status: 200 }
|
||||
@@ -22,6 +30,15 @@ class MessagesController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def convert
|
||||
authorize @contest
|
||||
|
||||
@completion = Completion.new()
|
||||
@completion.display_time_from_start = @message.display_time
|
||||
|
||||
render "completions/new"
|
||||
end
|
||||
|
||||
def destroy
|
||||
authorize @contest
|
||||
|
||||
@@ -36,10 +53,15 @@ class MessagesController < ApplicationController
|
||||
end
|
||||
|
||||
def set_message
|
||||
@message = Message.find(params[:id])
|
||||
@message = Message.find(params[:message_id])
|
||||
end
|
||||
|
||||
def set_data
|
||||
@contestants = @contest.contestants
|
||||
@puzzles = @contest.puzzles
|
||||
end
|
||||
|
||||
def message_params
|
||||
params.expect(message: [ :text, :time_seconds, :token ])
|
||||
params.expect(message: [ :author, :text, :time_seconds, :token ])
|
||||
end
|
||||
end
|
||||
|
@@ -29,7 +29,17 @@ class Completion < ApplicationRecord
|
||||
belongs_to :contestant
|
||||
belongs_to :puzzle
|
||||
|
||||
validates :time_seconds, presence: true
|
||||
validates_numericality_of :time_seconds
|
||||
before_save :add_time_seconds
|
||||
|
||||
validates :display_time_from_start, presence: true, format: { with: /\A((\d\d|\d):\d\d|\d\d|\d):\d\d\z/ }
|
||||
validates :puzzle_id, uniqueness: { scope: :contestant }
|
||||
|
||||
def add_time_seconds
|
||||
arr = display_time_from_start.split(":")
|
||||
if arr.size == 3
|
||||
self.time_seconds = arr[0].to_i * 3600 + arr[1].to_i * 60 + arr[2].to_i
|
||||
elsif arr.size == 2
|
||||
self.time_seconds = arr[0].to_i * 60 + arr[1].to_i
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@@ -3,6 +3,8 @@
|
||||
# Table name: messages
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# author :string
|
||||
# display_time :string
|
||||
# text :string not null
|
||||
# time_seconds :integer not null
|
||||
# created_at :datetime not null
|
||||
@@ -20,5 +22,6 @@
|
||||
class Message < ApplicationRecord
|
||||
belongs_to :contest
|
||||
|
||||
validates :author, presence: true
|
||||
validates :text, presence: true
|
||||
end
|
||||
|
@@ -15,6 +15,10 @@ class ContestPolicy < ApplicationPolicy
|
||||
true
|
||||
end
|
||||
|
||||
def convert?
|
||||
record.user.id == user.id || user.admin?
|
||||
end
|
||||
|
||||
def edit?
|
||||
record.user.id == user.id || user.admin?
|
||||
end
|
||||
|
@@ -1,9 +1,21 @@
|
||||
= form_with model: completion, url: url, method: method do |form|
|
||||
- if @message
|
||||
.row.mb-3
|
||||
.col
|
||||
h4 = t("messages.singular").capitalize
|
||||
.alert.alert-secondary
|
||||
b
|
||||
= @message.author
|
||||
br
|
||||
= @message.text
|
||||
.row
|
||||
.col
|
||||
h4 = t("completions.singular").capitalize
|
||||
.row.mb-3
|
||||
.col
|
||||
.form-floating
|
||||
= form.text_field :time_seconds, autocomplete: "off", class: "form-control"
|
||||
= form.label :time_seconds, class: "required"
|
||||
= form.text_field :display_time_from_start, autocomplete: "off", class: "form-control"
|
||||
= form.label :display_time_from_start, class: "required"
|
||||
.row.mb-3
|
||||
.col
|
||||
.form-floating
|
||||
|
@@ -51,17 +51,21 @@
|
||||
tr
|
||||
th scope="col"
|
||||
| Time
|
||||
th scope="col"
|
||||
| Author
|
||||
th scope="col"
|
||||
| Text
|
||||
tbody
|
||||
- @messages.each do |message|
|
||||
tr.align-middle scope="row"
|
||||
td
|
||||
= message.time_seconds
|
||||
= message.display_time
|
||||
td
|
||||
= message.author
|
||||
td
|
||||
= message.text
|
||||
td
|
||||
a.btn.btn-sm.btn-secondary href="" style="white-space: nowrap;"
|
||||
a.btn.btn-sm.btn-secondary href=contest_message_convert_path(@contest, message) style="white-space: nowrap;"
|
||||
| Add completion
|
||||
td
|
||||
= link_to "Delete", contest_message_path(@contest, message), data: { turbo_method: :delete }, class: "btn btn-sm btn-danger"
|
||||
|
Reference in New Issue
Block a user