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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
enforce enumerations to be active if they are default
ulferts30fd5412fc9f
diff --git a/app/components/admin/enumerations/item_form.rb b/app/components/admin/enumerations/item_form.rbindex 8794ce33889..d85507eb470 100644--- a/app/components/admin/enumerations/item_form.rb+++ b/app/components/admin/enumerations/item_form.rb@@ -52,14 +52,18 @@ module Adminform.check_box(name: :active,- label: object.class.human_attribute_name(:active)+ label: object.class.human_attribute_name(:active),+ data: { action: "admin--enumerations#lockstepDefault",+ "admin--enumerations-target": "active" })if object.class.can_have_default_value?form.check_box(name: :is_default,label: I18n.t(:label_default),- caption: I18n.t(:"priorities.admin.default.caption")+ caption: I18n.t(:"priorities.admin.default.caption"),+ data: { action: "admin--enumerations#lockstepActive",+ "admin--enumerations-target": "default" })enddiff --git a/app/models/enumeration.rb b/app/models/enumeration.rbindex a5bbd334320..dda4edc2a39 100644--- a/app/models/enumeration.rb+++ b/app/models/enumeration.rb@@ -44,6 +44,7 @@ class Enumeration < ApplicationRecorduniqueness: { scope: %i(type project_id),case_sensitive: false }validates :name, length: { maximum: 256 }+ validate :default_is_active, if: -> { self.class.can_have_default_value? }scope :shared, -> { where(project_id: nil) }scope :active, -> { where(active: true) }@@ -173,4 +174,10 @@ class Enumeration < ApplicationRecorddef check_integrityraise "Can't delete enumeration" if in_use?end++ def default_is_active+ if is_default? && !active?+ errors.add(:is_default, :active_required)+ end+ endenddiff --git a/app/views/admin/settings/work_package_priorities/edit.html.erb b/app/views/admin/settings/work_package_priorities/edit.html.erbindex b20ef8c7ec9..4d985d73517 100644--- a/app/views/admin/settings/work_package_priorities/edit.html.erb+++ b/app/views/admin/settings/work_package_priorities/edit.html.erb@@ -43,6 +43,8 @@ See COPYRIGHT and LICENSE files for more details.end%>+<% content_controller "admin--enumerations" %>+<%= settings_primer_form_with(model: @enumeration, scope: :enumeration, url: url_for(action: :update, id: @enumeration)) do |f| %><%= render(Admin::Enumerations::ItemForm.new(f)) %><% end %>diff --git a/app/views/admin/settings/work_package_priorities/new.html.erb b/app/views/admin/settings/work_package_priorities/new.html.erbindex 2d38005321b..676527fdbd3 100644--- a/app/views/admin/settings/work_package_priorities/new.html.erb+++ b/app/views/admin/settings/work_package_priorities/new.html.erb@@ -41,6 +41,8 @@ See COPYRIGHT and LICENSE files for more details.end%>+<% content_controller "admin--enumerations" %>+<%= settings_primer_form_with(model: @enumeration, scope: :enumeration, url: url_for(action: :create)) do |f| %><%= render(Admin::Enumerations::ItemForm.new(f)) %><% end %>diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 85b39faac3d..b3b5c6b2577 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -1463,6 +1463,10 @@ en:only_one_trial: "Only one trial token can be active. Please delete the previous trial token before adding another."unreadable: "can't be read. Are you sure it is a support token?"already_added: "This token has already been added."+ enumeration:+ attributes:+ active:+ active_required: "needs the 'Active' property to be true."grids/grid:overlaps: "overlap."outside: "is outside of the grid."diff --git a/frontend/src/stimulus/controllers/dynamic/admin/enumerations.controller.ts b/frontend/src/stimulus/controllers/dynamic/admin/enumerations.controller.tsnew file mode 100644index 00000000000..e996ebf878e--- /dev/null+++ b/frontend/src/stimulus/controllers/dynamic/admin/enumerations.controller.ts@@ -0,0 +1,55 @@+/*+ * -- 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.+ * +++ */++import { Controller } from '@hotwired/stimulus';++export default class EnumerationsController extends Controller {+ static targets = [+ 'active',+ 'default',+ ];++ declare readonly activeTarget:HTMLInputElement;+ declare readonly defaultTarget:HTMLInputElement;++ declare readonly hasDefaultTarget:boolean;++ public lockstepActive() {+ if (this.defaultTarget.checked) {+ this.activeTarget.checked = true;+ }+ }++ public lockstepDefault() {+ if (!this.activeTarget.checked && this.hasDefaultTarget) {+ this.defaultTarget.checked = false;+ }+ }+}diff --git a/modules/costs/app/views/admin/settings/time_entry_activities/edit.html.erb b/modules/costs/app/views/admin/settings/time_entry_activities/edit.html.erbindex cf7880ed9f1..dce899b88a8 100644--- a/modules/costs/app/views/admin/settings/time_entry_activities/edit.html.erb+++ b/modules/costs/app/views/admin/settings/time_entry_activities/edit.html.erb@@ -43,6 +43,8 @@ See COPYRIGHT and LICENSE files for more details.end%>+<% content_controller "admin--enumerations" %>+<%= settings_primer_form_with(model: @enumeration, scope: :enumeration, url: url_for(action: :update, id: @enumeration)) do |f| %><%= render(Admin::Enumerations::ItemForm.new(f)) %><% end %>diff --git a/modules/costs/app/views/admin/settings/time_entry_activities/new.html.erb b/modules/costs/app/views/admin/settings/time_entry_activities/new.html.erbindex a2b119b18fc..8c77861380a 100644--- a/modules/costs/app/views/admin/settings/time_entry_activities/new.html.erb+++ b/modules/costs/app/views/admin/settings/time_entry_activities/new.html.erb@@ -43,6 +43,8 @@ See COPYRIGHT and LICENSE files for more details.end%>+<% content_controller "admin--enumerations" %>+<%= primer_form_with(model: @enumeration, scope: :enumeration, url: url_for(action: :create)) do |f| %><%= render(Admin::Enumerations::ItemForm.new(f)) %><% end %>diff --git a/modules/costs/spec/models/time_entry_activity_spec.rb b/modules/costs/spec/models/time_entry_activity_spec.rbindex 458f16bf282..57e8010df1a 100644--- a/modules/costs/spec/models/time_entry_activity_spec.rb+++ b/modules/costs/spec/models/time_entry_activity_spec.rb@@ -28,6 +28,8 @@require "spec_helper"+require Rails.root.join("spec/models/enumerations/shared_enumeration_examples").to_s+RSpec.describe TimeEntryActivity dolet(:new_activity) { described_class.new }let(:saved_activity) { described_class.create name: "Design" }@@ -52,4 +54,8 @@ RSpec.describe TimeEntryActivity do.to eq :enumeration_activitiesendend++ it_behaves_like "enumeration validation", false do+ let(:enumeration) { build_stubbed(:time_entry_activity) }+ endenddiff --git a/modules/documents/app/views/admin/settings/document_categories/edit.html.erb b/modules/documents/app/views/admin/settings/document_categories/edit.html.erbindex 4340ede5e0e..922c90cf315 100644--- a/modules/documents/app/views/admin/settings/document_categories/edit.html.erb+++ b/modules/documents/app/views/admin/settings/document_categories/edit.html.erb@@ -42,6 +42,8 @@ See COPYRIGHT and LICENSE files for more details.end%>+<% content_controller "admin--enumerations" %>+<%= settings_primer_form_with(model: @enumeration, scope: :enumeration, url: url_for(action: :update, id: @enumeration)) do |f| %><%= render(Admin::Enumerations::ItemForm.new(f)) %><% end %>diff --git a/modules/documents/app/views/admin/settings/document_categories/new.html.erb b/modules/documents/app/views/admin/settings/document_categories/new.html.erbindex ff084d218ec..e23cc512ff0 100644--- a/modules/documents/app/views/admin/settings/document_categories/new.html.erb+++ b/modules/documents/app/views/admin/settings/document_categories/new.html.erb@@ -41,6 +41,8 @@ See COPYRIGHT and LICENSE files for more details.end%>+<% content_controller "admin--enumerations" %>+<%= primer_form_with(model: @enumeration, scope: :enumeration, url: url_for(action: :create)) do |f| %><%= render(Admin::Enumerations::ItemForm.new(f)) %><% end %>diff --git a/modules/documents/spec/models/document_category_spec.rb b/modules/documents/spec/models/document_category_spec.rbindex 8d618e1d0d6..bedf1cb7e2b 100644--- a/modules/documents/spec/models/document_category_spec.rb+++ b/modules/documents/spec/models/document_category_spec.rb@@ -29,6 +29,8 @@#++require_relative "../spec_helper"+require Rails.root.join("spec/models/enumerations/shared_enumeration_examples").to_s+RSpec.describe DocumentCategory dolet(:project) { create(:project) }@@ -58,4 +60,8 @@ RSpec.describe DocumentCategory doold_default.reloadend.to change { old_default.is_default? }.from(true).to(false)end++ it_behaves_like "enumeration validation", true do+ let(:enumeration) { build_stubbed(:document_category) }+ endenddiff --git a/spec/models/enumerations/shared_enumeration_examples.rb b/spec/models/enumerations/shared_enumeration_examples.rbnew file mode 100644index 00000000000..28c9bb0b670--- /dev/null+++ b/spec/models/enumerations/shared_enumeration_examples.rb@@ -0,0 +1,53 @@+# 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"++RSpec.shared_context "enumeration validation" do |default_supported| # rubocop:disable RSpec/ContextWording+ let(:enumeration) do+ super()+ rescue NoMethodError+ raise "'enumeration' let needs to be set"+ end++ describe "#valid?" do+ if default_supported+ context "with the enumeration being inactive and default" do+ it "is invalid", :aggregate_failures do+ enumeration.active = false+ enumeration.is_default = true++ expect(enumeration.valid?).to be false+ expect(enumeration.errors.symbols_for(:is_default)).to contain_exactly(:active_required)+ end+ end+ end+ end+enddiff --git a/spec/models/issue_priority_spec.rb b/spec/models/issue_priority_spec.rbindex a53064c24fd..9c00131ae5f 100644--- a/spec/models/issue_priority_spec.rb+++ b/spec/models/issue_priority_spec.rb@@ -29,12 +29,18 @@#++require "spec_helper"+require_relative "enumerations/shared_enumeration_examples"+RSpec.describe IssuePriority doshared_let(:priority) { create(:priority) }shared_let(:default_priority) { create(:default_priority) }let(:stubbed_priority) { build_stubbed(:priority) }+ it_behaves_like "enumeration validation", true do+ let(:enumeration) { build_stubbed(:priority) }+ end+describe ".ancestors" doit "is an enumeration" doexpect(IssuePriority.ancestors)