Implement missing & remaining pieces propagation + cleaner forms
This commit is contained in:
@@ -24,6 +24,7 @@ class CompletionsController < ApplicationController
|
||||
end
|
||||
|
||||
@completion = Completion.new
|
||||
@completion.completed = true
|
||||
if params[:contestant_id]
|
||||
@completion.contestant_id = params[:contestant_id]
|
||||
end
|
||||
@@ -111,6 +112,6 @@ class CompletionsController < ApplicationController
|
||||
end
|
||||
|
||||
def completion_params
|
||||
params.expect(completion: [ :display_time_from_start, :remaining_pieces, :contestant_id, :message_id, :puzzle_id ])
|
||||
params.expect(completion: [ :display_time_from_start, :completed, :missing_pieces, :remaining_pieces, :contestant_id, :message_id, :puzzle_id ])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -131,7 +131,7 @@ class ContestsController < ApplicationController
|
||||
not_found and return
|
||||
end
|
||||
|
||||
if @offline.images.length > 1
|
||||
if @offline.submitted
|
||||
render :offline_already_submitted and return
|
||||
end
|
||||
end
|
||||
@@ -144,11 +144,15 @@ class ContestsController < ApplicationController
|
||||
not_found and return
|
||||
end
|
||||
|
||||
@offline.completed = true
|
||||
@offline.submitted = true
|
||||
@offline.completed = params[:offline][:completed]
|
||||
@offline.end_time = Time.now()
|
||||
@offline.images.attach(params[:offline][:end_image])
|
||||
@offline.missing_pieces = params[:offline][:missing_pieces]
|
||||
@offline.remaining_pieces = params[:offline][:remaining_pieces]
|
||||
if @offline.completed
|
||||
@offline.missing_pieces = params[:offline][:missing_pieces]
|
||||
else
|
||||
@offline.remaining_pieces = params[:offline][:remaining_pieces]
|
||||
end
|
||||
if @offline.save
|
||||
if @contest.puzzles.length > 0
|
||||
dp = display_time(@offline.end_time.to_i - @offline.start_time.to_i)
|
||||
@@ -156,7 +160,10 @@ class ContestsController < ApplicationController
|
||||
Completion.create(contest: @contest,
|
||||
contestant: contestant,
|
||||
puzzle: @contest.puzzles[0],
|
||||
display_time_from_start: dp)
|
||||
completed: @offline.completed,
|
||||
display_time_from_start: dp,
|
||||
missing_pieces: @offline.missing_pieces,
|
||||
remaining_pieces: @offline.remaining_pieces)
|
||||
extend_completions!(contestant)
|
||||
end
|
||||
redirect_to "/public/#{@contest.friendly_id}/offline/#{@offline.generate_token_for(:token)}/completed"
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
# Table name: completions
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# completed :boolean
|
||||
# display_relative_time :string
|
||||
# display_time_from_start :string
|
||||
# missing_pieces :integer
|
||||
# remaining_pieces :integer
|
||||
# time_seconds :integer
|
||||
# created_at :datetime not null
|
||||
@@ -36,8 +38,10 @@ class Completion < ApplicationRecord
|
||||
|
||||
before_save :add_time_seconds, if: -> { display_time_from_start.present? }
|
||||
before_save :nullify_display_time
|
||||
before_save :clean_pieces
|
||||
|
||||
validates :display_time_from_start, presence: true, format: { with: /\A(((\d\d|\d):\d\d|\d\d|\d):\d\d|\d\d|\d)\z/ }, if: -> { remaining_pieces == nil }
|
||||
validates :display_time_from_start, presence: true, format: { with: /\A(((\d\d|\d):\d\d|\d\d|\d):\d\d|\d\d|\d)\z/ }, if: -> { completed }
|
||||
validates :remaining_pieces, presence: true, if: -> { !completed }
|
||||
validates :contestant_id, uniqueness: { scope: :puzzle }, if: -> { contest.puzzles.size == 1 }
|
||||
validates :puzzle_id, uniqueness: { scope: :contestant }, if: -> { contest.puzzles.size > 1 }
|
||||
validates :remaining_pieces, numericality: { only_integer: true }, if: -> { remaining_pieces.present? }
|
||||
@@ -66,4 +70,12 @@ class Completion < ApplicationRecord
|
||||
self.time_seconds = arr[0].to_i
|
||||
end
|
||||
end
|
||||
|
||||
def clean_pieces
|
||||
if self.completed
|
||||
self.remaining_pieces = nil
|
||||
else
|
||||
self.missing_pieces = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
# name :string not null
|
||||
# remaining_pieces :integer
|
||||
# start_time :datetime not null
|
||||
# submitted :boolean
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# contest_id :integer not null
|
||||
@@ -32,7 +33,10 @@ class Offline < ApplicationRecord
|
||||
|
||||
generates_token_for :token
|
||||
|
||||
before_save :clean_pieces
|
||||
|
||||
validates :name, presence: true
|
||||
validates :remaining_pieces, presence: true, if: -> { submitted && !completed }
|
||||
validates :start_time, presence: true
|
||||
|
||||
validate :end_image_is_present
|
||||
@@ -56,10 +60,7 @@ class Offline < ApplicationRecord
|
||||
end
|
||||
|
||||
def end_image_is_present
|
||||
logger = Logger.new(STDOUT)
|
||||
logger.info(self.missing_pieces)
|
||||
logger.info(self.missing_pieces.present?)
|
||||
if self.completed && self.images.length < 2
|
||||
if self.submitted && self.images.length < 2
|
||||
errors.add(:end_image, I18n.t("activerecord.errors.models.offline.attributes.end_image.blank"))
|
||||
end
|
||||
end
|
||||
@@ -69,4 +70,12 @@ class Offline < ApplicationRecord
|
||||
errors.add(:images, I18n.t("activerecord.errors.models.offline.attributes.start_image.blank"))
|
||||
end
|
||||
end
|
||||
|
||||
def clean_pieces
|
||||
if self.completed
|
||||
self.remaining_pieces = nil
|
||||
else
|
||||
self.missing_pieces = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -32,17 +32,55 @@
|
||||
- else
|
||||
= form.hidden_field :puzzle_id
|
||||
.row.mb-3
|
||||
.col
|
||||
.form-check.form-switch
|
||||
= form.check_box :completed, class: "form-check-input"
|
||||
= form.label :completed
|
||||
javascript:
|
||||
completedEl = document.getElementById('completion_completed');
|
||||
completedEl.addEventListener('change', (e) => {
|
||||
const timeEl = document.getElementById('time');
|
||||
const missingPiecesEl = document.getElementById('missing_pieces');
|
||||
const remainingPiecesEl = document.getElementById('remaining_pieces');
|
||||
if (e.target.checked) {
|
||||
timeEl.style.display = 'block';
|
||||
missingPiecesEl.style.display = 'block';
|
||||
remainingPiecesEl.style.display = 'none';
|
||||
} else {
|
||||
timeEl.style.display = 'none';
|
||||
missingPiecesEl.style.display = 'none';
|
||||
remainingPiecesEl.style.display = 'block';
|
||||
}
|
||||
})
|
||||
.row.mb-3 id="time"
|
||||
.col
|
||||
.form-floating
|
||||
= form.text_field :display_time_from_start, autocomplete: "off", class: "form-control"
|
||||
= form.label :display_time_from_start, class: "required"
|
||||
.row.mb-3
|
||||
.row.mb-3 id="missing_pieces"
|
||||
.col
|
||||
.form-floating
|
||||
= form.text_field :missing_pieces, autocomplete: "off", class: "form-control"
|
||||
= form.label :missing_pieces
|
||||
.row.mb-3 id="remaining_pieces" style="display: none;"
|
||||
.col
|
||||
.form-floating
|
||||
= form.text_field :remaining_pieces, autocomplete: "off", class: "form-control"
|
||||
= form.label :remaining_pieces
|
||||
.form-text
|
||||
= t("activerecord.attributes.completion.remaining_pieces_description")
|
||||
javascript:
|
||||
completedEl = document.getElementById('completion_completed');
|
||||
timeEl = document.getElementById('time');
|
||||
missingPiecesEl = document.getElementById('missing_pieces');
|
||||
remainingPiecesEl = document.getElementById('remaining_pieces');
|
||||
if (completedEl.checked) {
|
||||
timeEl.style.display = 'block';
|
||||
missingPiecesEl.style.display = 'block';
|
||||
remainingPiecesEl.style.display = 'none';
|
||||
} else {
|
||||
timeEl.style.display = 'none';
|
||||
missingPiecesEl.style.display = 'none';
|
||||
remainingPiecesEl.style.display = 'block';
|
||||
}
|
||||
.row
|
||||
.col
|
||||
= form.submit submit_text, class: "btn btn-primary"
|
||||
@@ -33,6 +33,10 @@
|
||||
.row.mt-5
|
||||
.col
|
||||
h3 Completions
|
||||
.row
|
||||
.col
|
||||
.alert.alert-info
|
||||
= t("contestants.edit.completions_note")
|
||||
table.table.table-striped.table-hover
|
||||
thead
|
||||
tr
|
||||
@@ -44,6 +48,8 @@
|
||||
- else
|
||||
th scope="col"
|
||||
= t("activerecord.attributes.completion.display_time")
|
||||
th scope="col"
|
||||
= t("activerecord.attributes.completion.missing_pieces")
|
||||
th scope="col"
|
||||
= t("activerecord.attributes.completion.remaining_pieces")
|
||||
th scope="col"
|
||||
@@ -56,6 +62,8 @@
|
||||
- if @contest.puzzles.size > 1
|
||||
td
|
||||
= completion.display_relative_time
|
||||
td
|
||||
= completion.missing_pieces
|
||||
td
|
||||
= completion.remaining_pieces
|
||||
td
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
= form_with model: @offline, url: "/public/#{@contest.friendly_id}/offline/#{@offline.generate_token_for(:token)}" do |form|
|
||||
= form.hidden_field :completed
|
||||
h3 = t("offlines.form.start_message")
|
||||
h3 = t("offlines.form.start_message", name: @offline.name)
|
||||
h1 id="display-time" style="font-size: 80px;"
|
||||
javascript:
|
||||
const startTime = #{@offline.start_time.to_i};
|
||||
startTime = #{@offline.start_time.to_i};
|
||||
function updateTime() {
|
||||
const displayTimeEl = document.getElementById('display-time');
|
||||
const s = Math.floor((Date.now() - 1000 * startTime) / 1000);
|
||||
let ss = s % 60;
|
||||
let mm = Math.floor(s / 60) % 60;
|
||||
let hh = Math.floor(s / 3600);
|
||||
displayTimeEl.innerHTML = `${hh < 10 ? `0${hh}` : hh}:${mm < 10 ? `0${mm}` : mm}:${ss < 10 ? `0${ss}` : ss}`;
|
||||
setTimeout(updateTime, 1000);
|
||||
if (displayTimeEl) {
|
||||
const s = Math.floor((Date.now() - 1000 * startTime) / 1000);
|
||||
let ss = s % 60;
|
||||
let mm = Math.floor(s / 60) % 60;
|
||||
let hh = Math.floor(s / 3600);
|
||||
displayTimeEl.innerHTML = `${hh < 10 ? `0${hh}` : hh}:${mm < 10 ? `0${mm}` : mm}:${ss < 10 ? `0${ss}` : ss}`;
|
||||
setTimeout(updateTime, 1000);
|
||||
} else {
|
||||
setTimeout(updateTime, 20);
|
||||
}
|
||||
}
|
||||
setTimeout(updateTime, 50);
|
||||
setTimeout(updateTime, 1);
|
||||
.row.mt-5.mb-3
|
||||
.col
|
||||
.form-text.mb-1
|
||||
@@ -35,20 +38,49 @@
|
||||
}
|
||||
|
||||
setMaxUploadSize();
|
||||
.row.mt-4.mb-3
|
||||
.row.mb-3
|
||||
.col
|
||||
.form-check.form-switch
|
||||
= form.check_box :completed, class: "form-check-input"
|
||||
= form.label :completed
|
||||
javascript:
|
||||
completedEl = document.getElementById('offline_completed');
|
||||
completedEl.addEventListener('change', (e) => {
|
||||
missingPiecesEl = document.getElementById('missing_pieces');
|
||||
remainingPiecesEl = document.getElementById('remaining_pieces');
|
||||
if (e.target.checked) {
|
||||
missingPiecesEl.style.display = 'block';
|
||||
remainingPiecesEl.style.display = 'none';
|
||||
} else {
|
||||
missingPiecesEl.style.display = 'none';
|
||||
remainingPiecesEl.style.display = 'block';
|
||||
}
|
||||
})
|
||||
.row.mb-3 id="missing_pieces" style="display: none;"
|
||||
.col
|
||||
.form-floating
|
||||
= form.text_field :missing_pieces, autocomplete: "off", class: "form-control"
|
||||
= form.label :missing_pieces
|
||||
.form-text
|
||||
= t("offlines.form.missing_pieces")
|
||||
.row.mb-3
|
||||
.row.mb-3 id="remaining_pieces"
|
||||
.col
|
||||
.form-floating
|
||||
= form.text_field :remaining_pieces, autocomplete: "off", class: "form-control"
|
||||
= form.label :remaining_pieces
|
||||
.form-text
|
||||
= t("offlines.form.remaining_pieces")
|
||||
javascript:
|
||||
completedEl = document.getElementById('offline_completed');
|
||||
missingPiecesEl = document.getElementById('missing_pieces');
|
||||
remainingPiecesEl = document.getElementById('remaining_pieces');
|
||||
if (completedEl.checked) {
|
||||
missingPiecesEl.style.display = 'block';
|
||||
remainingPiecesEl.style.display = 'none';
|
||||
} else {
|
||||
missingPiecesEl.style.display = 'none';
|
||||
remainingPiecesEl.style.display = 'block';
|
||||
}
|
||||
.row.mt-4
|
||||
.col
|
||||
= form.submit t("helpers.buttons.end"), class: "btn btn-primary"
|
||||
Reference in New Issue
Block a user