Implement missing & remaining pieces propagation + cleaner forms
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user