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

add duration to project phase

Ivan Kuchindf3f97087f56
diff --git a/app/forms/projects/life_cycles/form.rb b/app/forms/projects/life_cycles/form.rb
index 4ab76c6b1e3..15d1db460f9 100644
--- a/app/forms/projects/life_cycles/form.rb
+++ b/app/forms/projects/life_cycles/form.rb
@@ -55,9 +55,9 @@ module Projects::LifeCycles
value = [model.start_date, model.finish_date].compact.join(" - ")
input_attributes = { name: :date_range, value: }
- if model.working_days_count
+ if model.duration
input_attributes[:caption] =
- I18n.t("project_phase.working_days_count", count: model.working_days_count)
+ I18n.t("project_phase.duration", count: model.duration)
end
form.range_date_picker **base_input_attributes, **input_attributes
diff --git a/app/models/project/phase.rb b/app/models/project/phase.rb
index 7a2c4e794a6..dbc3a37f3fd 100644
--- a/app/models/project/phase.rb
+++ b/app/models/project/phase.rb
@@ -33,6 +33,7 @@ class Project::Phase < ApplicationRecord
class_name: "Project::PhaseDefinition"
has_many :work_packages, inverse_of: :project_phase, dependent: :nullify
+ validates :duration, presence: true, if: :range_set?
validate :validate_date_range
delegate :name,
@@ -54,12 +55,6 @@ class Project::Phase < ApplicationRecord
end
end
- def working_days_count
- return nil if not_set?
-
- Day.working.from_range(from: start_date, to: finish_date).count
- end
-
def date_range=(range)
case range
when String
diff --git a/app/services/journals/create_service/project_phase.rb b/app/services/journals/create_service/project_phase.rb
index 718d6752651..363ec42a39a 100644
--- a/app/services/journals/create_service/project_phase.rb
+++ b/app/services/journals/create_service/project_phase.rb
@@ -49,14 +49,16 @@ class Journals::CreateService
phase_id,
start_date,
finish_date,
- active
+ active,
+ duration
)
SELECT
#{id_from_inserted_journal_sql},
project_phases.id,
project_phases.start_date,
project_phases.finish_date,
- project_phases.active
+ project_phases.active,
+ project_phases.duration
FROM project_phases
WHERE
#{only_if_created_sql}
@@ -84,6 +86,7 @@ class Journals::CreateService
phases.start_date IS DISTINCT FROM project_phase_journals.start_date
OR phases.finish_date IS DISTINCT FROM project_phase_journals.finish_date
OR phases.active IS DISTINCT FROM project_phase_journals.active
+ OR phases.duration IS DISTINCT FROM project_phase_journals.duration
SQL
end
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index eeb1d35a0f9..7b2297fc144 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -460,7 +460,7 @@ en:
work_package_priorities:
new_label: "New priority"
project_phase:
- working_days_count:
+ duration:
one: "Duration: %{count} working day"
other: "Duration: %{count} working days"
lists:
diff --git a/db/migrate/20250411104802_add_duration_to_project_phases.rb b/db/migrate/20250411104802_add_duration_to_project_phases.rb
new file mode 100644
index 00000000000..7901b5d6b5c
--- /dev/null
+++ b/db/migrate/20250411104802_add_duration_to_project_phases.rb
@@ -0,0 +1,8 @@
+# frozen_string_literal: true
+
+class AddDurationToProjectPhases < ActiveRecord::Migration[8.0]
+ def change
+ add_column :project_phases, :duration, :integer, null: true
+ add_column :project_phase_journals, :duration, :integer, null: true
+ end
+end
diff --git a/spec/factories/project_phase_factory.rb b/spec/factories/project_phase_factory.rb
index bf5e600d07d..9c50cff8467 100644
--- a/spec/factories/project_phase_factory.rb
+++ b/spec/factories/project_phase_factory.rb
@@ -36,6 +36,7 @@ FactoryBot.define do
start_date { Date.current - 2.days }
finish_date { Date.current + 2.days }
+ duration { 5 }
trait :skip_validate do
to_create { |instance| instance.save(validate: false) }
diff --git a/spec/models/project/phase_spec.rb b/spec/models/project/phase_spec.rb
index 1070f0a5c3f..1a82e665041 100644
--- a/spec/models/project/phase_spec.rb
+++ b/spec/models/project/phase_spec.rb
@@ -124,43 +124,4 @@ RSpec.describe Project::Phase do
expect(subject.errors[:date_range]).to be_empty
end
end
-
- describe "#working_days_count" do
- it "returns nil if not_set? is true" do
- allow(Day).to receive(:working)
-
- subject.start_date = nil
- subject.finish_date = nil
-
- expect(subject.working_days_count).to be_nil
- expect(Day).not_to have_received(:working)
- end
-
- it "returns the correct number of days if start_date and finish_date are the same" do
- subject.start_date = Time.zone.today
- subject.finish_date = Time.zone.today
- expect(subject.working_days_count).to eq(1)
- end
-
- it "returns the correct number of days for a valid date range" do
- subject.start_date = Date.parse("2024-11-25")
- subject.finish_date = Date.parse("2024-11-27")
- expect(subject.working_days_count).to eq(3)
- end
-
- it "calls the Day.working.from_range method with the right arguments" do
- subject.start_date = Date.parse("2024-11-25")
- subject.finish_date = Date.parse("2024-11-27")
-
- allow(Day).to receive(:working).and_return(Day)
- allow(Day).to receive(:from_range)
- .with(from: subject.start_date, to: subject.finish_date)
- .and_return([])
-
- expect(subject.working_days_count).to eq(0)
-
- expect(Day).to have_received(:working).with(no_args)
- expect(Day).to have_received(:from_range).with(from: subject.start_date, to: subject.finish_date)
- end
- end
end