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

turn this_week setting aware

ulferts0b79bf44e200
diff --git a/app/models/queries/projects/filters/any_stage_or_gate_filter.rb b/app/models/queries/projects/filters/any_stage_or_gate_filter.rb
index 517cf545bc5..7596214cea3 100644
--- a/app/models/queries/projects/filters/any_stage_or_gate_filter.rb
+++ b/app/models/queries/projects/filters/any_stage_or_gate_filter.rb
@@ -54,30 +54,42 @@ class Queries::Projects::Filters::AnyStageOrGateFilter < Queries::Projects::Filt
end
def where
- case operator.to_sym
- when Queries::Operators::OnDate.to_sym
- stage_where_on(parsed_start)
- .or(gate_where(parsed_end))
- .arel
- .exists
- when Queries::Operators::Today.to_sym
- stage_where_on(today)
- .or(gate_where(today, today))
- .arel
- .exists
- when Queries::Operators::BetweenDate.to_sym
- stage_where_between(parsed_start, parsed_end)
- .or(gate_where(parsed_start, parsed_end))
- .arel
- .exists
- when Queries::Operators::ThisWeek.to_sym
- stage_overlaps_this_week
- .or(gate_where(today.beginning_of_week, today.end_of_week))
- .arel
- .exists
- else
- raise "Unknown operator #{operator}"
- end
+ scope = case operator.to_sym
+ when Queries::Operators::OnDate.to_sym
+ on_date
+ when Queries::Operators::Today.to_sym
+ on_today
+ when Queries::Operators::BetweenDate.to_sym
+ between_date
+ when Queries::Operators::ThisWeek.to_sym
+ this_week
+ else
+ raise "Unknown operator #{operator}"
+ end
+
+ scope
+ .arel
+ .exists
+ end
+
+ def on_date
+ stage_where_on(parsed_start)
+ .or(gate_where(parsed_end))
+ end
+
+ def on_today
+ stage_where_on(today)
+ .or(gate_where(today, today))
+ end
+
+ def between_date
+ stage_where_between(parsed_start, parsed_end)
+ .or(gate_where(parsed_start, parsed_end))
+ end
+
+ def this_week
+ stage_overlaps_this_week
+ .or(gate_where(beginning_of_week.to_date, end_of_week.to_date))
end
def stage_where_on(start_date, end_date = start_date)
@@ -117,7 +129,7 @@ class Queries::Projects::Filters::AnyStageOrGateFilter < Queries::Projects::Filt
.where(type: Project::Stage.name)
.active
.where(
- <<~SQL.squish, today.beginning_of_week, today.end_of_week
+ <<~SQL.squish, beginning_of_week, end_of_week
daterange(#{Project::LifeCycleStep.table_name}.start_date,
#{Project::LifeCycleStep.table_name}.end_date,
'[]')
@@ -139,6 +151,14 @@ class Queries::Projects::Filters::AnyStageOrGateFilter < Queries::Projects::Filt
Time.zone.today
end
+ def beginning_of_week
+ OpenProject::I18n::Date.time_at_beginning_of_week
+ end
+
+ def end_of_week
+ beginning_of_week + 7.days
+ end
+
def connection
ActiveRecord::Base.connection
end
diff --git a/spec/models/queries/projects/project_query_results_any_stage_or_gate_spec.rb b/spec/models/queries/projects/project_query_results_any_stage_or_gate_spec.rb
index 3e3936a83c9..6926295f483 100644
--- a/spec/models/queries/projects/project_query_results_any_stage_or_gate_spec.rb
+++ b/spec/models/queries/projects/project_query_results_any_stage_or_gate_spec.rb
@@ -37,18 +37,15 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
shared_let(:stage_start_date) { Date.parse("2025-02-07") }
shared_let(:stage_end_date) { Date.parse("2025-02-17") }
- shared_let(:project_with_stage) do
- create(:project, name: "Project with stage") do |project|
- create(:project_stage, project:, start_date: stage_start_date, end_date: stage_end_date)
- end
+ shared_let(:project_with_stage) { create(:project, name: "Project with stage") }
+ shared_let(:stage) do
+ create(:project_stage, project: project_with_stage, start_date: stage_start_date, end_date: stage_end_date)
end
- shared_let(:gate_date) { Date.parse("2025-02-22") }
- shared_let(:project_with_gate) do
- create(:project, name: "Project with gate") do |project|
- create(:project_gate, project:, date: gate_date)
- end
- end
+ shared_let(:gate_date) { Date.parse("2025-03-06") }
+ shared_let(:project_with_gate) { create(:project, name: "Project with gate") }
+ shared_let(:gate) { create(:project_gate, project: project_with_gate, date: gate_date) }
+
shared_let(:project_without_step) { create(:project, name: "Project without step") }
shared_let(:user) do
@@ -320,8 +317,6 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
end
end
- # TODO: get the start of week setting in
-
context "with a w (this week) operator" do
before do
instance.where("any_stage_or_gate", "w", [])
@@ -344,9 +339,6 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
end
context "when the current week overlaps the end of the stage" do
- # Would otherwise interfere with the spec
- remove_gate
-
it "returns the project whose stage begins within the week" do
Timecop.travel(stage_end_date.noon) do
expect(instance.results).to contain_exactly(project_with_stage)
@@ -454,6 +446,110 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
end
end
end
+
+ context "when being on Monday, the stage ends on Sunday and the week is configured to start on Sunday",
+ with_settings: { start_of_week: 7 } do
+ before do
+ stage.update_column(:end_date, Date.parse("2025-02-16"))
+ end
+
+ it "returns the project whose stage has ended within the current week" do
+ Timecop.travel(Date.parse("2025-02-17").noon) do
+ expect(instance.results).to contain_exactly(project_with_stage)
+ end
+ end
+ end
+
+ context "when being on Monday, the stage ends on Sunday and the week is configured to start on Monday",
+ with_settings: { start_of_week: 1 } do
+ before do
+ stage.update_column(:end_date, Date.parse("2025-02-16"))
+ end
+
+ it "returns no project as the stage ended in the week before" do
+ Timecop.travel(Date.parse("2025-02-17").noon) do
+ expect(instance.results).to be_empty
+ end
+ end
+ end
+
+ context "when being on Monday, the gate is on Sunday and the week is configured to start on Sunday",
+ with_settings: { start_of_week: 7 } do
+ before do
+ gate.update_column(:start_date, Date.parse("2025-03-02"))
+ end
+
+ it "returns the project whose gate is in the current week" do
+ Timecop.travel(Date.parse("2025-03-03").noon) do
+ expect(instance.results).to contain_exactly(project_with_gate)
+ end
+ end
+ end
+
+ context "when being on Monday, the gate is on Sunday and the week is configured to start on Monday",
+ with_settings: { start_of_week: 1 } do
+ before do
+ gate.update_column(:start_date, Date.parse("2025-03-02"))
+ end
+
+ it "returns no project as the stage ended in the week before" do
+ Timecop.travel(Date.parse("2025-03-03").noon) do
+ expect(instance.results).to be_empty
+ end
+ end
+ end
+
+ context "when being on Sunday, the stage ends on Monday and the week is configured to start on Monday",
+ with_settings: { start_of_week: 1 } do
+ before do
+ stage.update_column(:end_date, Date.parse("2025-02-17"))
+ end
+
+ it "returns the project whose stage has ended within the current week" do
+ Timecop.travel(Date.parse("2025-02-23").noon) do
+ expect(instance.results).to contain_exactly(project_with_stage)
+ end
+ end
+ end
+
+ context "when being on Sunday, the stage ends on Monday and the week is configured to start on Sunday",
+ with_settings: { start_of_week: 7 } do
+ before do
+ stage.update_column(:end_date, Date.parse("2025-02-17"))
+ end
+
+ it "returns no project as the stage ended in the week before" do
+ Timecop.travel(Date.parse("2025-02-23").noon) do
+ expect(instance.results).to be_empty
+ end
+ end
+ end
+
+ context "when being on Sunday, the gate is on Monday and the week is configured to start on Monday",
+ with_settings: { start_of_week: 1 } do
+ before do
+ gate.update_column(:start_date, Date.parse("2025-03-03"))
+ end
+
+ it "returns the project whose gate is in the current week" do
+ Timecop.travel(Date.parse("2025-03-09").noon) do
+ expect(instance.results).to contain_exactly(project_with_gate)
+ end
+ end
+ end
+
+ context "when being on Sunday, the gate is on Monday and the week is configured to start on Sunday",
+ with_settings: { start_of_week: 7 } do
+ before do
+ gate.update_column(:start_date, Date.parse("2025-03-03"))
+ end
+
+ it "returns no project as the stage ended in the week before" do
+ Timecop.travel(Date.parse("2025-03-09").noon) do
+ expect(instance.results).to be_empty
+ end
+ end
+ end
end
context "with a <>d (between) operator" do