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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
introduce `this week` operator on `AnyStageOrGateFilter`
ulferts15423e8300ec
diff --git a/app/models/queries/operators/this_week.rb b/app/models/queries/operators/this_week.rbindex 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::Operatorsdef self.begin_of_weekif 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_dayelse- Time.now.at_beginning_of_week - 1.day+ Time.current.at_beginning_of_week - 1.dayendelse# week starts on monday (Rails default)- Time.now.at_beginning_of_week+ Time.current.at_beginning_of_weekendendenddiff --git a/app/models/queries/projects/filters/any_stage_or_gate_filter.rb b/app/models/queries/projects/filters/any_stage_or_gate_filter.rbindex 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::Filtdef 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::Filtenddef where- case operator- when "=d"+ case operator.to_sym+ when Queries::Operators::OnDate.to_symstage_where_on(parsed_start).or(gate_where(parsed_end)).arel.exists- when "t"+ when Queries::Operators::Today.to_symstage_where_on(today).or(gate_where(today, today)).arel.exists- when "<>d"+ when Queries::Operators::BetweenDate.to_symstage_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+ .existselseraise "Unknown operator #{operator}"end@@ -74,7 +80,7 @@ class Queries::Projects::Filters::AnyStageOrGateFilter < Queries::Projects::Filtdef 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::Filtdef 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::Filtdef 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_startvalues.first.present? ? Date.parse(values.first) : nilenddiff --git a/spec/features/projects/lists/filters_spec.rb b/spec/features/projects/lists/filters_spec.rbindex 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)endenddiff --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.rbindex 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" doshared_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) docreate(:project, name: "Project with stage") do |project|create(:project_stage, project:, start_date: stage_start_date, end_date: stage_end_date)endend- shared_let(:gate_date) { Time.zone.today + 10.days }+ shared_let(:gate_date) { Date.parse("2025-02-22") }shared_let(:project_with_gate) docreate(: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" docurrent_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" dobefore doinstance.where("any_stage_or_gate", "=d", values)@@ -130,6 +138,7 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" doend# TODO: check active state+ # TODO: check permissionscontext "with a t (today) operator" dobefore do@@ -199,6 +208,104 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" doendend+ 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" dobefore doinstance.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 wellbefore do- Project::LifeCycleStep.where(type: Project::Gate.name).destroy_all+ remove_gateendit "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 wellbefore do- Project::LifeCycleStep.where(type: Project::Gate.name).destroy_all+ remove_gateendit "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 wellbefore do- Project::LifeCycleStep.where(type: Project::Gate.name).destroy_all+ remove_gateendit "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 wellbefore do- Project::LifeCycleStep.where(type: Project::Stage.name).destroy_all+ remove_stageendit "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 wellbefore do- Project::LifeCycleStep.where(type: Project::Stage.name).destroy_all+ remove_stageendit "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 wellbefore do- Project::LifeCycleStep.where(type: Project::Stage.name).destroy_all+ remove_stageendit "returns no project" do