Files
puzzle-scoreboard/app/models/offline.rb
sto b88460ae71
Some checks failed
CI / scan_ruby (push) Successful in 16s
CI / scan_js (push) Successful in 13s
CI / lint (push) Successful in 13s
CI / test (push) Failing after 35s
Implement projected time
2025-11-14 12:13:09 +01:00

86 lines
2.6 KiB
Ruby

# == Schema Information
#
# Table name: offlines
#
# 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
# submitted :boolean
# created_at :datetime not null
# updated_at :datetime not null
# completion_id :integer
# contest_id :integer not null
# contestant_id :integer
#
# Indexes
#
# index_offlines_on_completion_id (completion_id)
# index_offlines_on_contest_id (contest_id)
# index_offlines_on_contestant_id (contestant_id)
#
# Foreign Keys
#
# completion_id (completion_id => completions.id)
# contest_id (contest_id => contests.id)
# contestant_id (contestant_id => contestants.id)
#
class Offline < ApplicationRecord
belongs_to :contest
belongs_to :contestant, optional: true
belongs_to :completion, optional: true
has_many_attached :images
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
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
if self.submitted && self.images.length < 2
errors.add(:end_image, I18n.t("activerecord.errors.models.offline.attributes.end_image.blank"))
end
end
def start_image_is_present
if !self.images.attached?
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