From 576cddf29794766ede8a43b264eb3042d40268d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Pi=C4=8Dman?= Date: Thu, 11 Dec 2025 09:24:08 +0100 Subject: [PATCH] #9 Active Storage - Litmus --- app/models/dmsf_file_revision.rb | 7 +++- .../dmsf_file_extension_validator.rb | 5 ++- .../dmsf_file_revision_name_validator.rb | 35 +++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 app/validators/dmsf_file_revision_name_validator.rb diff --git a/app/models/dmsf_file_revision.rb b/app/models/dmsf_file_revision.rb index 4805c7cc..b07048e5 100644 --- a/app/models/dmsf_file_revision.rb +++ b/app/models/dmsf_file_revision.rb @@ -92,7 +92,12 @@ class DmsfFileRevision < ApplicationRecord validates :title, presence: true, length: { maximum: 255 }, dmsf_file_name: true validates :major_version, presence: true - validates :name, presence: true, dmsf_file_name: true, length: { maximum: 255 }, dmsf_file_extension: true + validates :name, + presence: true, + dmsf_file_name: true, + length: { maximum: 255 }, + dmsf_file_extension: true, + dmsf_file_revision_name: true validates :description, length: { maximum: 1.kilobyte } validates :size, dmsf_max_file_size: true diff --git a/app/validators/dmsf_file_extension_validator.rb b/app/validators/dmsf_file_extension_validator.rb index 7f8cc851..c6f45570 100644 --- a/app/validators/dmsf_file_extension_validator.rb +++ b/app/validators/dmsf_file_extension_validator.rb @@ -22,11 +22,10 @@ class DmsfFileExtensionValidator < ActiveModel::EachValidator include Redmine::I18n def validate_each(record, attribute, value) - return unless attribute.to_s == 'name' - extension = File.extname(value) + return if Attachment.valid_extension?(extension) - record.errors.add(:base, l(:error_attachment_extension_not_allowed, extension: extension)) + record.errors.add attribute, l(:error_attachment_extension_not_allowed, extension: extension) end end diff --git a/app/validators/dmsf_file_revision_name_validator.rb b/app/validators/dmsf_file_revision_name_validator.rb new file mode 100644 index 00000000..4704cfd1 --- /dev/null +++ b/app/validators/dmsf_file_revision_name_validator.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +# Redmine plugin for Document Management System "Features" +# +# Vít Jonáš , Karel Pičman +# +# This file is part of Redmine DMSF plugin. +# +# Redmine DMSF plugin is free software: you can redistribute it and/or modify it under the terms of the GNU General +# Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any +# later version. +# +# Redmine DMSF plugin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even +# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with Redmine DMSF plugin. If not, see +# . + +# File name validator +class DmsfFileRevisionNameValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + # Check name/title uniqueness + DmsfFile + .visible + .where(project_id: record.dmsf_file.project_id, dmsf_folder_id: record.dmsf_file.dmsf_folder_id) + .where.not(id: record.dmsf_file_id) + .find_each do |dmsf_file| + if dmsf_file.name == value + record.errors.add attribute, :taken + break + end + end + end +end