diff --git a/app/models/dmsf_folder.rb b/app/models/dmsf_folder.rb index 6af5feed..1e196386 100644 --- a/app/models/dmsf_folder.rb +++ b/app/models/dmsf_folder.rb @@ -96,6 +96,7 @@ class DmsfFolder < ActiveRecord::Base validates_uniqueness_of :title, scope: [:dmsf_folder_id, :project_id, :deleted], conditions: -> { where(deleted: STATUS_ACTIVE) } validates :description, length: { maximum: 65535 } + validates :dmsf_folder, dmsf_folder_parent: true, if: Proc.new { |folder| !folder.new_record? } before_create :default_values diff --git a/app/validators/dmsf_folder_parent_validator.rb b/app/validators/dmsf_folder_parent_validator.rb new file mode 100644 index 00000000..747e4087 --- /dev/null +++ b/app/validators/dmsf_folder_parent_validator.rb @@ -0,0 +1,37 @@ +# encoding: utf-8 +# frozen_string_literal: true +# +# Redmine plugin for Document Management System "Features" +# +# Copyright © 2011-20 Karel Pičman +# +# This program 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 2 +# of the License, or (at your option) any later version. +# +# This program 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 this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class DmsfFolderParentValidator < ActiveModel::EachValidator + + # Don't allow save folders with a parent pointing to the same folder + def validate_each(record, attribute, value) + folder = value + while folder + if folder == record + record.errors.add attribute, :invalid + return false + end + folder = folder.dmsf_folder + end + return true + end + +end \ No newline at end of file diff --git a/lib/redmine_dmsf.rb b/lib/redmine_dmsf.rb index d0fac574..4c05e632 100644 --- a/lib/redmine_dmsf.rb +++ b/lib/redmine_dmsf.rb @@ -29,6 +29,7 @@ DMSF_MAX_NOTIFICATION_RECEIVERS_INFO = 10 require_dependency File.dirname(__FILE__) + '/../app/validators/dmsf_file_name_validator' require_dependency File.dirname(__FILE__) + '/../app/validators/dmsf_workflow_name_validator' require_dependency File.dirname(__FILE__) + '/../app/validators/dmsf_url_validator' +require_dependency File.dirname(__FILE__) + '/../app/validators/dmsf_folder_parent_validator' # Plugin's patches require 'redmine_dmsf/patches/projects_helper_patch' diff --git a/test/unit/dmsf_folder_test.rb b/test/unit/dmsf_folder_test.rb index a9067216..c7eaa62b 100644 --- a/test/unit/dmsf_folder_test.rb +++ b/test/unit/dmsf_folder_test.rb @@ -237,4 +237,20 @@ class DmsfFolderTest < RedmineDmsf::Test::UnitTest assert DmsfFolder.find_by(project_id: @project2.id, title: @folder1.title) end + def test_valid_parent + @folder2.dmsf_folder = @folder1 + assert @folder2.save + end + + def test_valid_parent_nil + @folder2.dmsf_folder = nil + assert @folder2.save + end + + def test_valid_parent_loop + @folder1.dmsf_folder = @folder2 + # @folder2 is under @folder1 => loop! + assert !@folder1.save + end + end \ No newline at end of file