Implement file name validation based on path, name, and filetype upon file creation
This commit is contained in:
@ -2,6 +2,17 @@ require File.expand_path('../../../uploaders/file_uploader', __FILE__)
|
||||
require File.expand_path('../../../../lib/active_model/validations/boolean_presence_validator', __FILE__)
|
||||
|
||||
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: record.context).to_a
|
||||
unless existing_files.empty?
|
||||
record.errors[:base] << 'Duplicate'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class File < ActiveRecord::Base
|
||||
include DefaultValues
|
||||
|
||||
@ -44,6 +55,8 @@ module CodeOcean
|
||||
validates :weight, if: :teacher_defined_test?, numericality: true, presence: true
|
||||
validates :weight, absence: true, unless: :teacher_defined_test?
|
||||
|
||||
validates_with FileNameValidator, fields: [:name, :path, :file_type_id]
|
||||
|
||||
ROLES.each do |role|
|
||||
define_method("#{role}?") { self.role == role }
|
||||
end
|
||||
|
Reference in New Issue
Block a user