Offline participation form: ask for missing/remaining pieces
This commit is contained in:
@@ -35,7 +35,9 @@ class CompletionsController < ApplicationController
|
||||
@completion = Completion.new(completion_params)
|
||||
@completion.contest_id = @contest.id
|
||||
if @completion.save
|
||||
extend_completions!(@completion.contestant)
|
||||
if @completion.display_time_from_start.present?
|
||||
extend_completions!(@completion.contestant)
|
||||
end
|
||||
if @contestant && !params[:completion].key?(:message_id)
|
||||
redirect_to edit_contest_contestant_path(@contest, @contestant), notice: t("completions.new.notice")
|
||||
else
|
||||
|
||||
@@ -147,6 +147,8 @@ class ContestsController < ApplicationController
|
||||
@offline.completed = true
|
||||
@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.save
|
||||
if @contest.puzzles.length > 0
|
||||
dp = display_time(@offline.end_time.to_i - @offline.start_time.to_i)
|
||||
@@ -209,6 +211,6 @@ class ContestsController < ApplicationController
|
||||
end
|
||||
|
||||
def offline_end_params
|
||||
params.expect(offline: [ :completed, :end_image ])
|
||||
params.expect(offline: [ :completed, :end_image, :remaining_pieces, :missing_pieces ])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -34,16 +34,17 @@ class Completion < ApplicationRecord
|
||||
belongs_to :puzzle
|
||||
belongs_to :message, optional: true
|
||||
|
||||
before_save :add_time_seconds
|
||||
before_save :add_time_seconds, if: -> { display_time_from_start.present? }
|
||||
before_save :nullify_display_time
|
||||
|
||||
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 :contestant_id, uniqueness: { scope: :puzzle }, if: -> { contest.puzzles.size == 1 }
|
||||
validates :puzzle_id, uniqueness: { scope: :contestant }, if: -> { contest.puzzles.size > 1 }
|
||||
validate :remaining_pieces_is_correct
|
||||
validates :remaining_pieces, numericality: { only_integer: true }, if: -> { remaining_pieces.present? }
|
||||
validate :remaining_pieces_is_correct, if: -> { remaining_pieces.present? }
|
||||
|
||||
def remaining_pieces_is_correct
|
||||
if self.remaining_pieces && self.remaining_pieces > self.puzzle.pieces
|
||||
if self.remaining_pieces > self.puzzle.pieces
|
||||
errors.add(:remaining_pieces, "Cannot be greater than the number of pieces for this puzzle")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,15 +2,17 @@
|
||||
#
|
||||
# Table name: offlines
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# completed :boolean
|
||||
# end_time :datetime
|
||||
# name :string not null
|
||||
# start_time :datetime not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# contest_id :integer not null
|
||||
# contestant_id :integer
|
||||
# id :integer not null, primary key
|
||||
# completed :boolean
|
||||
# end_time :datetime
|
||||
# missing_pieces :integer
|
||||
# name :string not null
|
||||
# remaining_pieces :integer
|
||||
# start_time :datetime not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# contest_id :integer not null
|
||||
# contestant_id :integer
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
@@ -36,7 +38,27 @@ class Offline < ApplicationRecord
|
||||
validate :end_image_is_present
|
||||
validate :start_image_is_present
|
||||
|
||||
validates :missing_pieces, numericality: { only_integer: true }, if: -> { missing_pieces.present? }
|
||||
validates :remaining_pieces, numericality: { only_integer: true }, if: -> { remaining_pieces.present? }
|
||||
validate :missing_pieces_is_correct, if: -> { missing_pieces.present? }
|
||||
validate :remaining_pieces_is_correct, if: -> { remaining_pieces.present? }
|
||||
|
||||
def missing_pieces_is_correct
|
||||
if self.contest.puzzles.length > 0 && self.missing_pieces > self.contest.puzzles[0].pieces
|
||||
errors.add(:remaining_pieces, "Cannot be greater than the number of pieces for this puzzle")
|
||||
end
|
||||
end
|
||||
|
||||
def remaining_pieces_is_correct
|
||||
if self.contest.puzzles.length > 0 && self.remaining_pieces > self.contest.puzzles[0].pieces
|
||||
errors.add(:remaining_pieces, "Cannot be greater than the number of pieces for this puzzle")
|
||||
end
|
||||
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
|
||||
errors.add(:end_image, I18n.t("activerecord.errors.models.offline.attributes.end_image.blank"))
|
||||
end
|
||||
|
||||
@@ -35,6 +35,20 @@
|
||||
}
|
||||
|
||||
setMaxUploadSize();
|
||||
.row.mt-4.mb-3
|
||||
.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
|
||||
.col
|
||||
.form-floating
|
||||
= form.text_field :remaining_pieces, autocomplete: "off", class: "form-control"
|
||||
= form.label :remaining_pieces
|
||||
.form-text
|
||||
= t("offlines.form.remaining_pieces")
|
||||
.row.mt-4
|
||||
.col
|
||||
= form.submit t("helpers.buttons.end"), class: "btn btn-primary"
|
||||
Reference in New Issue
Block a user