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

Separate LifeCycle from ProjectLifeCycle to allow LifeCycles to be shared across projects.

Dombi Attila4fbdd4bae469
diff --git a/app/models/projects/life_cycle.rb b/app/models/life_cycle.rb
similarity index 74%
rename from app/models/projects/life_cycle.rb
rename to app/models/life_cycle.rb
index 65ab71d48b4..5744a30b914 100644
--- a/app/models/projects/life_cycle.rb
+++ b/app/models/life_cycle.rb
@@ -26,23 +26,11 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Projects::LifeCycle < ApplicationRecord
- self.table_name = :life_cycles
-
+class LifeCycle < ApplicationRecord
has_many :project_life_cycles, dependent: :destroy
has_many :projects, through: :project_life_cycles
- has_many :work_packages, dependent: :nullify
+
belongs_to :color, optional: false
validates :name, presence: true
- validates :type, inclusion: { in: %w[Projects::Stage Projects::Gate], message: :must_be_a_stage_or_gate }
-
- def initialize(*args)
- if instance_of? Projects::LifeCycle
- # Do not allow directly instantiating this class
- raise NotImplementedError, "Cannot instantiate the base Projects::LifeCycle class directly."
- end
-
- super
- end
end
diff --git a/app/models/project_life_cycle.rb b/app/models/project_life_cycle.rb
index a6068259a2f..74fa6df3f00 100644
--- a/app/models/project_life_cycle.rb
+++ b/app/models/project_life_cycle.rb
@@ -28,5 +28,18 @@
class ProjectLifeCycle < ApplicationRecord
belongs_to :project, optional: false
- belongs_to :life_cycle, class_name: "Projects::LifeCycle", optional: false
+ belongs_to :life_cycle, optional: false
+ has_many :work_packages, dependent: :nullify
+
+ validates :type, inclusion: { in: %w[Projects::Stage Projects::Gate], message: :must_be_a_stage_or_gate }
+
+ def initialize(*args)
+ if instance_of? ProjectLifeCycle
+ # Do not allow directly instantiating this class
+ raise NotImplementedError, "Cannot instantiate the base ProjectLifeCycle class directly. " \
+ "Use Projects::Stage or Projects::Gate instead."
+ end
+
+ super
+ end
end
diff --git a/app/models/projects/gate.rb b/app/models/projects/gate.rb
index 41543f85b86..9559b4c115c 100644
--- a/app/models/projects/gate.rb
+++ b/app/models/projects/gate.rb
@@ -26,7 +26,7 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Projects::Gate < Projects::LifeCycle
+class Projects::Gate < ProjectLifeCycle
validates :date, presence: true
validate :validate_restricted_attributes
diff --git a/app/models/projects/stage.rb b/app/models/projects/stage.rb
index 1081e3a9f3f..0532653795c 100644
--- a/app/models/projects/stage.rb
+++ b/app/models/projects/stage.rb
@@ -26,7 +26,7 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Projects::Stage < Projects::LifeCycle
+class Projects::Stage < ProjectLifeCycle
validates :start_date, :end_date, presence: true
validate :validate_restricted_attributes
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index 1d4437d1b58..9f5c2f2c046 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -54,7 +54,7 @@ class WorkPackage < ApplicationRecord
belongs_to :assigned_to, class_name: "Principal", optional: true
belongs_to :responsible, class_name: "Principal", optional: true
belongs_to :version, optional: true
- belongs_to :life_cycle, class_name: "Projects::LifeCycle", optional: true
+ belongs_to :project_life_cycle, optional: true
belongs_to :priority, class_name: "IssuePriority"
belongs_to :category, class_name: "Category", optional: true
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 3f7793e5a28..c02f52111cd 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1106,22 +1106,22 @@ en:
enabled_modules:
dependency_missing: "The module '%{dependency}' needs to be enabled as well since the module '%{module}' depends on it."
format: "%{message}"
- projects/gate:
+ project_custom_field_project_mapping:
attributes:
- base:
- date_range_not_allowed: "Cannot assign `start_date` or `end_date` to a Gate"
- projects/life_cycle:
+ project_ids:
+ blank: "Please select a project."
+ project_life_cycle:
attributes:
type:
must_be_a_stage_or_gate: "must be either Stage or Gate"
+ projects/gate:
+ attributes:
+ base:
+ date_range_not_allowed: "Cannot assign `start_date` or `end_date` to a Gate"
projects/stage:
attributes:
base:
date_not_allowed: "Cannot assign `date` to a Stage"
- project_custom_field_project_mapping:
- attributes:
- project_ids:
- blank: "Please select a project."
query:
attributes:
project:
diff --git a/db/migrate/20241030154245_create_life_cycles.rb b/db/migrate/20241030154245_create_life_cycles.rb
index 083794cdbfe..becaaed3368 100644
--- a/db/migrate/20241030154245_create_life_cycles.rb
+++ b/db/migrate/20241030154245_create_life_cycles.rb
@@ -29,11 +29,7 @@
class CreateLifeCycles < ActiveRecord::Migration[7.1]
def change
create_table :life_cycles do |t|
- t.string :type
t.string :name
- t.date :start_date
- t.date :end_date
- t.date :date
t.belongs_to :color, type: :int
t.timestamps
diff --git a/db/migrate/20241101124648_add_life_cycle_to_work_packages.rb b/db/migrate/20241101124648_add_life_cycle_to_work_packages.rb
deleted file mode 100644
index f0c307f5cef..00000000000
--- a/db/migrate/20241101124648_add_life_cycle_to_work_packages.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class AddLifeCycleToWorkPackages < ActiveRecord::Migration[7.1]
- def change
- add_reference :work_packages, :life_cycle, foreign_key: { to_table: :life_cycles }, null: true
- end
-end
diff --git a/db/migrate/20241105151352_create_project_life_cycles.rb b/db/migrate/20241105151352_create_project_life_cycles.rb
index 397a1f32636..e5576093b07 100644
--- a/db/migrate/20241105151352_create_project_life_cycles.rb
+++ b/db/migrate/20241105151352_create_project_life_cycles.rb
@@ -1,6 +1,10 @@
class CreateProjectLifeCycles < ActiveRecord::Migration[7.1]
def change
create_table :project_life_cycles do |t|
+ t.string :type
+ t.date :start_date
+ t.date :end_date
+ t.date :date
t.references :project, foreign_key: true
t.references :life_cycle, foreign_key: true
diff --git a/db/migrate/20241105175000_add_life_cycle_to_work_packages.rb b/db/migrate/20241105175000_add_life_cycle_to_work_packages.rb
new file mode 100644
index 00000000000..13aaf9d8ab4
--- /dev/null
+++ b/db/migrate/20241105175000_add_life_cycle_to_work_packages.rb
@@ -0,0 +1,8 @@
+class AddLifeCycleToWorkPackages < ActiveRecord::Migration[7.1]
+ def change
+ add_reference :work_packages,
+ :project_life_cycle,
+ foreign_key: { to_table: :project_life_cycles },
+ null: true
+ end
+end
diff --git a/spec/factories/life_cycle_factory.rb b/spec/factories/life_cycle_factory.rb
new file mode 100644
index 00000000000..749781c4be4
--- /dev/null
+++ b/spec/factories/life_cycle_factory.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.
+#++
+
+FactoryBot.define do
+ factory :life_cycle do
+ sequence(:name) { |n| "Life cycle No. #{n}" }
+ color
+ end
+end
diff --git a/spec/factories/project_life_cycle_factory.rb b/spec/factories/project_life_cycle_factory.rb
index 97a24948794..abe1ce59cf6 100644
--- a/spec/factories/project_life_cycle_factory.rb
+++ b/spec/factories/project_life_cycle_factory.rb
@@ -27,17 +27,16 @@
#++
FactoryBot.define do
- factory :life_cycle, class: "Projects::LifeCycle" do
- color
+ factory :project_life_cycle do
+ project
+ life_cycle
factory :project_stage, class: "::Projects::Stage" do
- sequence(:name) { |n| "Stage No. #{n}" }
start_date { Date.current - 2.days }
end_date { Date.current + 2.days }
end
factory :project_gate, class: "::Projects::Gate" do
- sequence(:name) { |n| "Gate No. #{n}" }
date { Date.current + 2.days }
end
end
diff --git a/spec/models/project_life_cycle_spec.rb b/spec/models/project_life_cycle_spec.rb
index a0a826f6e0a..13b636699f9 100644
--- a/spec/models/project_life_cycle_spec.rb
+++ b/spec/models/project_life_cycle_spec.rb
@@ -1,6 +1,48 @@
+#-- 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"
RSpec.describe ProjectLifeCycle do
- it { is_expected.to belong_to(:project).required(true) }
- it { is_expected.to belong_to(:life_cycle).required(true) }
+ it "cannot be instantiated" do
+ expect { described_class.new }.to raise_error(NotImplementedError)
+ end
+
+ 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: "Projects::Gate") }.not_to raise_error
+ end
+
+ # For more specs see:
+ # - spec/support/shared/project_life_cycle_helpers.rb
+ # - spec/models/projects/gate_spec.rb
+ # - spec/models/projects/stage_spec.rb
end
diff --git a/spec/models/projects/gate_spec.rb b/spec/models/projects/gate_spec.rb
index b725c812cf8..333c9b0ee29 100644
--- a/spec/models/projects/gate_spec.rb
+++ b/spec/models/projects/gate_spec.rb
@@ -27,10 +27,10 @@
#++
require "rails_helper"
-require "support/shared/projects_life_cycle_helpers"
+require "support/shared/project_life_cycle_helpers"
RSpec.describe Projects::Gate do
- it_behaves_like "a Projects::LifeCycle event"
+ it_behaves_like "a ProjectLifeCycle event"
describe "validations" do
it { is_expected.to validate_presence_of(:date) }
diff --git a/spec/models/projects/life_cycle_spec.rb b/spec/models/projects/life_cycle_spec.rb
index 354de103985..4b7435a021b 100644
--- a/spec/models/projects/life_cycle_spec.rb
+++ b/spec/models/projects/life_cycle_spec.rb
@@ -28,21 +28,14 @@
require "rails_helper"
-RSpec.describe Projects::LifeCycle do
- it "cannot be instantiated" do
- expect { described_class.new }.to raise_error(NotImplementedError)
+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) }
end
- it "cannot be instantiated with an invalid type" do
- expect { described_class.new(type: "InvalidType") }.to raise_error(ActiveRecord::SubclassNotFound)
+ describe "validations" do
+ it { is_expected.to validate_presence_of(:name) }
end
-
- it "can be instantiated with a valid type" do
- expect { described_class.new(type: "Projects::Gate") }.not_to raise_error
- end
-
- # For more specs see:
- # - spec/support/shared/projects_life_cycle_helpers.rb
- # - spec/models/projects/gate_spec.rb
- # - spec/models/projects/stage_spec.rb
end
diff --git a/spec/models/projects/stage_spec.rb b/spec/models/projects/stage_spec.rb
index 9224917c0df..d3c988a4a4d 100644
--- a/spec/models/projects/stage_spec.rb
+++ b/spec/models/projects/stage_spec.rb
@@ -27,10 +27,10 @@
#++
require "rails_helper"
-require "support/shared/projects_life_cycle_helpers"
+require "support/shared/project_life_cycle_helpers"
RSpec.describe Projects::Stage do
- it_behaves_like "a Projects::LifeCycle event"
+ it_behaves_like "a ProjectLifeCycle event"
describe "validations" do
it { is_expected.to validate_presence_of(:start_date) }
diff --git a/spec/support/shared/projects_life_cycle_helpers.rb b/spec/support/shared/project_life_cycle_helpers.rb
similarity index 78%
rename from spec/support/shared/projects_life_cycle_helpers.rb
rename to spec/support/shared/project_life_cycle_helpers.rb
index 94336fabfcf..9201b7416ec 100644
--- a/spec/support/shared/projects_life_cycle_helpers.rb
+++ b/spec/support/shared/project_life_cycle_helpers.rb
@@ -26,18 +26,18 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-RSpec.shared_examples_for "a Projects::LifeCycle event" do
- it "inherits from Projects::LifeCycle" do
- expect(described_class < Projects::LifeCycle).to be true
+RSpec.shared_examples_for "a ProjectLifeCycle event" do
+ it "inherits from ProjectLifeCycle" do
+ expect(described_class < ProjectLifeCycle).to be true
end
- describe "validations" 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) }
+ describe "associations" do
+ it { is_expected.to belong_to(:project).required(true) }
+ it { is_expected.to belong_to(:life_cycle).required(true) }
it { is_expected.to have_many(:work_packages) }
- it { is_expected.to validate_presence_of(:name) }
+ end
+ describe "validations" do
it "is invalid if type is not Stage or Gate" do
life_cycle = described_class.new
life_cycle.type = "InvalidType"