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

Change association between LifeCycle and Project to a has_many through.

Dombi Attila52a2fb1e6073
diff --git a/app/models/project.rb b/app/models/project.rb
index 8d028fb72e7..0ce2162dcdd 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -86,7 +86,8 @@ class Project < ApplicationRecord
has_many :notification_settings, dependent: :destroy
has_many :project_storages, dependent: :destroy, class_name: "Storages::ProjectStorage"
has_many :storages, through: :project_storages
- has_many :life_cycles, class_name: "Projects::LifeCycle", dependent: :destroy
+ has_many :project_life_cycles, dependent: :destroy
+ has_many :life_cycles, through: :project_life_cycles
store_attribute :settings, :deactivate_work_package_attachments, :boolean
diff --git a/app/models/project_life_cycle.rb b/app/models/project_life_cycle.rb
new file mode 100644
index 00000000000..a6068259a2f
--- /dev/null
+++ b/app/models/project_life_cycle.rb
@@ -0,0 +1,32 @@
+#-- 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 ProjectLifeCycle < ApplicationRecord
+ belongs_to :project, optional: false
+ belongs_to :life_cycle, class_name: "Projects::LifeCycle", optional: false
+end
diff --git a/app/models/projects/life_cycle.rb b/app/models/projects/life_cycle.rb
index f3c4e5d1bab..65ab71d48b4 100644
--- a/app/models/projects/life_cycle.rb
+++ b/app/models/projects/life_cycle.rb
@@ -27,8 +27,12 @@
#++
class Projects::LifeCycle < ApplicationRecord
- belongs_to :project
+ self.table_name = :life_cycles
+
+ 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 }
diff --git a/db/migrate/20241030154245_create_projects_life_cycles.rb b/db/migrate/20241030154245_create_life_cycles.rb
similarity index 90%
rename from db/migrate/20241030154245_create_projects_life_cycles.rb
rename to db/migrate/20241030154245_create_life_cycles.rb
index acb220dfbbe..083794cdbfe 100644
--- a/db/migrate/20241030154245_create_projects_life_cycles.rb
+++ b/db/migrate/20241030154245_create_life_cycles.rb
@@ -26,16 +26,15 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class CreateProjectsLifeCycles < ActiveRecord::Migration[7.1]
+class CreateLifeCycles < ActiveRecord::Migration[7.1]
def change
- create_table :projects_life_cycles do |t|
+ 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.references :project, foreign_key: true
t.timestamps
end
diff --git a/db/migrate/20241101124648_add_life_cycle_to_work_packages.rb b/db/migrate/20241101124648_add_life_cycle_to_work_packages.rb
index 74dece8486e..f0c307f5cef 100644
--- a/db/migrate/20241101124648_add_life_cycle_to_work_packages.rb
+++ b/db/migrate/20241101124648_add_life_cycle_to_work_packages.rb
@@ -1,5 +1,5 @@
class AddLifeCycleToWorkPackages < ActiveRecord::Migration[7.1]
def change
- add_reference :work_packages, :life_cycle, foreign_key: { to_table: :projects_life_cycles }, null: true
+ 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
new file mode 100644
index 00000000000..397a1f32636
--- /dev/null
+++ b/db/migrate/20241105151352_create_project_life_cycles.rb
@@ -0,0 +1,10 @@
+class CreateProjectLifeCycles < ActiveRecord::Migration[7.1]
+ def change
+ create_table :project_life_cycles do |t|
+ t.references :project, foreign_key: true
+ t.references :life_cycle, foreign_key: true
+
+ t.timestamps
+ end
+ end
+end
diff --git a/spec/factories/project_life_cycle_factory.rb b/spec/factories/project_life_cycle_factory.rb
index c822999a3b6..97a24948794 100644
--- a/spec/factories/project_life_cycle_factory.rb
+++ b/spec/factories/project_life_cycle_factory.rb
@@ -27,14 +27,18 @@
#++
FactoryBot.define do
- 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 :life_cycle, class: "Projects::LifeCycle" do
+ color
+
+ 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 }
+ factory :project_gate, class: "::Projects::Gate" do
+ sequence(:name) { |n| "Gate No. #{n}" }
+ date { Date.current + 2.days }
+ end
end
end
diff --git a/spec/models/project_life_cycle_spec.rb b/spec/models/project_life_cycle_spec.rb
new file mode 100644
index 00000000000..a0a826f6e0a
--- /dev/null
+++ b/spec/models/project_life_cycle_spec.rb
@@ -0,0 +1,6 @@
+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) }
+end
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index a5ff8bc6452..e168340094a 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -382,7 +382,14 @@ RSpec.describe Project do
end
describe "life_cycles" do
- it { is_expected.to have_many(:life_cycles).class_name("Projects::LifeCycle").dependent(:destroy) }
+ it { is_expected.to have_many(:project_life_cycles).dependent(:destroy) }
+
+ it do
+ expect(subject)
+ .to have_many(:life_cycles)
+ .through(:project_life_cycles)
+ .class_name("Projects::LifeCycle")
+ end
end
describe "#enabled_module_names=", with_settings: { default_projects_modules: %w(work_package_tracking repository) } do
diff --git a/spec/models/work_package_spec.rb b/spec/models/work_package_spec.rb
index 08523150002..7c247ab052a 100644
--- a/spec/models/work_package_spec.rb
+++ b/spec/models/work_package_spec.rb
@@ -70,7 +70,7 @@ RSpec.describe WorkPackage do
it { is_expected.to belong_to(:assigned_to).class_name("Principal").optional }
it { is_expected.to belong_to(:responsible).class_name("Principal").optional }
it { is_expected.to belong_to(:version).optional }
- it { is_expected.to belong_to(:life_cycle).class_name("Projects::LifeCycle").optional }
+ it { is_expected.to belong_to(:life_cycle).optional }
it { is_expected.to belong_to(:priority).class_name("IssuePriority") }
it { is_expected.to belong_to(:category).optional }
it { is_expected.to have_many(:time_entries).dependent(:delete_all) }
diff --git a/spec/support/shared/projects_life_cycle_helpers.rb b/spec/support/shared/projects_life_cycle_helpers.rb
index 71407a3792b..94336fabfcf 100644
--- a/spec/support/shared/projects_life_cycle_helpers.rb
+++ b/spec/support/shared/projects_life_cycle_helpers.rb
@@ -32,7 +32,9 @@ RSpec.shared_examples_for "a Projects::LifeCycle event" do
end
describe "validations" do
- it { is_expected.to belong_to(:project) }
+ 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 { is_expected.to have_many(:work_packages) }
it { is_expected.to validate_presence_of(:name) }