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

extract beginning_of_week calculation into OpenProject::I18n

ulfertsdc856279f10c
diff --git a/app/models/queries/operators/this_week.rb b/app/models/queries/operators/this_week.rb
index 037671fbda6..ad95b78d533 100644
--- a/app/models/queries/operators/this_week.rb
+++ b/app/models/queries/operators/this_week.rb
@@ -33,24 +33,10 @@ module Queries::Operators
require_value false
def self.sql_for_field(_values, db_table, db_field)
- from = begin_of_week
+ from = OpenProject::I18n::Date.time_at_beginning_of_week
"#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [
connection.quoted_date(from), connection.quoted_date(from + 7.days)
]
end
-
- def self.begin_of_week
- if I18n.t(:general_first_day_of_week) == "7"
- # week starts on sunday
- if Time.zone.today.cwday == 7
- Time.current.at_beginning_of_day
- else
- Time.current.at_beginning_of_week - 1.day
- end
- else
- # week starts on monday (Rails default)
- Time.current.at_beginning_of_week
- end
- end
end
end
diff --git a/app/models/queries/projects/filters/any_stage_or_gate_filter.rb b/app/models/queries/projects/filters/any_stage_or_gate_filter.rb
index 8e19d31c0de..517cf545bc5 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
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
# -- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2010-2024 the OpenProject GmbH
diff --git a/lib/open_project/i18n/date.rb b/lib/open_project/i18n/date.rb
new file mode 100644
index 00000000000..3fb263e55c3
--- /dev/null
+++ b/lib/open_project/i18n/date.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2025 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 OpenProject
+ module I18n
+ module Date
+ module_function
+
+ def self.beginning_of_week
+ case (Setting.start_of_week || ::I18n.t(:general_first_day_of_week)).to_i
+ when 1
+ :monday
+ when 7
+ :sunday
+ when 6
+ :saturday
+ else
+ ::Date.beginning_of_week
+ end
+ end
+
+ def time_at_beginning_of_week
+ Time.current.at_beginning_of_week(beginning_of_week)
+ end
+ end
+ end
+end
diff --git a/lib_static/redmine/i18n.rb b/lib_static/redmine/i18n.rb
index a8e0e980deb..443574ed2a3 100644
--- a/lib_static/redmine/i18n.rb
+++ b/lib_static/redmine/i18n.rb
@@ -26,6 +26,9 @@
# See COPYRIGHT and LICENSE files for more details.
#++
+# This file is to be split up into smaller files in the OpenProject namespace.
+# A start has been made by splitting off OpenProject::I18n::Date into its own file.
+
module Redmine
module I18n
include ActionView::Helpers::NumberHelper
@@ -45,19 +48,6 @@ module Redmine
.sort
end
- def self.start_of_week
- case Setting.start_of_week.to_i
- when 1
- :monday
- when 7
- :sunday
- when 6
- :saturday
- else
- Date.beginning_of_week
- end
- end
-
def self.valid_languages
all_languages & (Setting.available_languages + [Setting.default_language])
end
diff --git a/modules/meeting/app/controllers/meetings_controller.rb b/modules/meeting/app/controllers/meetings_controller.rb
index b7cfdf97cab..f0d13e1bf4b 100644
--- a/modules/meeting/app/controllers/meetings_controller.rb
+++ b/modules/meeting/app/controllers/meetings_controller.rb
@@ -372,7 +372,7 @@ class MeetingsController < ApplicationController
def group_meetings(all_meetings) # rubocop:disable Metrics/AbcSize
next_week = Time
.current
- .next_occurring(Redmine::I18n.start_of_week)
+ .next_occurring(OpenProject::I18n::Date.beginning_of_week)
.beginning_of_day
groups = Hash.new { |h, k| h[k] = [] }
groups[:later] = show_more_pagination(all_meetings
diff --git a/modules/meeting/spec/requests/meetings_index_spec.rb b/modules/meeting/spec/requests/meetings_index_spec.rb
index bacded1a352..098bee5a638 100644
--- a/modules/meeting/spec/requests/meetings_index_spec.rb
+++ b/modules/meeting/spec/requests/meetings_index_spec.rb
@@ -185,7 +185,7 @@ RSpec.describe "Meeting index",
end
describe "with start_of_week on sunday",
- with_settings: { start_of_week: 0 } do
+ with_settings: { start_of_week: 7 } do
it "sorts upcoming meetings into buckets" do
expect(subject).to have_http_status(:ok)
diff --git a/modules/meeting/spec/support/pages/meetings/index.rb b/modules/meeting/spec/support/pages/meetings/index.rb
index f4309e3b317..227825732bd 100644
--- a/modules/meeting/spec/support/pages/meetings/index.rb
+++ b/modules/meeting/spec/support/pages/meetings/index.rb
@@ -193,7 +193,7 @@ module Pages::Meetings
def meeting_group_key(meeting)
start_date = meeting.start_time.to_date
- next_week = Time.current.next_occurring(Redmine::I18n.start_of_week)
+ next_week = Time.current.next_occurring(OpenProject::I18n::Date.beginning_of_week)
if start_date == Time.zone.today
:today
diff --git a/spec/lib/open_project/i18n/date_spec.rb b/spec/lib/open_project/i18n/date_spec.rb
new file mode 100644
index 00000000000..d3d75b4ef89
--- /dev/null
+++ b/spec/lib/open_project/i18n/date_spec.rb
@@ -0,0 +1,213 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2025 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 "spec_helper"
+
+RSpec.describe OpenProject::I18n::Date do
+ describe ".beginning_of_week" do
+ context "when the first day of the week is Sunday", with_settings: { start_of_week: 7 } do
+ it "returns :sunday" do
+ expect(described_class.beginning_of_week).to eq(:sunday)
+ end
+ end
+
+ context "when the first day of the week is Monday", with_settings: { start_of_week: 2 } do
+ it "returns :monday" do
+ expect(described_class.beginning_of_week).to eq(:monday)
+ end
+ end
+
+ context "when the first day of the week is Saturday", with_settings: { start_of_week: 6 } do
+ it "returns :monday" do
+ expect(described_class.beginning_of_week).to eq(:saturday)
+ end
+ end
+
+ context "when the first day of the week is not set and I18n states Monday", with_settings: { start_of_week: nil } do
+ before do
+ allow(I18n).to receive(:t).with(:general_first_day_of_week).and_return("1")
+ end
+
+ it "returns :monday" do
+ expect(described_class.beginning_of_week).to eq(:monday)
+ end
+ end
+
+ context "when the first day of the week is not set and I18n states Sunday", with_settings: { start_of_week: nil } do
+ before do
+ allow(I18n).to receive(:t).with(:general_first_day_of_week).and_return("7")
+ end
+
+ it "returns :sunday" do
+ expect(described_class.beginning_of_week).to eq(:sunday)
+ end
+ end
+ end
+
+ describe ".time_at_beginning_of_week" do
+ context "when the first day of the week is Sunday", with_settings: { start_of_week: 7 } do
+ context "when today is Sunday" do
+ it "returns the beginning of the day" do
+ Timecop.travel(DateTime.parse("2025-02-16").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-16").beginning_of_day)
+ end
+ end
+ end
+
+ context "when today is Monday" do
+ it "returns the beginning of Sunday" do
+ Timecop.travel(DateTime.parse("2025-02-10").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-09").beginning_of_day)
+ end
+ end
+ end
+
+ context "when today is Saturday" do
+ it "returns the beginning of Sunday" do
+ Timecop.travel(DateTime.parse("2025-02-15").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-09").beginning_of_day)
+ end
+ end
+ end
+ end
+
+ context "when the first day of the week is Monday", with_settings: { start_of_week: 1 } do
+ context "when today is Sunday" do
+ it "returns the beginning of the Monday" do
+ Timecop.travel(DateTime.parse("2025-02-16").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-10").beginning_of_day)
+ end
+ end
+ end
+
+ context "when today is Monday" do
+ it "returns the beginning of the day" do
+ Timecop.travel(DateTime.parse("2025-02-10").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-10").beginning_of_day)
+ end
+ end
+ end
+
+ context "when today is Saturday" do
+ it "returns the beginning of Monday" do
+ Timecop.travel(DateTime.parse("2025-02-15").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-10").beginning_of_day)
+ end
+ end
+ end
+ end
+
+ context "when the first day of the week is Saturday", with_settings: { start_of_week: 6 } do
+ context "when today is Sunday" do
+ it "returns the beginning of the Monday" do
+ Timecop.travel(DateTime.parse("2025-02-16").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-15").beginning_of_day)
+ end
+ end
+ end
+
+ context "when today is Monday" do
+ it "returns the beginning of the day" do
+ Timecop.travel(DateTime.parse("2025-02-10").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-08").beginning_of_day)
+ end
+ end
+ end
+
+ context "when today is Saturday" do
+ it "returns the beginning of Monday" do
+ Timecop.travel(DateTime.parse("2025-02-15").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-15").beginning_of_day)
+ end
+ end
+ end
+ end
+
+ context "when the first day of the week is not set but language states '7'", with_settings: { start_of_week: nil } do
+ before do
+ allow(I18n).to receive(:t).with(:general_first_day_of_week).and_return("7")
+ end
+
+ context "when today is Sunday" do
+ it "returns the beginning of the day" do
+ Timecop.travel(DateTime.parse("2025-02-16").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-16").beginning_of_day)
+ end
+ end
+ end
+
+ context "when today is Monday" do
+ it "returns the beginning of Sunday" do
+ Timecop.travel(DateTime.parse("2025-02-10").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-09").beginning_of_day)
+ end
+ end
+ end
+
+ context "when today is Saturday" do
+ it "returns the beginning of Sunday" do
+ Timecop.travel(DateTime.parse("2025-02-15").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-09").beginning_of_day)
+ end
+ end
+ end
+ end
+
+ context "when the first day of the week is not set but language states '1'", with_settings: { start_of_week: nil } do
+ before do
+ allow(I18n).to receive(:t).with(:general_first_day_of_week).and_return("1")
+ end
+
+ context "when today is Sunday" do
+ it "returns the beginning of the Monday" do
+ Timecop.travel(DateTime.parse("2025-02-16").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-10").beginning_of_day)
+ end
+ end
+ end
+
+ context "when today is Monday" do
+ it "returns the beginning of the day" do
+ Timecop.travel(DateTime.parse("2025-02-10").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-10").beginning_of_day)
+ end
+ end
+ end
+
+ context "when today is Saturday" do
+ it "returns the beginning of Monday" do
+ Timecop.travel(DateTime.parse("2025-02-15").noon) do
+ expect(described_class.time_at_beginning_of_week).to eq(Time.zone.parse("2025-02-10").beginning_of_day)
+ end
+ end
+ end
+ end
+ end
+end