Implement file name validation based on path, name, and filetype upon file creation

This commit is contained in:
Maximilian Grundke
2016-05-10 16:03:52 +02:00
parent 7e4c886b65
commit 9fc974af20
4 changed files with 32 additions and 0 deletions

View File

@ -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