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
use validity_period for at_timestamp filtering
ulfertsf9ddf9a84369
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rbindex 98c6394acef..f1d31c2cb0f 100644--- a/app/models/journable/historic_active_record_relation.rb+++ b/app/models/journable/historic_active_record_relation.rb@@ -61,7 +61,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relationinstance_variable_set key, relation.instance_variable_get(key)end- self.timestamp = timestamp+ self.timestamp = Array(timestamp)readonly!instance_variable_set :@table, model.journal_class.arel_tableend@@ -240,7 +240,12 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation#def add_timestamp_condition(relation)relation.joins_values = [journals_join_statement] + relation.joins_values- relation.merge(Journal.where(journable_type: model.name).at_timestamp(timestamp))++ timestamp_condition = Array(timestamp).map do |t|+ Journal.where(journable_type: model.name).at_timestamp(t)+ end.reduce(&:or)++ relation.merge(timestamp_condition)enddef journals_join_statement@@ -311,7 +316,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation"journals.journable_id as id","journables.created_at as created_at","journals.updated_at as updated_at",- "'#{timestamp}' as timestamp"+ "CASE #{timestamp_case_when_statements} END as timestamp"] + \model.column_names_missing_in_journal.collect do |missing_column_name|"null as #{missing_column_name}"@@ -377,6 +382,23 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relationsql_string.gsub! "\"#{CustomValue.table_name}\".", "\"#{Journal::CustomizableJournal.table_name}\"."end+ def timestamp_case_when_statements+ timestamp+ .map do |timestamp|+ comparison_time = case timestamp+ when Timestamp+ timestamp.to_time+ when DateTime+ timestamp.in_time_zone+ else+ raise NotImplementedError, "Unknown timestamp type: #{timestamp.class}"+ end++ "WHEN \"journals\".\"validity_period\" @> timestamp with time zone '#{comparison_time}' THEN '#{timestamp}'"+ end+ .join(" ")+ end+class NotImplementedError < StandardError; endenddiff --git a/app/models/journable/timestamps.rb b/app/models/journable/timestamps.rbindex 53fb992c5b5..7aeb3f89007 100644--- a/app/models/journable/timestamps.rb+++ b/app/models/journable/timestamps.rb@@ -84,7 +84,7 @@ module Journable::Timestampsendend- # Instantiates a journable with historic data from the given timestap.+ # Instantiates a journable with historic data from the given timestamp.## WorkPackage.find(1).at_timestamp(1.year.ago)#diff --git a/app/models/journal/timestamps.rb b/app/models/journal/timestamps.rbindex 56df58db676..5dd909f0c2d 100644--- a/app/models/journal/timestamps.rb+++ b/app/models/journal/timestamps.rb@@ -70,9 +70,7 @@ module Journal::Timestampstimestamp = timestamp.to_time if timestamp.kind_of? Timestamptimestamp = 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))+ where(['validity_period @> timestamp with time zone ?', timestamp])endendenddiff --git a/app/models/query/results.rb b/app/models/query/results.rbindex 6c6b821da04..5afdaa0166c 100644--- a/app/models/query/results.rb+++ b/app/models/query/results.rb@@ -51,12 +51,7 @@ class ::Query::Resultsdef sorted_work_packages_matching_the_filters_todaysorted_work_packages.visible- .merge(filtered_work_packages_matching_the_filters_today)- end-- def sorted_work_packages_matching_the_filters_at_any_of_the_given_timestamps- sorted_work_packages- .where(id: work_packages_matching_the_filters_at_any_of_the_given_timestamps)+ .merge(filtered_work_packages.merge(filter_merges))end# For filtering on historic data, this returns the work packages@@ -65,11 +60,9 @@ class ::Query::Results# concatenation that means that a user has to have no permission to see a work package# at any of the timestamps. This has to be used with care. Callers will have to# ensure to not reveal information.- def work_packages_matching_the_filters_at_any_of_the_given_timestamps- query.timestamps.collect do |timestamp|- WorkPackage- .where(id: filtered_work_packages.visible.at_timestamp(timestamp))- end.reduce(:or)+ def sorted_work_packages_matching_the_filters_at_any_of_the_given_timestamps+ sorted_work_packages+ .where(id: filtered_work_packages.visible.at_timestamp(query.timestamps))end# Returns an active-record relation that applies the filters to find the matching@@ -86,10 +79,6 @@ class ::Query::Results.where(query.statement)end- def filtered_work_packages_matching_the_filters_today- filtered_work_packages.merge(filter_merges)- end-def sorted_work_packageswork_package_scope.joins(sort_criteria_joins)diff --git a/spec/factories/work_package_factory.rb b/spec/factories/work_package_factory.rbindex 614211c31bd..397942c1592 100644--- a/spec/factories/work_package_factory.rb+++ b/spec/factories/work_package_factory.rb@@ -31,6 +31,7 @@ FactoryBot.define dotransient docustom_values { nil }days { WorkPackages::Shared::Days.for(self) }+ journals { nil }endpriority@@ -90,5 +91,26 @@ FactoryBot.define dowp.type = wp.project.types.firstendend++ callback(:after_create) do |work_package, evaluator|+ if evaluator.journals.present?+ work_package.journals.destroy_all++ evaluator.journals.each do |timestamp, attributes|+ work_package_attributes = work_package.attributes.except("id")+ journal_attributes = work_package_attributes \+ .extract!(*Journal::WorkPackageJournal.attribute_names) \+ .symbolize_keys.merge(attributes)+ create(:work_package_journal,+ journable: work_package,+ created_at: timestamp,+ updated_at: timestamp,+ data: build(:journal_work_package_journal, journal_attributes))+ end++ work_package.update_columns(created_at: work_package.journals.minimum(:created_at),+ updated_at: work_package.journals.maximum(:updated_at))+ end+ endendenddiff --git a/spec/models/journable/timestamps_spec.rb b/spec/models/journable/timestamps_spec.rbindex e9ca790ffd4..3401dac8ab0 100644--- a/spec/models/journable/timestamps_spec.rb+++ b/spec/models/journable/timestamps_spec.rb@@ -39,42 +39,26 @@ RSpec.describe Journable::Timestamps dolet(:friday) { "2022-08-05".to_datetime }let!(:work_package) do- new_work_package = create(:work_package, description: "The work package as it is since Friday", estimated_hours: 10)- new_work_package.update_columns created_at: monday- new_work_package+ create(:work_package,+ description: "The work package as it is since Friday",+ estimated_hours: 10,+ journals: {+ monday => { description: "The work package as it has been on Monday", estimated_hours: 5 },+ wednesday => { description: "The work package as it has been on Wednesday", estimated_hours: 10 },+ friday => { description: "The work package as it is since Friday", estimated_hours: 15 }+ })endlet(:journable) { work_package }describe "when there are journals for Monday, Wednesday, and Friday" do- def create_journal(journable:, timestamp:, attributes: {})- work_package_attributes = work_package.attributes.except("id")- journal_attributes = work_package_attributes \- .extract!(*Journal::WorkPackageJournal.attribute_names) \- .symbolize_keys.merge(attributes)- create(:work_package_journal,- journable:, created_at: timestamp, updated_at: timestamp,- data: build(:journal_work_package_journal, journal_attributes))- end-let(:monday_journal) do- create_journal(journable: work_package, timestamp: monday,- attributes: { description: "The work package as it has been on Monday", estimated_hours: 5 })+ work_package.journals.find_by(created_at: monday)endlet(:wednesday_journal) do- create_journal(journable: work_package, timestamp: wednesday,- attributes: { description: "The work package as it has been on Wednesday", estimated_hours: 10 })+ work_package.journals.find_by(created_at: wednesday)endlet(:friday_journal) do- create_journal(journable: work_package, timestamp: friday,- attributes: { description: "The work package as it is since Friday", estimated_hours: 10 })- end-- before do- work_package.journals.destroy_all- monday_journal- wednesday_journal- friday_journal- work_package.reload+ work_package.journals.find_by(created_at: friday)enddescribe ".at_timestamp" do@@ -100,6 +84,10 @@ RSpec.describe Journable::Timestamps doexpect(subject.first.readonly?).to be trueend+ it 'adds a `timestamp` property to the returned work packages' do+ expect(subject.first.timestamp).to eq timestamp.iso8601+ end+it "returns the records with the journable id rather than the id of the journal record" doexpect(subject.pluck(:id)).to eq [work_package.id]expect(subject.first.id).to eq work_package.id@@ -158,6 +146,36 @@ RSpec.describe Journable::Timestamps doendend+ describe ".at_timestamp(Monday and Friday)" do+ let(:timestamp) { [monday, friday] }++ it "returns the work packages two times, in their state on Monday and the one on Friday", :aggregate_failures do+ expect(subject.length)+ .to eq 2++ expect(subject.find { |wp| wp.timestamp == monday.iso8601 }.description)+ .to eq "The work package as it has been on Monday"++ expect(subject.find { |wp| wp.timestamp == friday.iso8601 }.description)+ .to eq "The work package as it is since Friday"+ end+ end++ describe ".at_timestamp(Monday and PT0S)" do+ let(:timestamp) { [monday, Timestamp.now] }++ it "returns the work packages two times, in their state on Monday and the one on Friday", :aggregate_failures do+ expect(subject.length)+ .to eq 2++ expect(subject.find { |wp| wp.timestamp == monday.iso8601 }.description)+ .to eq "The work package as it has been on Monday"++ expect(subject.find { |wp| wp.timestamp == 'PT0S' }.description)+ .to eq "The work package as it is since Friday"+ end+ end+describe "when filtering on a column that only matches in the past" dolet(:relation) { WorkPackage.where(description: "The work package as it has been on Monday") }diff --git a/spec/models/journable/with_historic_attributes_spec.rb b/spec/models/journable/with_historic_attributes_spec.rbindex 6ab328a3bd0..d0f2a31acd0 100644--- a/spec/models/journable/with_historic_attributes_spec.rb+++ b/spec/models/journable/with_historic_attributes_spec.rb@@ -105,12 +105,17 @@ RSpec.describe Journable::WithHistoricAttributes,def create_journal(journable:, version:, timestamp:, attributes: {})work_package_attributes = journable.attributes.except("id")- journal_attributes = work_package_attributes \- .extract!(*Journal::WorkPackageJournal.attribute_names) \- .symbolize_keys.merge(attributes)++ journal_attributes = work_package_attributes+ .extract!(*Journal::WorkPackageJournal.attribute_names)+ .symbolize_keys+ .merge(attributes)+create(:work_package_journal,version:,- journable:, created_at: timestamp, updated_at: timestamp,+ journable:,+ created_at: timestamp,+ updated_at: timestamp,data: build(:journal_work_package_journal, journal_attributes))end