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
implementing `Journal.at_timestamp`
Sebastian Fiedlschuster72238709f4d2
diff --git a/app/models/journal.rb b/app/models/journal.rbindex 600a0d6defe..1576713bdd6 100644--- a/app/models/journal.rb+++ b/app/models/journal.rb@@ -32,6 +32,7 @@ class Journal < ApplicationRecordinclude ::JournalChangesinclude ::JournalFormatterinclude ::Acts::Journalized::FormatHooks+ include Journal::Timestampsregister_journal_formatter :diff, OpenProject::JournalFormatter::Diffregister_journal_formatter :attachment, OpenProject::JournalFormatter::Attachmentdiff --git a/app/models/journal/timestamps.rb b/app/models/journal/timestamps.rbnew file mode 100644index 00000000000..beae29c270b--- /dev/null+++ b/app/models/journal/timestamps.rb@@ -0,0 +1,62 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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 Journal::Timestamps+ extend ActiveSupport::Concern++ class_methods do++ # Select all journals that are the most current at the given timestamp+ # for their respective journables.+ #+ # Suppose, all journables have three journals: One for Monday, one for Wednesday,+ # one for Friday.+ #+ # `at_timestamp(before_monday)` will return none+ # `at_timestamp(monday)` will return the Monday journals+ # `at_timestamp(tuesday)` will return the Monday journals+ # `at_timestamp(wednesday)` will return the Wednesday journals+ # `at_timestamp(thursday)` will return the Wednesday journals+ # `at_timestamp(friday)` will return the Friday journals+ #+ # You may use this to find the journal that represents the data state of+ # a journable at a given timestamp.+ #+ def at_timestamp(timestamp)+ raise ArgumentError, "Expected timestamp to be an ActiveSupport::TimeWithZone or DateTime" unless timestamp.kind_of? ActiveSupport::TimeWithZone or timestamp.kind_of? DateTime+ timestamp = timestamp.in_time_zone if timestamp.kind_of? DateTime++ where(id:+ where(created_at: (..timestamp)) \+ .select("distinct on (journable_type, journable_id) id") \+ .reorder(:journable_type, :journable_id, created_at: :desc)+ )+ end++ end+enddiff --git a/spec/models/journal/timestamps_spec.rb b/spec/models/journal/timestamps_spec.rbnew file mode 100644index 00000000000..314df20a010--- /dev/null+++ b/spec/models/journal/timestamps_spec.rb@@ -0,0 +1,177 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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'++describe Journal::Timestamps, type: :model do++ let!(:work_package) { create :work_package }+ let(:journable) { work_package }++ describe ".at_timestamp" do+ before { @timestamp = Time.zone.now }+ subject { Journal.at_timestamp(@timestamp) }++ it "returns an active-record relation" do+ expect(subject).to be_kind_of ActiveRecord::Relation+ end++ describe "when appended to a where clause" do+ subject { Journal.where(user_id: work_package.author_id).at_timestamp(@timestamp) }+ it "returns an active-record relation" do+ expect(subject).to be_kind_of ActiveRecord::Relation+ end+ end+ describe "when prepended to a where clause" do+ subject { Journal.at_timestamp(@timestamp).where(user_id: work_package.author_id) }+ it "returns an active-record relation" do+ expect(subject).to be_kind_of ActiveRecord::Relation+ end+ end++ describe "when the given timestamp is nil" do+ before { @timestamp = nil }+ it "raises an error" do+ expect { subject }.to raise_error ArgumentError+ end+ end+ describe "when the given timestamp is nonsense" do+ before { @timestamp = "FOO BAR" }+ it "raises an error" do+ expect { subject }.to raise_error ArgumentError+ end+ end+ describe "when the given timestamp is given as DateTime" do+ before { @timestamp = DateTime.current }+ it "raises no error" do+ expect { subject }.not_to raise_error+ end+ end+ describe "when the given timestamp is given as ActiveSupport::TimeWithZone" do+ before { @timestamp = Time.zone.now }+ it "raises no error" do+ expect(@timestamp).to be_kind_of ActiveSupport::TimeWithZone+ expect { subject }.not_to raise_error+ end+ end++ describe "when there are journals for Monday, Wednesday, and Friday" do+ before do+ @before_monday = "2022-01-01".to_datetime+ @monday = "2022-08-01".to_datetime+ @tuesday = "2022-08-02".to_datetime+ @wednesday = "2022-08-03".to_datetime+ @thursday = "2022-08-04".to_datetime+ @friday = "2022-08-05".to_datetime+ work_package.journals.destroy_all+ @monday_journal = create :work_package_journal, journable: work_package, created_at: @monday+ @wednesday_journal = create :work_package_journal, journable: work_package, created_at: @wednesday+ @friday_journal = create :work_package_journal, journable: work_package, created_at: @friday+ end++ describe ".at_timestamp(something before Monday)" do+ before { @timestamp = @before_monday }+ it "returns [] because before Monday, no journal exists" do+ expect(subject).to eq []+ end+ end+ describe ".at_timestamp(Monday)" do+ before { @timestamp = @monday }+ it "returns only the Monday journal because this is the most current on Monday" do+ expect(subject).to eq [@monday_journal]+ end+ end+ describe ".at_timestamp(Tuesday)" do+ before { @timestamp = @tuesday }+ it "returns only the Monday journal because this is the most current on Tuesday" do+ expect(subject).to eq [@monday_journal]+ end+ end+ describe ".at_timestamp(Wednesday)" do+ before { @timestamp = @wednesday }+ it "returns only the Wednesday journal because this is the most current on Wednesday" do+ expect(subject).to eq [@wednesday_journal]+ end+ end+ describe ".at_timestamp(Thursday)" do+ before { @timestamp = @thursday }+ it "returns only the Wednesday journal because this is the most current on Thursday" do+ expect(subject).to eq [@wednesday_journal]+ end+ end+ describe ".at_timestamp(Friday)" do+ before { @timestamp = @friday }+ it "returns only the Friday journal because this is the most current on Friday" do+ expect(subject).to eq [@friday_journal]+ end+ end++ describe "when at_timestamp is called on an association" do+ subject { journable.journals.at_timestamp(@timestamp) }++ describe ".at_timestamp(something before Monday)" do+ before { @timestamp = @before_monday }+ it "returns [] because before Monday, no journal exists" do+ expect(subject).to eq []+ end+ end+ describe ".at_timestamp(Monday)" do+ before { @timestamp = @monday }+ it "returns only the Monday journal because this is the most current on Monday" do+ expect(subject).to eq [@monday_journal]+ end+ end+ describe ".at_timestamp(Tuesday)" do+ before { @timestamp = @tuesday }+ it "returns only the Monday journal because this is the most current on Tuesday" do+ expect(subject).to eq [@monday_journal]+ end+ end+ describe ".at_timestamp(Wednesday)" do+ before { @timestamp = @wednesday }+ it "returns only the Wednesday journal because this is the most current on Wednesday" do+ expect(subject).to eq [@wednesday_journal]+ end+ end+ describe ".at_timestamp(Thursday)" do+ before { @timestamp = @thursday }+ it "returns only the Wednesday journal because this is the most current on Thursday" do+ expect(subject).to eq [@wednesday_journal]+ end+ end+ describe ".at_timestamp(Friday)" do+ before { @timestamp = @friday }+ it "returns only the Friday journal because this is the most current on Friday" do+ expect(subject).to eq [@friday_journal]+ end+ end+ end+ end++ end+end