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

Ensure the Project::LifeCycleStep and Definition type column is readonly and it cannot be changed into an invalid state.

Dombi Attilac48573961c16
diff --git a/app/models/project/gate.rb b/app/models/project/gate.rb
index f83dc6ce756..46ee6fffdb7 100644
--- a/app/models/project/gate.rb
+++ b/app/models/project/gate.rb
@@ -29,6 +29,8 @@
class Project::Gate < Project::LifeCycleStep
alias_attribute :date, :start_date
+ # This ensures the type cannot be changed after initialising the class.
+ validates :type, inclusion: { in: %w[Project::Gate], message: :must_be_a_gate }
validates :date, presence: true
validate :end_date_not_allowed
diff --git a/app/models/project/life_cycle_step.rb b/app/models/project/life_cycle_step.rb
index dc317a69bc8..dfa4b11efda 100644
--- a/app/models/project/life_cycle_step.rb
+++ b/app/models/project/life_cycle_step.rb
@@ -33,7 +33,7 @@ class Project::LifeCycleStep < ApplicationRecord
class_name: "Project::LifeCycleStepDefinition"
has_many :work_packages, inverse_of: :project_life_cycle_step, dependent: :nullify
- attr_readonly :definition_id
+ attr_readonly :definition_id, :type
validates :type, inclusion: { in: %w[Project::Stage Project::Gate], message: :must_be_a_stage_or_gate }
diff --git a/app/models/project/life_cycle_step_definition.rb b/app/models/project/life_cycle_step_definition.rb
index 767483d8fce..d511cc284cc 100644
--- a/app/models/project/life_cycle_step_definition.rb
+++ b/app/models/project/life_cycle_step_definition.rb
@@ -38,6 +38,8 @@ class Project::LifeCycleStepDefinition < ApplicationRecord
validates :name, presence: true
validates :type, inclusion: { in: %w[Project::StageDefinition Project::GateDefinition], message: :must_be_a_stage_or_gate }
+ attr_readonly :type
+
acts_as_list
def initialize(*args)
diff --git a/app/models/project/stage.rb b/app/models/project/stage.rb
index 4e5f8ef92a4..b49ca7787b7 100644
--- a/app/models/project/stage.rb
+++ b/app/models/project/stage.rb
@@ -27,5 +27,7 @@
#++
class Project::Stage < Project::LifeCycleStep
+ # This ensures the type cannot be changed after initialising the class.
+ validates :type, inclusion: { in: %w[Project::Stage], message: :must_be_a_stage }
validates :start_date, :end_date, presence: true
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 34fec6cbe6f..f316ac0db44 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1118,6 +1118,8 @@ en:
attributes:
type:
must_be_a_stage_or_gate: "must be either Project::Stage or Project::Gate"
+ must_be_a_stage: "must be a Project::Stage"
+ must_be_a_gate: "must be a Project::Gate"
project/gate:
attributes:
base:
diff --git a/spec/models/project/gate_spec.rb b/spec/models/project/gate_spec.rb
index 874a4314ee0..f634c63c183 100644
--- a/spec/models/project/gate_spec.rb
+++ b/spec/models/project/gate_spec.rb
@@ -34,17 +34,18 @@ RSpec.describe Project::Gate do
describe "validations" do
it { is_expected.to validate_presence_of(:date) }
+ it { is_expected.to validate_inclusion_of(:type).in_array(["Project::Gate"]).with_message(:must_be_a_gate) }
it "is invalid if `end_date` is present" do
- gate_with_end_date = build(:project_gate, end_date: Time.zone.today)
+ subject.end_date = Time.zone.today
- expect(gate_with_end_date).not_to be_valid
- expect(gate_with_end_date.errors[:base])
+ expect(subject).not_to be_valid
+ expect(subject.errors[:base])
.to include("Cannot assign `end_date` to a Project::Gate")
end
it "is valid if `end_date` is not present" do
- valid_gate = build(:project_gate)
+ valid_gate = build(:project_gate, end_date: nil)
expect(valid_gate).to be_valid
end
end
diff --git a/spec/models/project/life_cycle_step_definition_spec.rb b/spec/models/project/life_cycle_step_definition_spec.rb
index 7ccdf639978..6251817bb2e 100644
--- a/spec/models/project/life_cycle_step_definition_spec.rb
+++ b/spec/models/project/life_cycle_step_definition_spec.rb
@@ -33,6 +33,12 @@ RSpec.describe Project::LifeCycleStepDefinition do
expect { described_class.new }.to raise_error(NotImplementedError)
end
+ context "with a Project::StageDefinition" do
+ subject { create :project_stage_definition }
+
+ it { is_expected.to have_readonly_attribute(:type) }
+ end
+
# For more specs see:
# - spec/support/shared/project_life_cycle_helpers.rb
# - spec/models/project/gate_definition_spec.rb
diff --git a/spec/models/project/life_cycle_step_spec.rb b/spec/models/project/life_cycle_step_spec.rb
index 1c47422612d..03722a2cb66 100644
--- a/spec/models/project/life_cycle_step_spec.rb
+++ b/spec/models/project/life_cycle_step_spec.rb
@@ -37,6 +37,7 @@ RSpec.describe Project::LifeCycleStep do
subject { build :project_gate }
it { is_expected.to have_readonly_attribute(:definition_id) }
+ it { is_expected.to have_readonly_attribute(:type) }
end
# For more specs see:
diff --git a/spec/models/project/stage_spec.rb b/spec/models/project/stage_spec.rb
index 2ee72a827ed..6fd727a34b2 100644
--- a/spec/models/project/stage_spec.rb
+++ b/spec/models/project/stage_spec.rb
@@ -35,6 +35,7 @@ RSpec.describe Project::Stage do
describe "validations" do
it { is_expected.to validate_presence_of(:start_date) }
it { is_expected.to validate_presence_of(:end_date) }
+ it { is_expected.to validate_inclusion_of(:type).in_array(["Project::Stage"]).with_message(:must_be_a_stage) }
it "is valid when `start_date` and `end_date` are present" do
valid_stage = build(:project_stage)