Files
codeocean/app/validators/code_ocean/file_name_validator.rb
Sebastian Serth 40c7a50834 Directly use ActiveRecord object instead of ID
This change will improve the performance by reducing unnecessary SELECT queries.
2023-02-18 19:28:26 +01:00

14 lines
431 B
Ruby

# frozen_string_literal: true
module CodeOcean
class FileNameValidator < ActiveModel::Validator
def validate(record)
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
end
end
end