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

custom field delete service and contract (error with links if referenced in calculated value fields)

Ivan Kuchin870658c82b37
diff --git a/app/contracts/custom_fields/delete_contract.rb b/app/contracts/custom_fields/delete_contract.rb
new file mode 100644
index 00000000000..bf483055e13
--- /dev/null
+++ b/app/contracts/custom_fields/delete_contract.rb
@@ -0,0 +1,67 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+module CustomFields
+ class DeleteContract < BaseContract
+ class ReferencedInOtherFieldsHtmlError
+ include Rails.application.routes.url_helpers
+ include ActionView::Helpers::OutputSafetyHelper
+ include ActionView::Helpers::TranslationHelper
+ include ActionView::Helpers::UrlHelper
+
+ def message(referenced, referencing)
+ links = referencing.map do |cf|
+ url = admin_settings_project_custom_field_path(cf)
+ link_to(cf.name, url)
+ end
+
+ t(
+ "activerecord.errors.models.custom_field.referenced_in_other_fields_html",
+ name: referenced.name,
+ links: to_sentence(links),
+ count: links.length
+ )
+ end
+ end
+
+ validate :not_referenced
+
+ def not_referenced
+ referencing = model.class.with_formula_referencing(model)
+ return if referencing.empty?
+
+ errors.add(
+ :base,
+ :referenced_in_other_fields,
+ message: ReferencedInOtherFieldsHtmlError.new.message(model, referencing)
+ )
+ end
+ end
+end
diff --git a/app/controllers/admin/settings/project_custom_fields_controller.rb b/app/controllers/admin/settings/project_custom_fields_controller.rb
index e6d25ed0066..db9896fc9b0 100644
--- a/app/controllers/admin/settings/project_custom_fields_controller.rb
+++ b/app/controllers/admin/settings/project_custom_fields_controller.rb
@@ -147,9 +147,15 @@ module Admin::Settings
end
def destroy
- @custom_field.destroy
+ result = CustomFields::DeleteService.new(user: current_user, model: @custom_field).call
- update_section_via_turbo_stream(project_custom_field_section: @custom_field.project_custom_field_section.reload)
+ if result.success?
+ update_section_via_turbo_stream(project_custom_field_section: @custom_field.project_custom_field_section.reload)
+ else
+ render_error_flash_message_via_turbo_stream(
+ message: join_flash_messages(result.errors)
+ )
+ end
respond_with_turbo_streams
end
diff --git a/app/services/custom_fields/delete_service.rb b/app/services/custom_fields/delete_service.rb
new file mode 100644
index 00000000000..0ed24b14bce
--- /dev/null
+++ b/app/services/custom_fields/delete_service.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+module CustomFields
+ class DeleteService < ::BaseServices::Delete
+ end
+end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 0521d10694b..56af67b156e 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1412,6 +1412,9 @@ en:
minimum: "need to include at least one filter for principal, context or id with the '=' operator."
custom_field:
at_least_one_custom_option: "At least one option needs to be available."
+ referenced_in_other_fields_html:
+ one: "%{name} is used in project attribute calculation %{links}."
+ other: "%{name} is used in project attribute calculations: %{links}."
custom_fields_project:
attributes:
project_ids:
diff --git a/spec/contracts/custom_fields/delete_contract_spec.rb b/spec/contracts/custom_fields/delete_contract_spec.rb
new file mode 100644
index 00000000000..65a5a92abd3
--- /dev/null
+++ b/spec/contracts/custom_fields/delete_contract_spec.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+require "contracts/shared/model_contract_shared_context"
+
+RSpec.describe CustomFields::DeleteContract do
+ include_context "ModelContract shared context"
+
+ let(:cf) { build_stubbed(:project_custom_field) }
+ let(:contract) do
+ described_class.new(cf, current_user, options: {})
+ end
+
+ it_behaves_like "contract is valid for active admins and invalid for regular users"
+
+ describe "checks that field is not referenced" do
+ let(:current_user) { build_stubbed(:admin) }
+
+ before do
+ allow(ProjectCustomField).to receive(:with_formula_referencing).with(cf).and_return(referencing)
+ end
+
+ context "when the custom field is not referenced" do
+ let(:referencing) { [] }
+
+ include_examples "contract is valid"
+ end
+
+ context "when the custom field is referenced" do
+ let(:referencing) { [build_stubbed(:project_custom_field)] }
+
+ include_examples "contract is invalid", base: :referenced_in_other_fields
+ end
+ end
+end
diff --git a/spec/features/admin/custom_fields/projects/index_spec.rb b/spec/features/admin/custom_fields/projects/index_spec.rb
index 8d17a448e5c..72813f2d9cd 100644
--- a/spec/features/admin/custom_fields/projects/index_spec.rb
+++ b/spec/features/admin/custom_fields/projects/index_spec.rb
@@ -265,14 +265,70 @@ RSpec.describe "List project custom fields", :js do
end
end
- it "allows to delete a custom field" do
- within_project_custom_field_menu(boolean_project_custom_field) do
- accept_confirm do
- click_on("Delete")
+ describe "deleting custom fields" do
+ it "allows to delete a custom field" do
+ within_project_custom_field_menu(boolean_project_custom_field) do
+ accept_confirm do
+ click_on("Delete")
+ end
+ end
+
+ expect(page).to have_no_css("[data-test-selector='project-custom-field-container-#{boolean_project_custom_field.id}']")
+ end
+
+ it "prevents deletion of custom field used in a calculated value custom fields" do
+ within_project_custom_field_menu(integer_project_custom_field) do
+ accept_confirm do
+ click_on("Delete")
+ end
+ end
+ page.within(find_flash_element(type: :error)) do
+ expect(page).to have_cf_admin_link(calculated_from_int_project_custom_field)
+ expect(page).to have_cf_admin_link(calculated_from_int_and_float_project_custom_field)
end
+ expect_and_dismiss_flash(
+ message: "Integer field is used in project attribute calculations: Calculated field using int and " \
+ "Calculated field using int and float.",
+ type: :error
+ )
+ expect(page).to have_css("[data-test-selector='project-custom-field-container-#{integer_project_custom_field.id}']")
+
+ within_project_custom_field_menu(float_project_custom_field) do
+ accept_confirm do
+ click_on("Delete")
+ end
+ end
+ page.within(find_flash_element(type: :error)) do
+ expect(page).to have_cf_admin_link(calculated_from_int_and_float_project_custom_field)
+ end
+ expect_and_dismiss_flash(
+ message: "Float field is used in project attribute calculation Calculated field using int and float.",
+ type: :error
+ )
+ expect(page).to have_css("[data-test-selector='project-custom-field-container-#{float_project_custom_field.id}']")
+
+ # Can delete calculated value field
+ within_project_custom_field_menu(calculated_from_int_and_float_project_custom_field) do
+ accept_confirm do
+ click_on("Delete")
+ end
+ end
+ expect(page).to have_no_css(
+ "[data-test-selector='project-custom-field-container-#{calculated_from_int_and_float_project_custom_field.id}']"
+ )
+
+ # Can delete used custom field afterwards
+ within_project_custom_field_menu(float_project_custom_field) do
+ accept_confirm do
+ click_on("Delete")
+ end
+ end
+ expect(page).to have_no_css("[data-test-selector='project-custom-field-container-#{float_project_custom_field.id}']")
end
- expect(page).to have_no_css("[data-test-selector='project-custom-field-container-#{boolean_project_custom_field.id}']")
+ def have_cf_admin_link(custom_field)
+ have_link(custom_field.name, href: admin_settings_project_custom_field_path(custom_field))
+ end
end
it "redirects to the custom field edit page via menu item" do
diff --git a/spec/features/admin/custom_fields/projects/shared_context.rb b/spec/features/admin/custom_fields/projects/shared_context.rb
index 96d9f3a025d..4b87040129e 100644
--- a/spec/features/admin/custom_fields/projects/shared_context.rb
+++ b/spec/features/admin/custom_fields/projects/shared_context.rb
@@ -29,6 +29,8 @@
#++
RSpec.shared_context "with seeded project custom fields" do
+ using CustomFieldFormulaReferencing
+
shared_let(:admin) { create(:admin) }
shared_let(:non_admin) { create(:user) }
@@ -66,6 +68,26 @@ RSpec.shared_context "with seeded project custom fields" do
project_custom_field_section: section_for_input_fields)
end
+ let!(:calculated_from_int_project_custom_field) do
+ create(
+ :calculated_value_project_custom_field,
+ :skip_validations,
+ name: "Calculated field using int",
+ formula: "#{integer_project_custom_field} * 2",
+ project_custom_field_section: section_for_input_fields
+ )
+ end
+
+ let!(:calculated_from_int_and_float_project_custom_field) do
+ create(
+ :calculated_value_project_custom_field,
+ :skip_validations,
+ name: "Calculated field using int and float",
+ formula: "#{float_project_custom_field} * #{integer_project_custom_field}",
+ project_custom_field_section: section_for_input_fields
+ )
+ end
+
let!(:list_project_custom_field) do
create(:list_project_custom_field, name: "List field",
project_custom_field_section: section_for_select_fields,