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

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.rb
index 7861b206d66..b8f8ad62872 100644
--- a/app/models/concerns/tableless.rb
+++ b/app/models/concerns/tableless.rb
@@ -33,6 +33,10 @@ module Tableless
false
end
+ def readonly?
+ true
+ end
+
class_methods do
def attribute_names
@attribute_names ||= attribute_types.keys
diff --git a/app/models/day.rb b/app/models/day.rb
index ab62e303f27..c5f0a6b9f2c 100644
--- a/app/models/day.rb
+++ b/app/models/day.rb
@@ -39,10 +39,12 @@ class Day < ApplicationRecord
inverse_of: false,
class_name: 'NonWorkingDay',
foreign_key: :date,
- primary_key: :date
+ primary_key: :date,
+ dependent: nil
attribute :date, :date, default: nil
attribute :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 < ApplicationRecord
today = Time.zone.today
from = today.at_beginning_of_month
to = 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)
end
def self.from_sql(from:, to:)
@@ -62,23 +64,17 @@ class Day < ApplicationRecord
(SELECT
to_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 working
FROM
generate_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
) days
SQL
end
-
- 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
- end
end
diff --git a/spec/models/day_spec.rb b/spec/models/day_spec.rb
index 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 do
let(: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)
+ end
it { is_expected.to be_readonly }
it { is_expected.to respond_to :id }
@@ -67,7 +72,7 @@ describe Day, type: :model do
context 'with a non-working day' do
before do
- create(:non_working_day, date: subject.date)
+ create(:non_working_day, date: first_of_year)
end
it 'is false' do
@@ -87,7 +92,7 @@ describe Day, type: :model do
context 'with a non working day' do
before do
- create(:non_working_day, date: subject.date)
+ create(:non_working_day, date: first_of_year)
end
it 'is false' do
diff --git a/spec/requests/api/v3/days/day_spec.rb b/spec/requests/api/v3/days/day_spec.rb
index 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'
+ end
end
end