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