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

Add Gate and Stage base sti classes to allow definition of Stage and Gate templates in an OP instance.

Dombi Attila60c07be64d6f
diff --git a/app/models/gate.rb b/app/models/gate.rb
new file mode 100644
index 00000000000..3eee742ec91
--- /dev/null
+++ b/app/models/gate.rb
@@ -0,0 +1,30 @@
+#-- 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.
+#++
+
+class Gate < LifeCycle
+end
diff --git a/app/models/life_cycle.rb b/app/models/life_cycle.rb
index ec88b5ff43c..56176327bd1 100644
--- a/app/models/life_cycle.rb
+++ b/app/models/life_cycle.rb
@@ -33,4 +33,15 @@ class LifeCycle < ApplicationRecord
belongs_to :color, optional: false
validates :name, presence: true
+ validates :type, inclusion: { in: %w[Stage Gate], message: :must_be_a_stage_or_gate }
+
+ def initialize(*args)
+ if instance_of? LifeCycle
+ # Do not allow directly instantiating this class
+ raise NotImplementedError, "Cannot instantiate the base LifeCycle class directly. " \
+ "Use Stage or Gate instead."
+ end
+
+ super
+ end
end
diff --git a/app/models/project/life_cycle.rb b/app/models/project/life_cycle.rb
index d0dbfbd2423..91a678dbe29 100644
--- a/app/models/project/life_cycle.rb
+++ b/app/models/project/life_cycle.rb
@@ -28,9 +28,11 @@
class Project::LifeCycle < ApplicationRecord
belongs_to :project, optional: false
- belongs_to :life_cycle, optional: false
+ belongs_to :life_cycle, optional: false, class_name: "::LifeCycle"
has_many :work_packages, inverse_of: :project_life_cycle, dependent: :nullify
+ attr_readonly :life_cycle_id
+
validates :type, inclusion: { in: %w[Project::Stage Project::Gate], message: :must_be_a_stage_or_gate }
def initialize(*args)
diff --git a/app/models/stage.rb b/app/models/stage.rb
new file mode 100644
index 00000000000..e235c9c450a
--- /dev/null
+++ b/app/models/stage.rb
@@ -0,0 +1,30 @@
+#-- 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.
+#++
+
+class Stage < LifeCycle
+end
diff --git a/config/initializers/new_framework_defaults_7_1.rb b/config/initializers/new_framework_defaults_7_1.rb
index e39bd17a210..cda7419b65d 100644
--- a/config/initializers/new_framework_defaults_7_1.rb
+++ b/config/initializers/new_framework_defaults_7_1.rb
@@ -167,7 +167,7 @@
# behavior would allow assignment but silently not persist changes to the
# database.
#++
-# Rails.application.config.active_record.raise_on_assign_to_attr_readonly = true
+Rails.application.config.active_record.raise_on_assign_to_attr_readonly = true
###
# Enable validating only parent-related columns for presence when the parent is mandatory.
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 2644d868e04..290f07d11ff 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1071,6 +1071,10 @@ en:
name:
blank: "is mandatory. Please select a name."
not_unique: "is already in use. Please select another name."
+ life_cycle:
+ attributes:
+ type:
+ must_be_a_stage_or_gate: "must be either Stage or Gate"
meeting:
error_conflict: "Unable to save because the meeting was updated by someone else in the meantime. Please reload the page."
notifications:
@@ -1113,7 +1117,7 @@ en:
project/life_cycle:
attributes:
type:
- must_be_a_stage_or_gate: "must be either Stage or Gate"
+ must_be_a_stage_or_gate: "must be either Project::Stage or Project::Gate"
project/gate:
attributes:
base:
diff --git a/db/migrate/20241030154245_create_life_cycles.rb b/db/migrate/20241030154245_create_life_cycles.rb
index becaaed3368..557c7ec8a6f 100644
--- a/db/migrate/20241030154245_create_life_cycles.rb
+++ b/db/migrate/20241030154245_create_life_cycles.rb
@@ -29,6 +29,7 @@
class CreateLifeCycles < ActiveRecord::Migration[7.1]
def change
create_table :life_cycles do |t|
+ t.string :type
t.string :name
t.belongs_to :color, type: :int
diff --git a/spec/factories/life_cycle_factory.rb b/spec/factories/life_cycle_factory.rb
index 749781c4be4..81752900bf8 100644
--- a/spec/factories/life_cycle_factory.rb
+++ b/spec/factories/life_cycle_factory.rb
@@ -28,7 +28,14 @@
FactoryBot.define do
factory :life_cycle do
- sequence(:name) { |n| "Life cycle No. #{n}" }
color
+
+ factory :gate, class: "Gate" do
+ sequence(:name) { |n| "Gate No. #{n}" }
+ end
+
+ factory :stage, class: "Stage" do
+ sequence(:name) { |n| "Stage No. #{n}" }
+ end
end
end
diff --git a/spec/factories/project_life_cycle_factory.rb b/spec/factories/project_life_cycle_factory.rb
index f96a7c160ac..cd5e4a7f874 100644
--- a/spec/factories/project_life_cycle_factory.rb
+++ b/spec/factories/project_life_cycle_factory.rb
@@ -29,14 +29,15 @@
FactoryBot.define do
factory :project_life_cycle, class: "Project::LifeCycle" do
project
- life_cycle
factory :project_stage, class: "Project::Stage" do
+ life_cycle factory: :stage
start_date { Date.current - 2.days }
end_date { Date.current + 2.days }
end
factory :project_gate, class: "Project::Gate" do
+ life_cycle factory: :gate
date { Date.current + 2.days }
end
end
diff --git a/spec/models/gate_spec.rb b/spec/models/gate_spec.rb
new file mode 100644
index 00000000000..bed01deef3f
--- /dev/null
+++ b/spec/models/gate_spec.rb
@@ -0,0 +1,34 @@
+#-- 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 "rails_helper"
+require "support/shared/life_cycle_helpers"
+
+RSpec.describe Gate do
+ it_behaves_like "a LifeCycle event"
+end
diff --git a/spec/models/life_cycle_spec.rb b/spec/models/life_cycle_spec.rb
index 4b7435a021b..a855543e534 100644
--- a/spec/models/life_cycle_spec.rb
+++ b/spec/models/life_cycle_spec.rb
@@ -29,13 +29,20 @@
require "rails_helper"
RSpec.describe LifeCycle do
- describe "associations" do
- it { is_expected.to have_many(:project_life_cycles).dependent(:destroy) }
- it { is_expected.to have_many(:projects).through(:project_life_cycles) }
- it { is_expected.to belong_to(:color).required(true) }
+ it "cannot be instantiated" do
+ expect { described_class.new }.to raise_error(NotImplementedError)
end
- describe "validations" do
- it { is_expected.to validate_presence_of(:name) }
+ it "cannot be instantiated with an invalid type" do
+ expect { described_class.new(type: "InvalidType") }.to raise_error(ActiveRecord::SubclassNotFound)
end
+
+ it "can be instantiated with a valid type" do
+ expect { described_class.new(type: "Gate") }.not_to raise_error
+ end
+
+ # For more specs see:
+ # - spec/support/shared/life_cycle_helpers.rb
+ # - spec/models/gate_spec.rb
+ # - spec/models/stage_spec.rb
end
diff --git a/spec/models/project/life_cycle_spec.rb b/spec/models/project/life_cycle_spec.rb
index 48de1751bf3..ce8dd5c41ec 100644
--- a/spec/models/project/life_cycle_spec.rb
+++ b/spec/models/project/life_cycle_spec.rb
@@ -41,6 +41,28 @@ RSpec.describe Project::LifeCycle do
expect { described_class.new(type: "Project::Gate") }.not_to raise_error
end
+ describe "with an instantiated Gate" do
+ subject { build :project_gate }
+
+ it "allows setting a life_cycle" do
+ expected_life_cycle = create :gate
+ subject.life_cycle = expected_life_cycle
+
+ expect(subject.save).to be(true)
+ expect(subject.reload.life_cycle).to eq(expected_life_cycle)
+ end
+
+ context "when the Gate is already saved" do
+ subject { create :project_gate }
+
+ it "does not allow updating the life_cycle" do
+ expect do
+ subject.life_cycle = build :gate
+ end.to raise_error ActiveRecord::ReadonlyAttributeError
+ end
+ end
+ end
+
# For more specs see:
# - spec/support/shared/project_life_cycle_helpers.rb
# - spec/models/projects/gate_spec.rb
diff --git a/spec/models/stage_spec.rb b/spec/models/stage_spec.rb
new file mode 100644
index 00000000000..750ba837927
--- /dev/null
+++ b/spec/models/stage_spec.rb
@@ -0,0 +1,34 @@
+#-- 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 "rails_helper"
+require "support/shared/life_cycle_helpers"
+
+RSpec.describe Stage do
+ it_behaves_like "a LifeCycle event"
+end
diff --git a/spec/support/shared/life_cycle_helpers.rb b/spec/support/shared/life_cycle_helpers.rb
new file mode 100644
index 00000000000..d0ebdc542b8
--- /dev/null
+++ b/spec/support/shared/life_cycle_helpers.rb
@@ -0,0 +1,50 @@
+#-- 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.
+#++
+
+RSpec.shared_examples_for "a LifeCycle event" do
+ it "inherits from LifeCycle" do
+ expect(described_class < LifeCycle).to be true
+ end
+
+ describe "associations" do
+ it { is_expected.to have_many(:project_life_cycles).dependent(:destroy) }
+ it { is_expected.to have_many(:projects).through(:project_life_cycles) }
+ it { is_expected.to belong_to(:color).required(true) }
+ end
+
+ describe "validations" do
+ it { is_expected.to validate_presence_of(:name) }
+
+ it "is invalid if type is not Stage or Gate" do
+ life_cycle = described_class.new
+ life_cycle.type = "InvalidType"
+ expect(life_cycle).not_to be_valid
+ expect(life_cycle.errors[:type]).to include("must be either Stage or Gate")
+ end
+ end
+end
diff --git a/spec/support/shared/project_life_cycle_helpers.rb b/spec/support/shared/project_life_cycle_helpers.rb
index 2a657268c03..551e6c58a2c 100644
--- a/spec/support/shared/project_life_cycle_helpers.rb
+++ b/spec/support/shared/project_life_cycle_helpers.rb
@@ -42,7 +42,7 @@ RSpec.shared_examples_for "a Project::LifeCycle event" do
life_cycle = described_class.new
life_cycle.type = "InvalidType"
expect(life_cycle).not_to be_valid
- expect(life_cycle.errors[:type]).to include("must be either Stage or Gate")
+ expect(life_cycle.errors[:type]).to include("must be either Project::Stage or Project::Gate")
end
end
end