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
turn retrieving the project_phase into a work_package instance method
ulferts155c99841ef9
diff --git a/app/models/work_package.rb b/app/models/work_package.rbindex 9beed80240b..dd3204b562f 100644--- a/app/models/work_package.rb+++ b/app/models/work_package.rb@@ -338,6 +338,15 @@ class WorkPackage < ApplicationRecordduration ? duration * 24 : nilend+ def project_phase+ # This might look less efficient than using+ # ProjectPhase.find_by(definition_id: project_phase_definition_id, project_id: project_id)+ # as more phases are loaded.+ # However, the expected number of phases per project is rather small and this way, a project+ # loaded for multiple work packages can be reused.+ project&.phases&.detect { |phase| phase.definition_id == project_phase_definition_id }+ end+# aliasing subject to name# using :alias is not possible as AR will add the subject method laterdef namediff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rbindex 5bd14163429..b16687cce4c 100644--- a/lib/api/v3/work_packages/work_package_representer.rb+++ b/lib/api/v3/work_packages/work_package_representer.rb@@ -682,9 +682,7 @@ module APIenddef project_phase- @project_phase ||= represented.project&.phases&.detect do- it.definition_id == represented.project_phase_definition_id- end+ @project_phase ||= represented.project_phaseenddef phase_set_and_active?diff --git a/spec/models/work_package_spec.rb b/spec/models/work_package_spec.rbindex 5d8e46f5ace..f5f0abc2069 100644--- a/spec/models/work_package_spec.rb+++ b/spec/models/work_package_spec.rb@@ -878,4 +878,30 @@ RSpec.describe WorkPackage doexpect(work_package.remaining_hours).to eq((3 * 8) + 1.5)endend++ describe "#project_phase" do+ let(:project_phase) { build_stubbed(:project_phase, definition: project_phase_definition) }+ let(:project_phase_definition) { build_stubbed(:project_phase_definition) }+ let(:project) { build_stubbed(:project, phases: [project_phase]) }+ let(:work_package) do+ build_stubbed(:work_package,+ project:,+ project_phase_definition: project_phase_definition)+ end++ describe "when the project phase exists in the project" do+ it "returns the project phase definition" do+ expect(work_package.project_phase).to eq(project_phase)+ end+ end++ describe "when the project phase does not exist in the project (e.g. moved into the project)" do+ # There is now only another project phase active in the project+ let(:project_phase) { build_stubbed(:project_phase, definition: build_stubbed(:project_phase_definition)) }++ it "returns nil" do+ expect(work_package.project_phase).to be_nil+ end+ end+ endend