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
baseline api: add `Journable::WithHistoricTimestamps` attributes through eager-loading wrapper
Sebastian Fiedlschustera115aa76fda9
diff --git a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb b/lib/api/v3/work_packages/eager_loading/historic_attributes.rbnew file mode 100644index 00000000000..62801f667d1--- /dev/null+++ b/lib/api/v3/work_packages/eager_loading/historic_attributes.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 API::V3::WorkPackages::EagerLoading+ class HistoricAttributes < Base+ attr_accessor :timestamps, :query++ def apply(work_package)+ work_package_with_historic_attributes = work_packages_with_historic_attributes.detect { |wp| wp.id == work_package.id }+ work_package.attributes = work_package_with_historic_attributes.attributes.except('timestamp')+ work_package.baseline_attributes = work_package_with_historic_attributes.baseline_attributes+ work_package.attributes_by_timestamp = work_package_with_historic_attributes.attributes_by_timestamp+ end++ def self.module+ HistoricAttributesAccessors+ end++ private++ def work_packages_with_historic_attributes+ @work_packages_with_historic_attributes ||= begin+ @timestamps ||= @query.try(:timestamps) || []+ Journable::WithHistoricAttributes.wrap_multiple(work_packages, timestamps: @timestamps, query: @query) # TODO: rename wrap to wrap_one, wrap_multiple to wrap+ end+ end++ end++ module HistoricAttributesAccessors+ extend ActiveSupport::Concern++ included do+ attr_accessor :baseline_attributes, :attributes_by_timestamp+ end+ end+enddiff --git a/lib/api/v3/work_packages/work_package_collection_representer.rb b/lib/api/v3/work_packages/work_package_collection_representer.rbindex cd716cdd259..fa0ba48d5ff 100644--- a/lib/api/v3/work_packages/work_package_collection_representer.rb+++ b/lib/api/v3/work_packages/work_package_collection_representer.rb@@ -71,7 +71,7 @@ module API# and set those to be the represented collection.# A potential ordering is reapplied to the work package collection in ruby.- @represented = ::API::V3::WorkPackages::WorkPackageEagerLoadingWrapper.wrap(represented, current_user)+ @represented = ::API::V3::WorkPackages::WorkPackageEagerLoadingWrapper.wrap(represented, current_user, timestamps:, query: _query)endlink :sumsSchema do@@ -140,7 +140,7 @@ module APIrep_class = element_decorator.custom_field_class(all_fields)represented.map do |model|- rep_class.send(:new, model, current_user:)+ rep_class.send(:new, model, current_user:, timestamps:, query: _query)end},exec_context: :decorator,diff --git a/lib/api/v3/work_packages/work_package_eager_loading_wrapper.rb b/lib/api/v3/work_packages/work_package_eager_loading_wrapper.rbindex 376474ccaa1..246c72fdb87 100644--- a/lib/api/v3/work_packages/work_package_eager_loading_wrapper.rb+++ b/lib/api/v3/work_packages/work_package_eager_loading_wrapper.rb@@ -35,23 +35,25 @@ module APIendclass << self- def wrap(ids_in_order, current_user)+ attr_accessor :timestamps, :query++ def wrap(ids_in_order, current_user, timestamps: nil, query: nil)work_packages = add_eager_loading(WorkPackage.where(id: ids_in_order), current_user).to_a- wrap_and_apply(work_packages, eager_loader_classes_all)+ wrap_and_apply(work_packages, eager_loader_classes_all, timestamps:, query:).sort_by { |wp| ids_in_order.index(wp.id) }end- def wrap_one(work_package, _current_user)+ def wrap_one(work_package, _current_user, timestamps: nil, query: nil)return work_package if work_package.respond_to?(:wrapped?)- wrap_and_apply([work_package], eager_loader_classes_all)+ wrap_and_apply([work_package], eager_loader_classes_all, timestamps:, query:).firstendprivate- def wrap_and_apply(work_packages, container_classes)+ def wrap_and_apply(work_packages, container_classes, timestamps:, query:)containers = container_classes.map { |klass| klass.new(work_packages) }@@ -60,6 +62,9 @@ module APIendcontainers.each do |container|+ container.timestamps = timestamps if container.respond_to?(:timestamps=)+ container.query = query if container.respond_to?(:query=)+work_packages.each do |work_package|container.apply(work_package)end@@ -75,7 +80,8 @@ module API::API::V3::WorkPackages::EagerLoading::Project,::API::V3::WorkPackages::EagerLoading::Checksum,::API::V3::WorkPackages::EagerLoading::CustomValue,- ::API::V3::WorkPackages::EagerLoading::CustomAction+ ::API::V3::WorkPackages::EagerLoading::CustomAction,+ ::API::V3::WorkPackages::EagerLoading::HistoricAttributes]enddiff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rbindex e324df7ccf3..7b0aee299fe 100644--- a/lib/api/v3/work_packages/work_package_representer.rb+++ b/lib/api/v3/work_packages/work_package_representer.rb@@ -41,10 +41,15 @@ module APIcached_representer key_parts: %i(project),disabled: false- def initialize(model, current_user:, embed_links: false)+ attr_accessor :timestamps, :query++ def initialize(model, current_user:, embed_links: false, timestamps: nil, query: nil)+ @query = query+ @timestamps = timestamps || query.try(:timestamps) || []+model = load_complete_model(model)- super+ super(model, current_user:, embed_links:)endself_link title_getter: ->(*) { represented.subject }@@ -647,7 +652,7 @@ module APIenddef load_complete_model(model)- ::API::V3::WorkPackages::WorkPackageEagerLoadingWrapper.wrap_one(model, current_user)+ ::API::V3::WorkPackages::WorkPackageEagerLoadingWrapper.wrap_one(model, current_user, timestamps:, query:)endendenddiff --git a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_representer_spec.rbindex 18d19caee0c..d81f1dc60ee 100644--- a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb+++ b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb@@ -34,8 +34,10 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter dolet(:member) { build_stubbed(:user) }let(:current_user) { member }let(:embed_links) { true }+ let(:timestamps) { nil }+ let(:query) { nil }let(:representer) do- described_class.create(work_package, current_user:, embed_links:)+ described_class.create(work_package, current_user:, embed_links:, timestamps:, query:)endlet(:parent) { nil }let(:priority) { build_stubbed(:priority, updated_at: Time.zone.now) }@@ -1298,6 +1300,46 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do.at_path('_embedded/customActions/0/name')endend++ context 'when passing timestamps' do+ let(:timestamps) { [Timestamp.new(baseline_time), Timestamp.now] }+ let(:baseline_time) { "2022-01-01".to_time }+ let(:work_pacakges) { WorkPackage.where(id: work_package.id) }+ let(:work_package) do+ new_work_package = create(:work_package, subject: "The current work package")+ new_work_package.update_columns created_at: baseline_time - 1.day+ new_work_package+ end+ let(:original_journal) do+ create_journal(journable: work_package, timestamp: baseline_time - 1.day,+ version: 1,+ attributes: { subject: "The original work package" })+ end+ let(:current_journal) do+ create_journal(journable: work_package, timestamp: 1.day.ago,+ version: 2,+ attributes: { subject: "The current work package" })+ end++ def create_journal(journable:, version:, 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, version:,+ journable:, created_at: timestamp, updated_at: timestamp,+ data: build(:journal_work_package_journal, journal_attributes))+ end++ before do+ WorkPackage.destroy_all+ work_package+ Journal.destroy_all+ original_journal+ current_journal+ end++ endenddescribe 'caching' dodiff --git a/spec/support/api/v3/work_packages/work_package_representer_eager_loading.rb b/spec/support/api/v3/work_packages/work_package_representer_eager_loading.rbindex af43b97956d..9a9a7a154f0 100644--- a/spec/support/api/v3/work_packages/work_package_representer_eager_loading.rb+++ b/spec/support/api/v3/work_packages/work_package_representer_eager_loading.rb@@ -34,6 +34,17 @@ shared_context 'eager loaded work package representer' do.to receive(:cache_checksum).and_return(srand)+ allow(work_package)+ .to receive(:baseline_attributes)+ .and_return({ subject: "The original work package" })++ allow(work_package)+ .to receive(:attributes_by_timestamp)+ .and_return({+ "2022-01-01T00:00:00+00:00" => {subject: "The original work package"},+ "PT0S" => {subject: "The current work package"}+ })+work_packageendend