Implement missing & remaining pieces propagation + cleaner forms
All checks were successful
CI / scan_ruby (push) Successful in 18s
CI / scan_js (push) Successful in 14s
CI / lint (push) Successful in 13s
CI / test (push) Successful in 34s

This commit is contained in:
sto
2025-11-10 12:26:48 +01:00
parent 6549124c08
commit f4136ea58a
15 changed files with 175 additions and 38 deletions

View File

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

View File

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