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
Project::Phase.covering_dates_and_days_of_week adapted from WorkPackage with hackyily adapted test
Ivan Kuchin66e85756182b
diff --git a/app/models/project/phase.rb b/app/models/project/phase.rbindex 7bb0fca3ce9..337d9f9f235 100644--- a/app/models/project/phase.rb+++ b/app/models/project/phase.rb@@ -51,7 +51,8 @@ class Project::Phase < ApplicationRecordattr_readonly :definition_idscope :active, -> { where(active: true) }- scopes :order_by_position+ scopes :order_by_position,+ :covering_dates_and_days_of_weekclass << selfdef visible(user = User.current)diff --git a/app/models/project/phases/scopes/covering_dates_and_days_of_week.rb b/app/models/project/phases/scopes/covering_dates_and_days_of_week.rbnew file mode 100644index 00000000000..74c7e1a0e10--- /dev/null+++ b/app/models/project/phases/scopes/covering_dates_and_days_of_week.rb@@ -0,0 +1,88 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module Project::Phases::Scopes::CoveringDatesAndDaysOfWeek+ extend ActiveSupport::Concern+ using CoreExtensions::SquishSql++ class_methods do+ # Fetches all phases that cover specific days of the week, and/or specific dates.+ #+ # The period considered is from the phase start date to the due date.+ #+ # @param dates Date[] An array of the Date objects.+ # @param days_of_week number[] An array of the ISO days of the week to+ # consider. 1 is Monday, 7 is Sunday.+ def covering_dates_and_days_of_week(days_of_week: [], dates: [])+ days_of_week = Array(days_of_week)+ dates = Array(dates)+ return none if days_of_week.empty? && dates.empty?++ ids_sql = sanitize_sql([<<~SQL.squish, { days_of_week:, dates: }])+ WITH+ -- select phases with at least one date+ phases_with_dates AS (+ SELECT+ id,+ COALESCE(start_date, finish_date) AS start_date,+ COALESCE(finish_date, start_date) AS finish_date+ FROM project_phases+ WHERE+ start_date IS NOT NULL+ OR finish_date IS NOT NULL+ ),++ -- generate a row for every date between start_date and finish_date+ covered_dates AS (+ SELECT+ id,+ generate_series(start_date, finish_date, '1 day') AS date+ FROM phases_with_dates+ ),++ -- add day of the week column+ covered_dates_and_wday AS (+ SELECT+ id,+ date,+ EXTRACT(isodow FROM date) AS dow+ FROM covered_dates+ )++ -- select id of phases covering the given days+ SELECT id+ FROM covered_dates_and_wday+ WHERE dow IN (:days_of_week) OR date IN (:dates)+ SQL++ where("id IN (#{ids_sql})")+ end+ end+enddiff --git a/spec/models/project/phases/scopes/covering_dates_and_days_of_week_spec.rb b/spec/models/project/phases/scopes/covering_dates_and_days_of_week_spec.rbnew file mode 100644index 00000000000..d8aefbb5fb6--- /dev/null+++ b/spec/models/project/phases/scopes/covering_dates_and_days_of_week_spec.rb@@ -0,0 +1,156 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require "rails_helper"++RSpec.describe Project::Phases::Scopes::CoveringDatesAndDaysOfWeek do+ # Constructs the keyword arguments for the `#covering_dates_and_days_of_week` method.+ # It's a `{ days_of_week:, dates: }` hash for given days of week built differently+ # depending on the `days_args_strategy` value.+ #+ # `days_args_strategy` can take the following values:+ # - `:days_of_week_only`: returns `{ days_of_week: ..., dates: [] }` so it+ # only contains days of week+ # - `:dates_only`: returns `{ days_of_week: [], dates: ... }` so it only+ # contains specific dates for the specified days of weeks over the next 2+ # weeks+ # - `:mixed`: returns `{ days_of_week: ..., dates: ... }` so it contains a mix of days+ # of week and specific dates: Monday, Wednesday, Friday and Sunday are days of week, and+ # Tuesday, Thursday and Saturday are dates+ def day_args(*days_of_week_as_symbols)+ next_monday = Date.current.next_occurring(:monday)+ values = days_of_week_as_symbols.map { |dow| next_monday.next_occurring(dow.to_sym) }+ .flat_map { |day| [day, day + 7.days] }++ case days_args_strategy+ when :days_of_week_only+ days_of_week = values.map(&:cwday).uniq+ dates = []+ when :dates_only+ days_of_week = []+ dates = values+ when :mixed+ # Monday, Wednesday, Friday and Sunday as days of week+ # Tuesday, Thursday and Saturday as dates+ days_of_week = values.map(&:cwday).uniq.filter(&:odd?)+ dates = values.filter { |day| day.cwday.even? }+ end+ { days_of_week:, dates: }+ end++ def create_phases_from_table(representation)+ # TODO: less hacky way to adapt tables for work packages to create phases+ TableHelpers::TableParser.new.parse(representation).to_h do |entry|+ entry => { attributes: { subject:, start_date:, due_date: } }+ [+ subject,+ create(:project_phase, start_date:, finish_date: due_date)+ ]+ end+ end++ shared_context "with the days of week" do+ let(:days_args_strategy) { :days_of_week_only }+ end++ shared_context "with specific dates" do+ let(:days_args_strategy) { :dates_only }+ end++ shared_context "with days of week and specific dates mixed" do+ let(:days_args_strategy) { :mixed }+ end++ for_each_context "with the days of week",+ "with specific dates",+ "with days of week and specific dates mixed" do+ describe "#covering_dates_and_days_of_week" do+ it "returns phases having start date or due date being in the given days of week" do+ table =+ create_phases_from_table(<<~TABLE)+ subject | MTWTFSS |+ covered1 | XX |+ covered2 | XX |+ covered3 | X |+ covered4 | [ |+ covered5 | ] |+ not_covered1 | X |+ not_covered2 | X |+ not_covered3 | XX |+ not_covered4 | |+ TABLE++ expect(Project::Phase.covering_dates_and_days_of_week(**day_args(:tuesday)))+ .to contain_exactly(+ table["covered1"],+ table["covered2"],+ table["covered3"],+ table["covered4"],+ table["covered5"]+ )+ end++ it "returns phases having days between start date and due date being in the given days of week" do+ table =+ create_phases_from_table(<<~TABLE)+ subject | MTWTFSS |+ covered1 | XXXX |+ covered2 | XXX |+ not_covered1 | XX |+ not_covered2 | X |+ TABLE++ expect(Project::Phase.covering_dates_and_days_of_week(**day_args(:tuesday, :wednesday)))+ .to contain_exactly(+ table["covered1"],+ table["covered2"]+ )+ end++ it "accepts a single day of week or an array of days" do+ table =+ create_phases_from_table(<<~TABLE)+ subject | MTWTFSS |+ covered | X |+ not_covered | X |+ TABLE++ single_value = day_args(:tuesday).transform_values { |v| Array(v).first }++ expect(Project::Phase.covering_dates_and_days_of_week(**single_value))+ .to eq([table["covered"]])+ expect(Project::Phase.covering_dates_and_days_of_week(**day_args(:tuesday)))+ .to eq([table["covered"]])+ expect(Project::Phase.covering_dates_and_days_of_week(**day_args(:tuesday, :wednesday)))+ .to eq([table["covered"]])+ end+ end+ end+end