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
[#47064] Support custom_field_* in the attributesByTimestamp
Dombi Attila629d6ef7150f
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rbindex 98c6394acef..26946cc06c7 100644--- a/app/models/journable/historic_active_record_relation.rb+++ b/app/models/journable/historic_active_record_relation.rb@@ -267,7 +267,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation# Does not work yet for other includes.#def add_join_projects_on_work_package_journals(relation)- if include_projects?(relation)+ if include_projects?relation.except(:includes, :eager_load, :preload).joins('LEFT OUTER JOIN "projects" ' \@@ -277,8 +277,8 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relationendend- def include_projects?(relation)- include_values = relation.values.fetch(:includes, [])+ def include_projects?+ include_values = values.fetch(:includes, [])include_values.include?(:project)end@@ -288,7 +288,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation# - the `work_package_journals` table (data)# - the `journals` table#- # Also, add the `timestamp` as column so that we have it as attribute in our model.+ # Also, add the `timestamp` and `journal_id` as column so that we have it as attribute in our model.#def select_columns_from_the_appropriate_tables(relation)if relation.select_values.count == 0@@ -311,7 +311,8 @@ 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"+ "'#{timestamp}' as timestamp",+ "journals.id as journal_id"] + \model.column_names_missing_in_journal.collect do |missing_column_name|"null as #{missing_column_name}"diff --git a/app/models/journable/timestamps.rb b/app/models/journable/timestamps.rbindex 53fb992c5b5..c8ee89f8741 100644--- a/app/models/journable/timestamps.rb+++ b/app/models/journable/timestamps.rb@@ -89,6 +89,7 @@ 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 fieldsif journal = journals.at_timestamp(timestamp).firstattributes = journal.data.attributes.merge({diff --git a/app/models/journable/with_historic_attributes.rb b/app/models/journable/with_historic_attributes.rbindex 0d638eafc36..8273b43920b 100644--- a/app/models/journable/with_historic_attributes.rb+++ b/app/models/journable/with_historic_attributes.rb@@ -132,7 +132,7 @@ class Journable::WithHistoricAttributes < SimpleDelegatorendendend-+ # TODO: Add comment that this is not being used for the API.def attributes_by_timestamp@attributes_by_timestamp ||= Hash.new do |h, t|attributes = if include_only_changed_attributesdiff --git a/app/models/journable/with_historic_attributes/loader.rb b/app/models/journable/with_historic_attributes/loader.rbindex 73375f2d8c9..17d4f3216d8 100644--- a/app/models/journable/with_historic_attributes/loader.rb+++ b/app/models/journable/with_historic_attributes/loader.rb@@ -38,7 +38,7 @@ class Journable::WithHistoricAttributesdef at_timestamp(timestamp)@at_timestamp ||= Hash.new do |h, t|- h[t] = (currently_invisible_journalized_at_timestamp(t) + currently_visible_journalized_at_timestamp(t)).index_by(&:id)+ h[t] = journalized_at_timestamp(t).index_by(&:id)end@at_timestamp[timestamp]@@ -74,6 +74,11 @@ class Journable::WithHistoricAttributes@currently_invisible_journables ||= journables - currently_visible_journablesend+ def journalized_at_timestamp(t)+ journalized = (currently_invisible_journalized_at_timestamp(t) + currently_visible_journalized_at_timestamp(t))+ load_customizable_journals(journalized)+ end+def currently_invisible_journalized_at_timestamp(timestamp)journalized_class.visible.at_timestamp(timestamp).where(id: currently_invisible_journables)end@@ -87,6 +92,22 @@ class Journable::WithHistoricAttributesendattr_accessor :journables++ def load_customizable_journals(journalized)+ journals_by_journable = Journal+ .where(id: journalized.map(&:journal_id))+ .includes({ customizable_journals: :custom_field })+ .index_by(&:journable_id)++ journalized.each do |work_package|+ # work_package.association(:journals).loaded!+ # work_package.association(:journals).target = Array(journals_by_journable[work_package.id])+ work_package.association(:custom_values).loaded!+ custom_values = Array(journals_by_journable[work_package.id]&.customizable_journals)+ work_package.association(:custom_values).target = custom_values+ end+ journalized+ endendprivate_constant :Loaderenddiff --git a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb b/lib/api/v3/work_packages/eager_loading/historic_attributes.rbindex 5bf9bb65d26..741c9905dc9 100644--- a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb+++ b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb@@ -159,14 +159,12 @@ module API::V3::WorkPackages::EagerLoadingnew_record? ? @timestamp : Timestamp.parse(__getobj__.timestamp)end- # Since custom fields are currently never displayed in the attributesByTimestamp,- # for which this object is used, simply short circuit the loading of the custom field information.def available_custom_fields- WorkPackageCustomField.none- end-- def define_all_custom_field_accessors- nil+ # The Journable::WithHistoricAttributes#load_customizable_journals 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.+ custom_values.map(&:custom_field)endendenddiff --git a/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb b/lib/api/v3/work_packages/work_package_at_timestamp_representer.rbindex fc830dc5cc0..89fad76fdfd 100644--- a/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb+++ b/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb@@ -54,6 +54,8 @@ module APIparent].freeze+ # Support custom values and custom fields in the allow list+SUPPORTED_PROPERTIES = (SUPPORTED_NON_LINK_PROPERTIES + SUPPORTED_LINK_PROPERTIES).freezeSTATIC_NON_LINK_PROPERTIES = %w[_meta].freezediff --git a/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rb b/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rbindex 5521ee2e715..932aa8b1cb6 100644--- a/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rb+++ b/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rb@@ -38,21 +38,6 @@ RSpec.describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'renderlet(:embed_links) { false }let(:timestamps) { nil }let(:query) { nil }- let(:properties) do- %w[- subject- start_date- due_date- assignee- responsible- project- status- priority- type- version- parent- ]- endlet(:due_date) { Date.current + 5.days }let(:start_date) { Date.current - 5.days }@@ -70,6 +55,16 @@ RSpec.describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'renderendendlet(:project) { build_stubbed(:project) }+ let(:custom_field) do+ build_stubbed(:text_wp_custom_field,+ name: 'Text CF',+ types: project.types,+ projects: [project])+ end++ let(:available_custom_fields) { [custom_field] }++ let(:custom_values) { { custom_field.id => 'This is a text value' } }let(:work_package) dobuild_stubbed(:work_package,@@ -82,7 +77,8 @@ RSpec.describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'renderpriority:,version:,parent:,- responsible:).tap do |wp|+ responsible:,+ custom_values:).tap do |wp|allow(wp).to receive(:respond_to?).and_call_original@@ -90,6 +86,9 @@ RSpec.describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'render.to receive(:respond_to?).with(:wrapped?).and_return(true)+ allow(wp)+ .to receive(:available_custom_fields)+ .and_return(available_custom_fields)endendlet(:timestamp) { Timestamp.new(1.day.ago) }@@ -136,6 +135,7 @@ RSpec.describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'render'subject' => work_package.subject,'startDate' => work_package.start_date,'dueDate' => work_package.due_date,+ "customField#{custom_field.id}" => 'This is a text value','_meta' => {'matchesFilters' => true,'exists' => true,