Skip to content

Commit

Permalink
Initialize all solutions attribute on class init
Browse files Browse the repository at this point in the history
Signed-off-by: Kostiantyn Kostiuk <[email protected]>
  • Loading branch information
kostyanf14 committed Sep 2, 2023
1 parent 992bab4 commit 3ae006d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/controllers/uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ def update

def resource_params
params.require(:upload).permit(:secret, solutions_attributes: %i[task_id file])
.merge(ips: ip_addresses, device_id: @device_id, contest:)
.reverse_merge(ips: ip_addresses, device_id: @device_id, contest:)
end

def initialize_resource
solutions_attributes = contest.tasks.order(:id).pluck(:id).map { { task_id: _1 } }
@resource = Upload.new solutions_attributes:, contest:
@resource = Upload.new contest:, solutions_attributes:
end

def build_resource
Expand Down
4 changes: 1 addition & 3 deletions app/models/solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Solution < ApplicationRecord
belongs_to :task, inverse_of: :solutions
has_one_attached :file

before_validation :assign_upload_number, on: :create
after_initialize :assign_upload_number, if: -> { new_record? && user && task }
after_commit :send_email, on: :create

delegate :display_name, :upload_limit, :accepted_ext, :file_names, to: :task, prefix: true, allow_nil: true
Expand All @@ -18,8 +18,6 @@ def send_email
end

def assign_upload_number
return unless user && task

# TODO: fix possible race condition
self.upload_number = Solution.where(user:, task:).count + 1
end
Expand Down
3 changes: 1 addition & 2 deletions app/models/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ def user

def solutions_attributes= attributes
attributes = attributes.values if attributes.is_a? Hash
@solutions = attributes.map { Solution.new _1 }
@solutions = attributes.map { Solution.new **_1, user:, ips:, device_id: }
end

def save
solutions.reject! { _1.file.blank? }
solutions.each { _1.assign_attributes user:, ips:, device_id: }
solutions.each &:save if valid?
end

Expand Down

0 comments on commit 3ae006d

Please sign in to comment.