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

Display duration in days caption for Project::Stage entries.

Dombi Attila59bf7cc408c8
diff --git a/app/forms/projects/life_cycles/form.rb b/app/forms/projects/life_cycles/form.rb
index e65ce49e9bb..de42b5a05ad 100644
--- a/app/forms/projects/life_cycles/form.rb
+++ b/app/forms/projects/life_cycles/form.rb
@@ -61,23 +61,21 @@ module Projects::LifeCycles
end
def single_value_life_cycle_input(form)
- input_attributes = base_input_attributes.merge(
- name: :date,
- value: model.date
- )
+ input_attributes = { name: :date, value: model.date }
- form.single_date_picker **input_attributes
+ form.single_date_picker **base_input_attributes, **input_attributes
end
def multi_value_life_cycle_input(form)
value = [model.start_date, model.end_date].compact.join(" - ")
- input_attributes = base_input_attributes.merge(
- name: :date_range,
- value:
- )
+ input_attributes = { name: :date_range, value: }
+ if model.working_days_count
+ input_attributes[:caption] =
+ I18n.t("project_stage.working_days_count", count: model.working_days_count)
+ end
- form.range_date_picker **input_attributes
+ form.range_date_picker **base_input_attributes, **input_attributes
end
def text
diff --git a/app/models/project/stage.rb b/app/models/project/stage.rb
index 94e29cf4fd2..bd6a3f0b1c8 100644
--- a/app/models/project/stage.rb
+++ b/app/models/project/stage.rb
@@ -31,6 +31,12 @@ class Project::Stage < Project::LifeCycleStep
validates :type, inclusion: { in: %w[Project::Stage], message: :must_be_a_stage }
validate :validate_date_range
+ def working_days_count
+ return nil if not_set?
+
+ Day.working.from_range(from: start_date, to: end_date).count
+ end
+
def date_range=(param)
self.start_date, self.end_date = param.split(" - ")
self.end_date ||= start_date # Allow single dates as range
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 0c833542ee2..0a6aeda61e7 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -425,7 +425,10 @@ en:
no_results_content_text: Create a new version
storage:
no_results_title_text: There is no additional recorded disk space consumed by this project.
-
+ project_stage:
+ working_days_count:
+ one: "Duration: %{count} working day"
+ other: "Duration: %{count} working days"
lists:
create:
success: "The modified list has been saved as a new list"
diff --git a/spec/models/project/stage_spec.rb b/spec/models/project/stage_spec.rb
index fb132a03cbb..ebe1b01646b 100644
--- a/spec/models/project/stage_spec.rb
+++ b/spec/models/project/stage_spec.rb
@@ -100,4 +100,40 @@ RSpec.describe Project::Stage do
expect(subject.errors.symbols_for(:type)).to include(:type_and_class_name_mismatch)
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.end_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 end_date are the same" do
+ subject.start_date = Time.zone.today
+ subject.end_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.end_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.end_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.end_date)
+ .and_return([])
+
+ expect(subject.working_days_count).to eq(0)
+ end
+ end
end