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
fixup! Add non-working days to the api/v3/days
Dombi Attila5a1d6aee3a08
diff --git a/app/models/concerns/tableless.rb b/app/models/concerns/tableless.rbindex 7861b206d66..b8f8ad62872 100644--- a/app/models/concerns/tableless.rb+++ b/app/models/concerns/tableless.rb@@ -33,6 +33,10 @@ module Tablelessfalseend+ def readonly?+ true+ end+class_methods dodef attribute_names@attribute_names ||= attribute_types.keysdiff --git a/app/models/day.rb b/app/models/day.rbindex ab62e303f27..c5f0a6b9f2c 100644--- a/app/models/day.rb+++ b/app/models/day.rb@@ -39,10 +39,12 @@ class Day < ApplicationRecordinverse_of: false,class_name: 'NonWorkingDay',foreign_key: :date,- primary_key: :date+ primary_key: :date,+ dependent: nilattribute :date, :date, default: nilattribute :day_of_week, :integer, default: nil+ attribute :working, :boolean, default: 't'delegate :name, to: :week_day, allow_nil: true@@ -50,11 +52,11 @@ class Day < ApplicationRecordtoday = Time.zone.todayfrom = today.at_beginning_of_monthto = today.next_month.at_end_of_month+ from_range(from:, to:).includes(:week_day).includes(:non_working_days).order(:date)+ end+ def self.from_range(from:, to:)from(Arel.sql(from_sql(from:, to:)))- .includes(:week_day)- .includes(:non_working_days)- .order(:date)enddef self.from_sql(from:, to:)@@ -62,23 +64,17 @@ class Day < ApplicationRecord(SELECTto_char(dd, 'YYYYMMDD')::integer id,date_trunc('day', dd)::date date,- extract(isodow from dd) day_of_week+ extract(isodow from dd) day_of_week,+ (COALESCE(week_days.working, TRUE) AND non_working_days.id IS NULL)::bool workingFROMgenerate_series( '#{from}'::timestamp,'#{to}'::timestamp,'1 day'::interval) dd+ LEFT JOIN week_days+ ON extract(isodow from dd) = week_days.day+ LEFT JOIN non_working_days+ ON date = non_working_days.date) daysSQLend-- def working- week_day&.working && non_working_days.empty?- end-- ##- # Since the base table is a generated series of dates that cannot be modified- # we should mark the records readonly.- def readonly?- true- endenddiff --git a/spec/models/day_spec.rb b/spec/models/day_spec.rbindex 2df16d929de..1a2ecc7c13c 100644--- a/spec/models/day_spec.rb+++ b/spec/models/day_spec.rb@@ -2,8 +2,13 @@ require 'spec_helper'describe Day, type: :model dolet(:today) { Date.current }+ let(:first_of_year) { Date.new(2022, 1, 1) }- subject { described_class.new(date: Date.new(2022, 1, 1), day_of_week: 6) }+ subject do+ described_class+ .from_range(from: Date.new(2022, 1, 1), to: Date.new(2022, 2, 1))+ .find(first_of_year.strftime("%Y%m%d").to_i)+ endit { is_expected.to be_readonly }it { is_expected.to respond_to :id }@@ -67,7 +72,7 @@ describe Day, type: :model docontext 'with a non-working day' dobefore do- create(:non_working_day, date: subject.date)+ create(:non_working_day, date: first_of_year)endit 'is false' do@@ -87,7 +92,7 @@ describe Day, type: :model docontext 'with a non working day' dobefore do- create(:non_working_day, date: subject.date)+ create(:non_working_day, date: first_of_year)endit 'is false' dodiff --git a/spec/requests/api/v3/days/day_spec.rb b/spec/requests/api/v3/days/day_spec.rbindex f86699dd0ea..03108ed9942 100644--- a/spec/requests/api/v3/days/day_spec.rb+++ b/spec/requests/api/v3/days/day_spec.rb@@ -56,5 +56,16 @@ describe ::API::V3::Days::DaysAPI,it_behaves_like 'API V3 collection response', 6, 6, 'Day'end++ context 'when filtering by working' do+ let(:filters) do+ [{ working: { operator: '=',+ values: ['t'] } }]+ end++ nb_days = (Time.zone.today.at_beginning_of_month..Time.zone.today.next_month.at_end_of_month)+ .count { |d| !(d.saturday? || d.sunday?) }+ it_behaves_like 'API V3 collection response', nb_days, nb_days, 'Day'+ endendend