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 Day.next_working method

Dombi Attila4e5c32e8a293
diff --git a/app/models/day.rb b/app/models/day.rb
index a2c7261b838..986e2f1eeca 100644
--- a/app/models/day.rb
+++ b/app/models/day.rb
@@ -97,6 +97,12 @@ class Day < ApplicationRecord
# Look up only from 8 days ago, because the Setting.working_days must have at least 1 working weekday.
from_range(from: 8.days.ago, to: Time.zone.yesterday).where(working: true).last
end
+
+ def next_working(from: Time.zone.today)
+ # Look up only the next 8 days, because the Setting.working_days must have at least 1 working weekday.
+ # Exclude the current day
+ from_range(from: from + 1.day, to: from + 9.days).where(working: true).first
+ end
end
def week_day
diff --git a/app/models/project/phase.rb b/app/models/project/phase.rb
index ea63545fbb7..b1a5b9ea695 100644
--- a/app/models/project/phase.rb
+++ b/app/models/project/phase.rb
@@ -87,11 +87,12 @@ class Project::Phase < ApplicationRecord
def default_start_date
return @default_start_date if defined?(@default_start_date)
- @default_start_date = project
+ last_finish_date = project
.available_phases
.select { it.position < position }
.filter_map(&:finish_date)
.last
- &.next_day
+
+ @default_start_date = last_finish_date && Day.next_working(from: last_finish_date).date
end
end
diff --git a/spec/models/day_spec.rb b/spec/models/day_spec.rb
index ee9574f91ac..c64fa1e9394 100644
--- a/spec/models/day_spec.rb
+++ b/spec/models/day_spec.rb
@@ -123,6 +123,80 @@ RSpec.describe Day do
end
end
+ describe ".next_working" do
+ shared_examples_for "calculating the next working day" do |as:|
+ context "when #{as} is Monday" do
+ let(:time) { Time.current.monday }
+
+ context "and tomorrow is a working day" do
+ it "returns Tuesday (tomorrow)" do
+ expect(subject.date).to eq(time.tomorrow)
+ end
+ end
+ end
+
+ context "when #{as} is Friday" do
+ let(:time) { Time.current.monday.next_occurring(:friday) }
+
+ context "and tomorrow is a weekend day" do
+ it "returns next Monday" do
+ expect(subject.date).to eq(time.next_occurring(:monday))
+ end
+ end
+ end
+
+ context "when #{as} is Thursday" do
+ let(:time) { Time.current.monday.next_occurring(:thursday) }
+
+ context "and tomorrow is working" do
+ it "returns Friday (tomorrow)" do
+ expect(subject.date).to eq(time.tomorrow)
+ end
+ end
+
+ context "and #{as} is non-working" do
+ before do
+ create(:non_working_day, date: time.tomorrow)
+ end
+
+ it "returns next Monday" do
+ expect(subject.date).to eq(time.next_occurring(:monday))
+ end
+ end
+ end
+
+ context "when #{as} is Saturday" do
+ let(:time) { Time.current.monday.next_occurring(:saturday) }
+
+ context "and tomorrow is non-working" do
+ before do
+ create(:non_working_day, date: time.tomorrow)
+ end
+
+ it "returns next Monday" do
+ expect(subject.date).to eq(time.next_occurring(:monday))
+ end
+ end
+ end
+ end
+
+ context "without providing a :from date" do
+ around do |ex|
+ Timecop.travel(time, &ex)
+ end
+
+ subject { described_class.next_working }
+
+ it_behaves_like "calculating the next working day", as: "today"
+ end
+
+ context "when providing a :from date" do
+ subject { described_class.next_working(from: time) }
+
+ it_behaves_like "calculating the next working day", as: "the from argument"
+ end
+ end
+
describe "#working" do
context "when the week day is non-working" do
shared_let(:working_days) { week_with_no_working_days }