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

Load the custom_values for the singular #at_timestamp method

Dombi Attila5c6835b43b48
diff --git a/app/models/journable/timestamps.rb b/app/models/journable/timestamps.rb
index c8ee89f8741..2269f8513fa 100644
--- a/app/models/journable/timestamps.rb
+++ b/app/models/journable/timestamps.rb
@@ -89,23 +89,24 @@ module Journable::Timestamps
# WorkPackage.find(1).at_timestamp(1.year.ago)
#
def at_timestamp(timestamp)
- # TODO: Update this method to also include the correct custom fields
- if journal = journals.at_timestamp(timestamp).first
- attributes = journal.data.attributes.merge(
- {
- "id" => id,
- "created_at" => created_at,
- "updated_at" => journal.updated_at,
- "timestamp" => timestamp
- }
- )
- self.class.column_names_missing_in_journal.each do |missing_column_name|
- attributes[missing_column_name] = nil
- end
- journable = self.class.instantiate(attributes)
- journable.readonly!
- journable
+ return unless journal = journals.at_timestamp(timestamp).first
+
+ attributes = journal.data.attributes.merge(
+ {
+ "id" => id,
+ "created_at" => created_at,
+ "updated_at" => journal.updated_at,
+ "timestamp" => timestamp,
+ "journal_id" => journal.id
+ }
+ )
+ self.class.column_names_missing_in_journal.each do |missing_column_name|
+ attributes[missing_column_name] = nil
end
+ journable = self.class.instantiate(attributes)
+ ::Journable::WithHistoricAttributes.load_custom_values(journable)
+ journable.readonly!
+ journable
end
def historic?
@@ -123,6 +124,6 @@ module Journable::Timestamps
def rollback!
raise ActiveRecord::RecordNotSaved, "This is no historic data. You can only revert to historic data." unless historic?
- self.class.find(id).update! attributes.except("id", "timestamp")
+ self.class.find(id).update! attributes.except("id", "timestamp", "journal_id")
end
end
diff --git a/app/models/journable/with_historic_attributes/loader.rb b/app/models/journable/with_historic_attributes/loader.rb
index e442f011af3..7e24d6363a3 100644
--- a/app/models/journable/with_historic_attributes/loader.rb
+++ b/app/models/journable/with_historic_attributes/loader.rb
@@ -27,6 +27,12 @@
# ++
class Journable::WithHistoricAttributes
+ class << self
+ def load_custom_values(journalized)
+ Loader.new(journalized).load_custom_values
+ end
+ end
+
class Loader
def initialize(journables)
@journables = Array(journables)
@@ -54,6 +60,16 @@ class Journable::WithHistoricAttributes
@work_package_ids_of_query_at_timestamp[query][timestamp]
end
+ def load_custom_values(journalized = journables)
+ journals_by_id = load_journals_by_id(journalized.map(&:journal_id))
+
+ journalized.each do |work_package|
+ journal = journals_by_id[work_package.journal_id]
+ set_custom_value_association_from_journal!(work_package:, journal:)
+ end
+ journalized
+ end
+
private
def work_package_ids_of_query_at_timestamp_calculation(query, timestamp)
@@ -74,9 +90,9 @@ class Journable::WithHistoricAttributes
@currently_invisible_journables ||= journables - currently_visible_journables
end
- def journalized_at_timestamp(t)
- journalized = (currently_invisible_journalized_at_timestamp(t) + currently_visible_journalized_at_timestamp(t))
- load_customizable_journals(journalized)
+ def journalized_at_timestamp(tms)
+ journalized = (currently_invisible_journalized_at_timestamp(tms) + currently_visible_journalized_at_timestamp(tms))
+ load_custom_values(journalized)
end
def currently_invisible_journalized_at_timestamp(timestamp)
@@ -91,31 +107,29 @@ class Journable::WithHistoricAttributes
journables.first.class
end
- attr_accessor :journables
-
- def load_customizable_journals(journalized)
- journals_by_id = Journal
- .where(id: journalized.map(&:journal_id))
+ def load_journals_by_id(journal_ids)
+ Journal
+ .where(id: journal_ids)
.includes({ customizable_journals: :custom_field })
.index_by(&:id)
+ end
- journalized.each do |work_package|
- journal = journals_by_id[work_package.journal_id]
- work_package.association(:journals).loaded!
- work_package.association(:journals).target = Array(journal)
-
- # Build the associated customizable_journals as custom values, this way the historic work packages
- # will behave just as the normal ones. Additionally set the reverse customized association
- # on the custom_values that points to the work_package itself.
- historic_custom_values = Array(journal&.customizable_journals).map do |customizable_journal|
- customizable_journal.as_custom_value(customized: work_package)
- end
+ def set_custom_value_association_from_journal!(work_package:, journal:)
+ work_package.association(:journals).loaded!
+ work_package.association(:journals).target = Array(journal)
- work_package.association(:custom_values).loaded!
- work_package.association(:custom_values).target = historic_custom_values
+ # Build the associated customizable_journals as custom values, this way the historic work packages
+ # will behave just as the normal ones. Additionally set the reverse customized association
+ # on the custom_values that points to the work_package itself.
+ historic_custom_values = Array(journal&.customizable_journals).map do |customizable_journal|
+ customizable_journal.as_custom_value(customized: work_package)
end
- journalized
+
+ work_package.association(:custom_values).loaded!
+ work_package.association(:custom_values).target = historic_custom_values
end
+
+ attr_accessor :journables
end
private_constant :Loader
end
diff --git a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
index 9319505786b..9f1df3efb2a 100644
--- a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
+++ b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
@@ -160,7 +160,7 @@ module API::V3::WorkPackages::EagerLoading
end
def available_custom_fields
- # The Journable::WithHistoricAttributes#load_customizable_journals loads the historic
+ # The Journable::WithHistoricAttributes#load_custom_values loads the historic
# Journal::CustomizableJournal objects and they are being dubbed as custom values.
# The custom_values contain only the historic Journal::CustomizableJournal objects
# that were available on the WorkPackage at the timestamp we are looking at.
diff --git a/spec/models/journable/timestamps_spec.rb b/spec/models/journable/timestamps_spec.rb
index e9ca790ffd4..96ea7eaa049 100644
--- a/spec/models/journable/timestamps_spec.rb
+++ b/spec/models/journable/timestamps_spec.rb
@@ -38,8 +38,9 @@ RSpec.describe Journable::Timestamps do
let(:thursday) { "2022-08-04".to_datetime }
let(:friday) { "2022-08-05".to_datetime }
+ let(:project) { create(:project_with_types) }
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 = create(:work_package, description: "The work package as it is since Friday", estimated_hours: 10, project:)
new_work_package.update_columns created_at: monday
new_work_package
end
@@ -588,6 +589,36 @@ RSpec.describe Journable::Timestamps do
expect(subject.send(column_name)).to eq 0
end
end
+
+ context 'with a custom field present' do
+ let!(:custom_field) do
+ create(:string_wp_custom_field,
+ name: 'String CF',
+ types: project.types,
+ projects: [project])
+ end
+
+ let!(:custom_value) do
+ create(:custom_value,
+ custom_field:,
+ customized: work_package,
+ value: 'The custom field as it is since Friday')
+ end
+
+ let!(:monday_customizable_journal) do
+ create(:journal_customizable_journal,
+ journal: monday_journal,
+ custom_field:,
+ value: 'The custom field as it has been on Monday')
+ end
+
+ it 'loads the custom_values relation with the historic values' do
+ expect(subject.send("custom_field_#{custom_field.id}"))
+ .to eq 'The custom field as it has been on Monday'
+ expect(journable.send("custom_field_#{custom_field.id}"))
+ .to eq 'The custom field as it is since Friday'
+ end
+ end
end
end