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

introduce `this week` operator on `AnyStageOrGateFilter`

ulferts15423e8300ec
diff --git a/app/models/queries/operators/this_week.rb b/app/models/queries/operators/this_week.rb
index 1d5af0993f5..037671fbda6 100644
--- a/app/models/queries/operators/this_week.rb
+++ b/app/models/queries/operators/this_week.rb
@@ -42,14 +42,14 @@ module Queries::Operators
def self.begin_of_week
if I18n.t(:general_first_day_of_week) == "7"
# week starts on sunday
- if Date.today.cwday == 7
- Time.now.at_beginning_of_day
+ if Time.zone.today.cwday == 7
+ Time.current.at_beginning_of_day
else
- Time.now.at_beginning_of_week - 1.day
+ Time.current.at_beginning_of_week - 1.day
end
else
# week starts on monday (Rails default)
- Time.now.at_beginning_of_week
+ Time.current.at_beginning_of_week
end
end
end
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 d8ec1aebd22..5db1121b470 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
@@ -36,6 +36,7 @@ class Queries::Projects::Filters::AnyStageOrGateFilter < Queries::Projects::Filt
def available_operators
[
::Queries::Operators::Today,
+ ::Queries::Operators::ThisWeek,
::Queries::Operators::OnDate,
::Queries::Operators::BetweenDate
]
@@ -51,22 +52,27 @@ class Queries::Projects::Filters::AnyStageOrGateFilter < Queries::Projects::Filt
end
def where
- case operator
- when "=d"
+ case operator.to_sym
+ when Queries::Operators::OnDate.to_sym
stage_where_on(parsed_start)
.or(gate_where(parsed_end))
.arel
.exists
- when "t"
+ when Queries::Operators::Today.to_sym
stage_where_on(today)
.or(gate_where(today, today))
.arel
.exists
- when "<>d"
+ 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
@@ -74,7 +80,7 @@ class Queries::Projects::Filters::AnyStageOrGateFilter < Queries::Projects::Filt
def stage_where_on(start_date, end_date = start_date)
Project::LifeCycleStep
- .where("project_id = #{Project.table_name}.id")
+ .where("#{Project::LifeCycleStep.table_name}.project_id = #{Project.table_name}.id")
.where(type: Project::Stage.name)
.where(date_range_clause(Project::LifeCycleStep.table_name, "start_date", nil, start_date))
.where(date_range_clause(Project::LifeCycleStep.table_name, "end_date", end_date, nil))
@@ -82,7 +88,7 @@ class Queries::Projects::Filters::AnyStageOrGateFilter < Queries::Projects::Filt
def stage_where_between(start_date, end_date)
Project::LifeCycleStep
- .where("project_id = #{Project.table_name}.id")
+ .where("#{Project::LifeCycleStep.table_name}.project_id = #{Project.table_name}.id")
.where(type: Project::Stage.name)
.where(date_range_clause(Project::LifeCycleStep.table_name, "start_date", start_date, nil))
.where(date_range_clause(Project::LifeCycleStep.table_name, "end_date", nil, end_date))
@@ -91,11 +97,26 @@ class Queries::Projects::Filters::AnyStageOrGateFilter < Queries::Projects::Filt
def gate_where(start_date, end_date = start_date)
# On gates, only the start_date is set.
Project::LifeCycleStep
- .where("project_id = #{Project.table_name}.id")
+ .where("#{Project::LifeCycleStep.table_name}.project_id = #{Project.table_name}.id")
.where(type: Project::Gate.name)
.where(date_range_clause(Project::LifeCycleStep.table_name, "start_date", start_date, end_date))
end
+ def stage_overlaps_this_week
+ Project::LifeCycleStep
+ .where("#{Project::LifeCycleStep.table_name}.project_id = #{Project.table_name}.id")
+ .where(type: Project::Stage.name)
+ .where(
+ <<~SQL.squish, today.beginning_of_week, today.end_of_week
+ daterange(#{Project::LifeCycleStep.table_name}.start_date,
+ #{Project::LifeCycleStep.table_name}.end_date,
+ '[]')
+ &&
+ daterange(?, ?, '[]')
+ SQL
+ )
+ end
+
def parsed_start
values.first.present? ? Date.parse(values.first) : nil
end
diff --git a/spec/features/projects/lists/filters_spec.rb b/spec/features/projects/lists/filters_spec.rb
index 65dccb4e8ae..a044d2e8f0d 100644
--- a/spec/features/projects/lists/filters_spec.rb
+++ b/spec/features/projects/lists/filters_spec.rb
@@ -661,6 +661,12 @@ RSpec.describe "Projects list filters", :js, with_settings: { login_required?: f
[Time.zone.today, Time.zone.today + 9.days])
projects_page.expect_projects_in_order(project, public_project)
+
+ projects_page.set_filter("any_stage_or_gate",
+ "Any stage or gate",
+ "this week")
+
+ projects_page.expect_projects_in_order(project)
end
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 99e1cbd76fb..f9e4813ff37 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
@@ -35,15 +35,15 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
shared_let(:view_role) { create(:project_role, permissions: %i[view_project_stages_and_gates]) }
- shared_let(:stage_start_date) { Time.zone.today - 5.days }
- shared_let(:stage_end_date) { Time.zone.today + 5.days }
+ 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
end
- shared_let(:gate_date) { Time.zone.today + 10.days }
+ 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)
@@ -59,6 +59,14 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
current_user { user }
+ def remove_gate
+ Project::LifeCycleStep.where(type: Project::Gate.name).destroy_all
+ end
+
+ def remove_stage
+ Project::LifeCycleStep.where(type: Project::Stage.name).destroy_all
+ end
+
context "with a =d (on) operator" do
before do
instance.where("any_stage_or_gate", "=d", values)
@@ -130,6 +138,7 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
end
# TODO: check active state
+ # TODO: check permissions
context "with a t (today) operator" do
before do
@@ -199,6 +208,104 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
end
end
+ context "with a w (this week) operator" do
+ before do
+ instance.where("any_stage_or_gate", "w", [])
+ end
+
+ context "when being in the middle of the stage" do
+ it "returns the project whose stage is currently running" do
+ Timecop.travel((stage_start_date + 3.days).noon) do
+ expect(instance.results).to contain_exactly(project_with_stage)
+ end
+ end
+ end
+
+ context "when the current week overlaps the beginning of the stage" do
+ it "returns the project whose stage begins within the week" do
+ Timecop.travel((stage_start_date - 1.day).noon) do
+ expect(instance.results).to contain_exactly(project_with_stage)
+ end
+ end
+ end
+
+ context "when the current week overlaps the end of the stage" do
+ # Would otherwise interfere with the spec
+ before do
+ remove_gate
+ end
+
+ 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)
+ end
+ end
+ end
+
+ context "when being before the stage" do
+ it "returns no project" do
+ Timecop.travel(stage_start_date.noon - 7.days) do
+ expect(instance.results).to be_empty
+ end
+ end
+ end
+
+ context "when being after the stage" do
+ it "returns no project" do
+ Timecop.travel(stage_end_date.noon + 7.days) do
+ expect(instance.results).to be_empty
+ end
+ end
+ end
+
+ context "when being a day before the gate" do
+ # Would otherwise interfere with the spec
+ before do
+ remove_stage
+ end
+
+ it "returns the project whose gate is within the current week" do
+ Timecop.travel(gate_date.noon - 1.day) do
+ expect(instance.results).to contain_exactly(project_with_gate)
+ end
+ end
+ end
+
+ context "when being a day after the gate" do
+ # Would otherwise interfere with the spec
+ before do
+ remove_stage
+ end
+
+ it "returns the project whose gate is within the current week" do
+ Timecop.travel(gate_date.noon + 1.day) do
+ expect(instance.results).to contain_exactly(project_with_gate)
+ end
+ end
+ end
+
+ context "when being in the week before the day of the gate" do
+ # Would otherwise interfere with the spec
+ before do
+ remove_stage
+ end
+
+ it "returns no project" do
+ Timecop.travel(gate_date.noon - 7.days) do
+ expect(instance.results).to be_empty
+ end
+ end
+ end
+
+ context "when being in the week after the day of the gate" do
+ it "returns no project" do
+ Timecop.travel(gate_date.noon + 7.days) do
+ expect(instance.results).to be_empty
+ end
+ end
+ end
+ end
+
context "with a <>d (between) operator" do
before do
instance.where("any_stage_or_gate", "<>d", values)
@@ -249,7 +356,7 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
# Interferes at it would otherwise be found as well
before do
- Project::LifeCycleStep.where(type: Project::Gate.name).destroy_all
+ remove_gate
end
it "returns the project with the stage" do
@@ -262,7 +369,7 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
# Interferes at it would otherwise be found as well
before do
- Project::LifeCycleStep.where(type: Project::Gate.name).destroy_all
+ remove_gate
end
it "returns the project with the stage" do
@@ -275,7 +382,7 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
# Interferes at it would otherwise be found as well
before do
- Project::LifeCycleStep.where(type: Project::Gate.name).destroy_all
+ remove_gate
end
it "returns no project" do
@@ -320,7 +427,7 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
# Interferes at it would otherwise be found as well
before do
- Project::LifeCycleStep.where(type: Project::Stage.name).destroy_all
+ remove_stage
end
it "returns the project with the gate" do
@@ -333,7 +440,7 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
# Interferes at it would otherwise be found as well
before do
- Project::LifeCycleStep.where(type: Project::Stage.name).destroy_all
+ remove_stage
end
it "returns the project with the gate" do
@@ -346,7 +453,7 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
# Interferes at it would otherwise be found as well
before do
- Project::LifeCycleStep.where(type: Project::Stage.name).destroy_all
+ remove_stage
end
it "returns no project" do