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
Remove condition for invalid LifeCycleStep initialization, add validation instead.
Dombi Attilaf29a90571317
diff --git a/app/models/project/life_cycle_step.rb b/app/models/project/life_cycle_step.rbindex c4e6cc58876..9a6b26a6e35 100644--- a/app/models/project/life_cycle_step.rb+++ b/app/models/project/life_cycle_step.rb@@ -38,17 +38,14 @@ class Project::LifeCycleStep < ApplicationRecordattr_readonly :definition_id, :typevalidates :type, inclusion: { in: %w[Project::Stage Project::Gate], message: :must_be_a_stage_or_gate }+ validate :validate_type_and_class_name_are_identicalscope :active, -> { where(active: true) }- def initialize(*args)- if instance_of? Project::LifeCycleStep- # Do not allow directly instantiating this class- raise NotImplementedError, "Cannot instantiate the base Project::LifeCycleStep class directly. " \- "Use Project::Stage or Project::Gate instead."+ def validate_type_and_class_name_are_identical+ if type != self.class.name+ errors.add(:type, :type_and_class_name_mismatch)end-- superenddef column_namediff --git a/app/models/project/life_cycle_step_definition.rb b/app/models/project/life_cycle_step_definition.rbindex 7386a0ff4a5..0aadf22d084 100644--- a/app/models/project/life_cycle_step_definition.rb+++ b/app/models/project/life_cycle_step_definition.rb@@ -37,26 +37,12 @@ class Project::LifeCycleStepDefinition < ApplicationRecordvalidates :name, presence: truevalidates :type, inclusion: { in: %w[Project::StageDefinition Project::GateDefinition], message: :must_be_a_stage_or_gate }+ validate :validate_type_and_class_name_are_identicalattr_readonly :typeacts_as_list- # TODO: Enabling this causes an error in the acts_as_customizable gem- # /gems/acts_as_list-1.2.4/lib/acts_as_list/active_record/acts/position_column_method_definer.rb- # define_singleton_method :touch_record_sql do- # new.touch_record_sql- # end- # Calling new will fail with the error below:- # def initialize(*args)- # if instance_of? Project::LifeCycleStepDefinition- # # Do not allow directly instantiating this class- # raise NotImplementedError, "Cannot instantiate the base Project::LifeCycleStepDefinition class directly. " \- # "Use Project::StageDefinition or Project::GateDefinition instead."- # end- # super- # end-def step_classraise NotImplementedErrorend@@ -64,4 +50,12 @@ class Project::LifeCycleStepDefinition < ApplicationRecorddef column_name"lcsd_#{id}"end++ private++ def validate_type_and_class_name_are_identical+ if type != self.class.name+ errors.add(:type, :type_and_class_name_mismatch)+ end+ endenddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex 8999c6dbd13..24cabb38a5b 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -1209,20 +1209,34 @@ en:attributes:type:must_be_a_stage_or_gate: "must be either Project::StageDefinition or Project::GateDefinition"+ type_and_class_name_mismatch: "must match the instantiated class name"+ project/gate_definition:+ attributes:+ type:+ type_and_class_name_mismatch: "must be a Project::GateDefinition"+ project/stage_definition:+ attributes:+ type:+ type_and_class_name_mismatch: "must be a Project::StageDefinition"project/life_cycle_step: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"+ type_and_class_name_mismatch: "must match the instantiated class name"project/gate:attributes:base:end_date_not_allowed: "Cannot assign end date to a Project::Gate"+ type:+ type_and_class_name_mismatch: "must be a Project::Gate"project/stage:attributes:date_range:start_date_must_be_before_end_date: "Start date must be before the end date"+ type:+ type_and_class_name_mismatch: "must be a Project::Stage"query:attributes:project:diff --git a/spec/models/project/gate_definition_spec.rb b/spec/models/project/gate_definition_spec.rbindex f943a416d9b..2b1d4c2f9ec 100644--- a/spec/models/project/gate_definition_spec.rb+++ b/spec/models/project/gate_definition_spec.rb@@ -41,6 +41,14 @@ RSpec.describe Project::GateDefinition doendend+ describe "validations" do+ it "is invalid if type and class name do not match" do+ subject.type = "Project::StageDefinition"+ expect(subject).not_to be_valid+ expect(subject.errors.symbols_for(:type)).to include(:type_and_class_name_mismatch)+ end+ end+describe "#step_class" doit "returns Project::Stage" doexpect(subject.step_class).to eq(Project::Gate)diff --git a/spec/models/project/gate_spec.rb b/spec/models/project/gate_spec.rbindex c0e7645e1f9..dd5b4c84e34 100644--- a/spec/models/project/gate_spec.rb+++ b/spec/models/project/gate_spec.rb@@ -48,5 +48,11 @@ RSpec.describe Project::Gate dovalid_gate = build(:project_gate, end_date: nil)expect(valid_gate).to be_validend++ it "is invalid if type and class name do not match" do+ subject.type = "Project::Stage"+ expect(subject).not_to be_valid+ expect(subject.errors.symbols_for(:type)).to include(:type_and_class_name_mismatch)+ endendenddiff --git a/spec/models/project/life_cycle_step_definition_spec.rb b/spec/models/project/life_cycle_step_definition_spec.rbindex ff58d203c5c..f5c69c5f7f9 100644--- a/spec/models/project/life_cycle_step_definition_spec.rb+++ b/spec/models/project/life_cycle_step_definition_spec.rb@@ -39,6 +39,14 @@ RSpec.describe Project::LifeCycleStepDefinition doit { is_expected.to have_readonly_attribute(:type) }end+ describe "validations" do+ it "is invalid if type and class name do not match" do+ subject.type = "Project::GateDefinition"+ expect(subject).not_to be_valid+ expect(subject.errors.symbols_for(:type)).to include(:type_and_class_name_mismatch)+ end+ end+# For more specs see:# - spec/support/shared/project_life_cycle_helpers.rb# - spec/models/project/gate_definition_spec.rbdiff --git a/spec/models/project/life_cycle_step_spec.rb b/spec/models/project/life_cycle_step_spec.rbindex 03722a2cb66..e1e882d5e1d 100644--- a/spec/models/project/life_cycle_step_spec.rb+++ b/spec/models/project/life_cycle_step_spec.rb@@ -29,8 +29,8 @@require "rails_helper"RSpec.describe Project::LifeCycleStep do- it "cannot be instantiated" do- expect { described_class.new }.to raise_error(NotImplementedError)+ it "can be instantiated" do+ expect { described_class.new }.not_to raise_error(NotImplementedError)enddescribe "with an instantiated Gate" do@@ -40,6 +40,14 @@ RSpec.describe Project::LifeCycleStep doit { is_expected.to have_readonly_attribute(:type) }end+ describe "validations" do+ it "is invalid if type and class name do not match" do+ subject.type = "Project::Gate"+ expect(subject).not_to be_valid+ expect(subject.errors.symbols_for(:type)).to include(:type_and_class_name_mismatch)+ end+ end+# For more specs see:# - spec/support/shared/project_life_cycle_helpers.rb# - spec/models/project/gate_spec.rbdiff --git a/spec/models/project/stage_definition_spec.rb b/spec/models/project/stage_definition_spec.rbindex 1d94467a546..0f236e6b46a 100644--- a/spec/models/project/stage_definition_spec.rb+++ b/spec/models/project/stage_definition_spec.rb@@ -41,6 +41,14 @@ RSpec.describe Project::StageDefinition doendend+ describe "validations" do+ it "is invalid if type and class name do not match" do+ subject.type = "Project::GateDefinition"+ expect(subject).not_to be_valid+ expect(subject.errors.symbols_for(:type)).to include(:type_and_class_name_mismatch)+ end+ end+describe "#step_class" doit "returns Project::Stage" doexpect(subject.step_class).to eq(Project::Stage)diff --git a/spec/models/project/stage_spec.rb b/spec/models/project/stage_spec.rbindex 3bd2d19cb19..fb132a03cbb 100644--- a/spec/models/project/stage_spec.rb+++ b/spec/models/project/stage_spec.rb@@ -93,5 +93,11 @@ RSpec.describe Project::Stage doexpect(subject).not_to be_validexpect(subject.errors[:date_range]).to be_emptyend++ it "is invalid if type and class name do not match" do+ subject.type = "Project::Gate"+ expect(subject).not_to be_valid+ expect(subject.errors.symbols_for(:type)).to include(:type_and_class_name_mismatch)+ endendend