Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Make Grids::DeleteContract base on the global delete contract (#9316)

Oliver Güntherefe4bf3289da
diff --git a/modules/grids/app/contracts/grids/delete_contract.rb b/modules/grids/app/contracts/grids/delete_contract.rb
index cf6f4d4b52e..4e14e59693f 100644
--- a/modules/grids/app/contracts/grids/delete_contract.rb
+++ b/modules/grids/app/contracts/grids/delete_contract.rb
@@ -31,22 +31,7 @@
require 'grids/base_contract'
module Grids
- class DeleteContract < BaseContract
- validate :validate_delete_allowed
-
- ##
- # Check whether this grid can be deleted.
- # The base contract already checks whether we can manage it.
- def validate_delete_allowed
- unless model.user_deletable?
- errors.add(:scope, :unremovable)
- end
- end
-
- protected
-
- def validate_model?
- false
- end
+ class DeleteContract < ::DeleteContract
+ delete_permission -> { model.user_deletable? && Grids::Configuration.writable?(model, user) }
end
end
diff --git a/modules/grids/spec/contracts/grids/delete_contract_spec.rb b/modules/grids/spec/contracts/grids/delete_contract_spec.rb
new file mode 100644
index 00000000000..1fa207b3c42
--- /dev/null
+++ b/modules/grids/spec/contracts/grids/delete_contract_spec.rb
@@ -0,0 +1,75 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# 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.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+describe Grids::DeleteContract do
+ let(:user) { FactoryBot.build_stubbed(:user) }
+ let(:grid) do
+ FactoryBot.build_stubbed(:grid)
+ end
+
+ let(:instance) { described_class.new(grid, user) }
+
+ before do
+ allow(Grids::Configuration)
+ .to receive(:writable?)
+ .and_return(writable)
+
+ allow(grid).to receive(:user_deletable?).and_return(user_deletable)
+ end
+
+ context 'when writable' do
+ let(:writable) { true }
+ let(:user_deletable) { true }
+
+ it 'deletes the grid even if no valid widgets' do
+ expect(instance.validate).to be_truthy
+ end
+ end
+
+ context 'when not writable' do
+ let(:writable) { false }
+ let(:user_deletable) { true }
+
+ it 'deletes the grid even if not valid' do
+ expect(instance.validate).to be_falsey
+ end
+ end
+
+ context 'when not deletable' do
+ let(:writable) { true }
+ let(:user_deletable) { false }
+
+ it 'deletes the grid even if not valid' do
+ expect(instance.validate).to be_falsey
+ end
+ end
+end