From 40c7a508340377fc0f8be5ff250b9c3b69a41646 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Sat, 18 Feb 2023 19:28:26 +0100 Subject: [PATCH] Directly use ActiveRecord object instead of ID This change will improve the performance by reducing unnecessary SELECT queries. --- app/controllers/concerns/file_parameters.rb | 2 +- app/controllers/concerns/submission_parameters.rb | 5 ++--- app/validators/code_ocean/file_name_validator.rb | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/app/controllers/concerns/file_parameters.rb b/app/controllers/concerns/file_parameters.rb index 5c4f48f8..c847f03a 100644 --- a/app/controllers/concerns/file_parameters.rb +++ b/app/controllers/concerns/file_parameters.rb @@ -8,7 +8,7 @@ module FileParameters next true if file.nil? || file.hidden || file.read_only # avoid that public files from other contexts can be created # `next` is similar to an early return and will proceed with the next iteration of the loop - next true if file.context_type == 'Exercise' && file.context_id != exercise.id + next true if file.context_type == 'Exercise' && file.context != exercise next true if file.context_type == 'Submission' && file.context.user != current_user next true if file.context_type == 'CommunitySolution' && controller_name != 'community_solutions' diff --git a/app/controllers/concerns/submission_parameters.rb b/app/controllers/concerns/submission_parameters.rb index 12a37ec0..c2c7de6f 100644 --- a/app/controllers/concerns/submission_parameters.rb +++ b/app/controllers/concerns/submission_parameters.rb @@ -21,9 +21,8 @@ module SubmissionParameters def merge_user(params) # The study_group_id might not be present in the session (e.g. for internal users), resulting in session[:study_group_id] = nil which is intended. params.merge( - user_id: current_user.id, - user_type: current_user.class.name, - study_group_id: session[:study_group_id] + user: current_user, + study_group_id: current_user.current_study_group_id ) end end diff --git a/app/validators/code_ocean/file_name_validator.rb b/app/validators/code_ocean/file_name_validator.rb index 0c377f01..19b7548c 100644 --- a/app/validators/code_ocean/file_name_validator.rb +++ b/app/validators/code_ocean/file_name_validator.rb @@ -3,8 +3,8 @@ module CodeOcean class FileNameValidator < ActiveModel::Validator def validate(record) - existing_files = File.where(name: record.name, path: record.path, file_type_id: record.file_type_id, - context_id: record.context_id, context_type: record.context_type).to_a + existing_files = File.where(name: record.name, path: record.path, file_type: record.file_type, + context: record.context) if !existing_files.empty? && (!record.context.is_a?(Exercise) || record.context.new_record?) record.errors.add(:base, 'Duplicate') end