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 weekday resource link to api/v3/days

Dombi Attila75dab909f662
diff --git a/app/models/day.rb b/app/models/day.rb
index c5f0a6b9f2c..8dea0bd74b3 100644
--- a/app/models/day.rb
+++ b/app/models/day.rb
@@ -52,7 +52,10 @@ 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)
+ from_range(from:, to:)
+ .includes(:week_day)
+ .includes(:non_working_days)
+ .order("days.id")
end
def self.from_range(from:, to:)
@@ -73,7 +76,7 @@ class Day < ApplicationRecord
LEFT JOIN week_days
ON extract(isodow from dd) = week_days.day
LEFT JOIN non_working_days
- ON date = non_working_days.date
+ ON dd = non_working_days.date
) days
SQL
end
diff --git a/spec/models/day_spec.rb b/spec/models/day_spec.rb
index 1a2ecc7c13c..3230df8a119 100644
--- a/spec/models/day_spec.rb
+++ b/spec/models/day_spec.rb
@@ -2,13 +2,11 @@ require 'spec_helper'
describe Day, type: :model do
let(:today) { Date.current }
- let(:first_of_year) { Date.new(2022, 1, 1) }
+ let(:date_range) { Date.new(2022, 1, 1)..Date.new(2022, 2, 1) }
+ let(:first_of_year) { date_range.begin }
+ let(:days) { described_class.from_range(from: date_range.begin, to: date_range.end) }
- 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
+ subject { days.find(first_of_year.strftime("%Y%m%d").to_i) }
it { is_expected.to be_readonly }
it { is_expected.to respond_to :id }
@@ -16,7 +14,7 @@ describe Day, type: :model do
it { is_expected.to respond_to :day_of_week }
it { is_expected.to respond_to :name }
- context 'with a collection' do
+ context 'with default_scope' do
let(:days) { described_class.default_scope }
it 'returns a default date range' do
@@ -50,6 +48,35 @@ describe Day, type: :model do
end
end
+ context 'for collection with multiple non-working days' do
+ let(:non_working_dates) { [date_range.begin, date_range.begin + 1.day] }
+
+ before do
+ create(:week_days)
+ non_working_dates.each { |date| create(:non_working_day, date:) }
+ end
+
+ it 'returns the correct number of days' do
+ expect(days.count).to eq(date_range.count)
+ end
+
+ it 'returns the dates included in the date_range' do
+ expect(days.collect(&:date)).to eq(date_range.to_a)
+ end
+
+ it 'returns working false for weekends and non_working_days' do
+ expected_working_states = date_range.map do |day|
+ !(day.saturday? || day.sunday? || day.in?(non_working_dates))
+ end
+ expect(days.pluck(:working)).to eq(expected_working_states)
+ end
+
+ it 'returns the correct day_of_week' do
+ expected_days_of_week = date_range.map { |day| Array(1..7)[day.wday - 1] }
+ expect(days.pluck(:day_of_week)).to eq(expected_days_of_week)
+ end
+ end
+
context 'with the weekday present' do
before do
create(:week_day, day: 6)
diff --git a/spec/models/queries/days/day_query_spec.rb b/spec/models/queries/days/day_query_spec.rb
index e52e4b85a34..a8387b43d85 100644
--- a/spec/models/queries/days/day_query_spec.rb
+++ b/spec/models/queries/days/day_query_spec.rb
@@ -30,7 +30,7 @@ require 'spec_helper'
describe Queries::Days::DayQuery, type: :model do
let(:instance) { described_class.new }
- let(:base_scope) { Day.order(date: :asc) }
+ let(:base_scope) { Day.reorder(date: :asc) }
let(:current_user) { build_stubbed(:admin) }
before do
@@ -63,13 +63,14 @@ describe Queries::Days::DayQuery, type: :model do
context 'with dates within the default range' do
let(:from) { Time.zone.today }
let(:to) { 5.days.from_now.to_date }
- let(:base_scope) { Day.from(Day.from_sql(from:, to:)).order(date: :asc) }
+ let(:base_scope) { Day.from_range(from:, to:).reorder(date: :asc) }
it 'is the same as handwriting the query' do
# Expectation has to be weirdly specific to the logic of Queries::Operators::DateRangeClauses
expected = base_scope.where("days.date > ? AND days.date <= ?",
(from - 1.day).end_of_day,
to.end_of_day)
+
expect(instance.results.to_sql).to eql expected.to_sql
end
end