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
introduce a WP representer only rendering a subset of properties
ulferts5ebacf46974d
diff --git a/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb b/lib/api/v3/work_packages/work_package_at_timestamp_representer.rbnew file mode 100644index 00000000000..70e40b298b1--- /dev/null+++ b/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb@@ -0,0 +1,106 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2023 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+ module V3+ module WorkPackages+ class WorkPackageAtTimestampRepresenter < WorkPackageRepresenter+ # We avoid caching since the properties displayed depend on the changes happening between+ # the baseline and the timestamp provided.+ cached_representer disabled: true++ # List of properties that are currently supported by this representer.+ # Probably, way more, maybe even all properties are supported but that is untested.+ SUPPORTED_NON_LINK_PROPERTIES = %w[+ subject+ start_date+ due_date+ ].freeze++ SUPPORTED_LINK_PROPERTIES = %w[+ assignee+ responsible+ project+ status+ priority+ type+ version+ ].freeze++ SUPPORTED_PROPERTIES = (SUPPORTED_NON_LINK_PROPERTIES + SUPPORTED_LINK_PROPERTIES).freeze++ def initialize(model, current_user:, timestamp:)+ super(model.journables_by_timestamp[timestamp.to_s], current_user:, embed_links:, timestamps: [timestamp])++ self.model_with_history = model+ self.timestamp = timestamp+ end++ private++ attr_accessor :timestamp,+ :model_with_history++ def representable_map(*)+ Representable::Binding::Map.new(super.select { |binding| rendered_properties.include?(binding.name) })+ end++ def compile_links_for(configs, *args)+ super(configs.select { |config| rendered_properties.include?(config.first[:rel]) },+ *args)+ end++ def rendered_properties+ @rendered_properties ||= begin+ # This conversion is good enough for the set of supported properties as it+ # * Converts assigned_to_id to assignee+ # * does not mess with `start_date` and `due_date`+ changed_properties = (model_with_history.attributes_by_timestamp[timestamp.to_s]&.to_h || {})+ .keys+ .map(&:to_s)+ .map do |property|+ if property.ends_with?('_id')+ API::Utilities::PropertyNameConverter.from_ar_name(property)+ else+ property+ end+ end++ properties = (changed_properties & SUPPORTED_PROPERTIES) + ['_meta']++ if model_with_history.exists_at_timestamps.include?(timestamp.to_s)+ properties + ["links", :self]+ else+ properties+ end+ end+ end+ end+ end+ end+enddiff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rbindex e642d403263..5d634289418 100644--- a/lib/api/v3/work_packages/work_package_representer.rb+++ b/lib/api/v3/work_packages/work_package_representer.rb@@ -332,30 +332,30 @@ module APIproperty :_meta,if: ->(*) {- respond_to? :matches_query_filters_at_timestamps \- and respond_to? :timestamps \- and timestamps != [Timestamp.now]+ represented.respond_to?(:matches_query_filters_at_timestamps) &&+ represented.respond_to?(:timestamps) &&+ represented.timestamps != [Timestamp.now]},getter: ->(*) {{# This meta property states whether the attributes of the work package at the# last given timestamp (commonly the current time) match the filters of the# query. https://github.com/opf/openproject/pull/11783- #- 'matchesFilters': matches_query_filters_at_timestamp?(timestamps.last),+ 'matchesFilters': represented.matches_query_filters_at_timestamp?(timestamps.last),# This meta property states whether the work package exists at the last given# timestamp (commonly the current time).# https://github.com/opf/openproject/pull/11783#issuecomment-1374897874#- 'exists': exists_at_timestamps.include?(timestamps.last),+ 'exists': represented.exists_at_timestamps.include?(timestamps.last),# This meta property holds the timestamp of the data of the work package.#'timestamp': timestamps.last.to_s}.compact},- uncacheable: true+ uncacheable: true,+ exec_context: :decoratorproperty :id,render_nil: true@@ -479,35 +479,23 @@ module APIendproperty :attributes_by_timestamp,- as: :attributesByTimestamp,if: ->(*) {- respond_to?(:attributes_by_timestamp) and respond_to?(:timestamps) and timestamps != [Timestamp.now]- },+ represented.respond_to?(:attributes_by_timestamp) &&+ represented.attributes_by_timestamp.present? &&+ represented.respond_to?(:timestamps) &&+ represented.timestamps != [Timestamp.now]+ },getter: ->(*) do- timestamps.collect do |timestamp|- attrs = attributes_by_timestamp[timestamp.to_s].to_h- if exists_at_timestamps.include?(timestamp)- attrs = attrs.merge({- '_links': {- 'self': {- 'href': API::V3::Utilities::PathHelper::ApiV3Path \- .work_package(id, timestamps: timestamp)- }- }- })- end- attrs = attrs.merge({- '_meta': {- 'timestamp': timestamp.to_s,- 'matchesFilters': matches_query_filters_at_timestamp?(timestamp),- 'exists': exists_at_timestamps.include?(timestamp)- }.compact- })- attrs+ represented.timestamps.collect do |timestamp|+ API::V3::WorkPackages::WorkPackageAtTimestampRepresenter+ .create(represented,+ timestamp:,+ current_user:)endend,embedded: true,- uncacheable: true+ uncacheable: true,+ exec_context: :decoratorassociated_resource :categorydiff --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.rbnew file mode 100644index 00000000000..b50d2111871--- /dev/null+++ b/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rb@@ -0,0 +1,317 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2023 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'++# Note: The specs in this file do not attempt to test the properties themselves with all their possible+# variations. This is done in length in the work_package_representer_spec.rb. Instead, the focus of the tests+# here are on the selection of properties.+describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'rendering' do+ include API::V3::Utilities::PathHelper++ let(:current_user) { build_stubbed(:user) }+ let(: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+ ]+ end++ let(:due_date) { Date.current + 5.days }+ let(:start_date) { Date.current - 5.days }+ let(:assigned_to) { build_stubbed(:user) }+ let(:responsible) { build_stubbed(:user) }+ let(:status) { build_stubbed(:status) }+ let(:type) { build_stubbed(:type) }+ let(:priority) { build_stubbed(:priority) }+ let(:version) { build_stubbed(:version) }+ let(:project) { build_stubbed(:project) }++ let(:work_package) do+ build_stubbed(:work_package,+ project:,+ status:,+ due_date:,+ start_date:,+ assigned_to:,+ type:,+ priority:,+ version:,+ responsible:).tap do |wp|+ allow(wp)+ .to receive(:respond_to?)+ .and_call_original+ allow(wp)+ .to receive(:respond_to?)+ .with(:wrapped?)+ .and_return(true)+ end+ end+ let(:timestamp) { Timestamp.new }++ let(:attributes_by_timestamp) do+ {+ timestamp.to_s => OpenStruct.new(work_package.attributes)+ }+ end+ let(:journables_by_timestamp) do+ {+ timestamp.to_s => work_package+ }+ end+ let(:exists_at_timestamps) { [timestamp.to_s] }+ let(:matches_query_filters_at_timestamps) { { timestamp.to_s => true } }++ let(:model) do+ # Mimicking the eager loading wrapper+ work_package.tap do |model|+ allow(model)+ .to receive(:journables_by_timestamp)+ .and_return(journables_by_timestamp)+ allow(model)+ .to receive(:attributes_by_timestamp)+ .and_return(attributes_by_timestamp)+ allow(model)+ .to receive(:exists_at_timestamps)+ .and_return(exists_at_timestamps)+ allow(model)+ .to receive(:matches_query_filters_at_timestamps)+ .and_return(matches_query_filters_at_timestamps)+ allow(model)+ .to receive(:matches_query_filters_at_timestamp?)+ .with(timestamp)+ .and_return(matches_query_filters_at_timestamps[timestamp.to_s])+ #allow(model)+ # .to receive(:respond_to?)+ # .and_call_original+ allow(model)+ .to receive(:respond_to?)+ .with(:matches_query_filters_at_timestamps)+ .and_return(true)+ end+ end++ let(:representer) do+ described_class.create(model, current_user:, timestamp:)+ end++ subject(:generated) { representer.to_json }++ context 'with all supported properties requested' do+ let(:expected_json) do+ {+ 'subject' => work_package.subject,+ 'startDate' => work_package.start_date,+ 'dueDate' => work_package.due_date,+ '_meta' => {+ 'matchesFilters' => true,+ 'exists' => true,+ 'timestamp' => timestamp.to_s+ },+ '_links' => {+ 'assignee' => {+ 'href' => api_v3_paths.user(assigned_to.id),+ 'title' => assigned_to.name+ },+ 'responsible' => {+ 'href' => api_v3_paths.user(responsible.id),+ 'title' => responsible.name+ },+ 'project' => {+ 'href' => api_v3_paths.project(project.id),+ 'title' => project.name+ },+ 'status' => {+ 'href' => api_v3_paths.status(status.id),+ 'title' => status.name+ },+ 'type' => {+ 'href' => api_v3_paths.type(type.id),+ 'title' => type.name+ },+ 'priority' => {+ 'href' => api_v3_paths.priority(priority.id),+ 'title' => priority.name+ },+ 'version' => {+ 'href' => api_v3_paths.version(version.id),+ 'title' => version.name+ },+ 'self' => {+ 'href' => api_v3_paths.work_package(work_package.id, timestamps: timestamp),+ 'title' => work_package.subject+ }+ }+ }.to_json+ end++ it 'renders as expected' do+ expect(subject)+ .to be_json_eql(expected_json)+ end+ end++ context 'with a subset of supported properties' do+ let(:attributes_by_timestamp) do+ {+ timestamp.to_s => OpenStruct.new(work_package.attributes.slice('start_date', 'assigned_to_id', 'version_id'))+ }+ end++ let(:expected_json) do+ {+ 'startDate' => work_package.start_date,+ '_meta' => {+ 'matchesFilters' => true,+ 'exists' => true,+ 'timestamp' => timestamp.to_s+ },+ '_links' => {+ 'assignee' => {+ 'href' => api_v3_paths.user(assigned_to.id),+ 'title' => assigned_to.name+ },+ 'version' => {+ 'href' => api_v3_paths.version(version.id),+ 'title' => version.name+ },+ 'self' => {+ 'href' => api_v3_paths.work_package(work_package.id, timestamps: timestamp),+ 'title' => work_package.subject+ }+ }+ }.to_json+ end++ it 'renders as expected' do+ expect(subject)+ .to be_json_eql(expected_json)+ end+ end++ context 'without a linked property' do+ let(:attributes_by_timestamp) do+ {+ timestamp.to_s => OpenStruct.new(work_package.attributes.slice('subject', 'start_date'))+ }+ end++ let(:expected_json) do+ {+ 'subject' => work_package.subject,+ 'startDate' => work_package.start_date,+ '_meta' => {+ 'matchesFilters' => true,+ 'exists' => true,+ 'timestamp' => timestamp.to_s+ },+ '_links' => {+ 'self' => {+ 'href' => api_v3_paths.work_package(work_package.id, timestamps: timestamp),+ 'title' => work_package.subject+ }+ }+ }.to_json+ end++ it 'renders with only `self` in links' do+ expect(subject)+ .to be_json_eql(expected_json)+ end+ end++ context 'with a nil value for a linked property' do+ let(:assigned_to) { nil }++ let(:attributes_by_timestamp) do+ {+ timestamp.to_s => OpenStruct.new(work_package.attributes.slice('assigned_to_id'))+ }+ end++ let(:expected_json) do+ {+ '_meta' => {+ 'matchesFilters' => true,+ 'exists' => true,+ 'timestamp' => timestamp.to_s+ },+ '_links' => {+ 'assignee' => {+ 'href' => nil+ },+ 'self' => {+ 'href' => api_v3_paths.work_package(work_package.id, timestamps: timestamp),+ 'title' => work_package.subject+ }+ }+ }.to_json+ end++ it 'renders as expected' do+ expect(subject)+ .to be_json_eql(expected_json)+ end+ end++ context 'without the timestamp being in the attributes_by_timestamp collection' do+ let(:attributes_by_timestamp) do+ {}+ end++ let(:exists_at_timestamps) { [] }++ let(:matches_query_filters_at_timestamps) { { timestamp.to_s => false } }++ let(:expected_json) do+ {+ '_meta' => {+ 'matchesFilters' => false,+ 'exists' => false,+ 'timestamp' => timestamp.to_s+ }+ }.to_json+ end++ it 'has only the meta noting that the wp did not exist' do+ expect(subject)+ .to be_json_eql(expected_json)+ end+ end+enddiff --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 71737fc7b15..81a9be9c38b 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@@ -1306,19 +1306,28 @@ describe API::V3::WorkPackages::WorkPackageRepresenter dolet(:baseline_time) { Time.zone.parse("2022-01-01") }let(:work_pacakges) { WorkPackage.where(id: work_package.id) }let(:work_package) do- new_work_package = create(:work_package, subject: "The current work package", project:)+ new_work_package = create(:work_package,+ subject: "The current work package",+ assigned_to: current_user,+ project:)new_work_package.update_columns created_at: baseline_time - 1.daynew_work_packageendlet(:original_journal) docreate_journal(journable: work_package, timestamp: baseline_time - 1.day,version: 1,- attributes: { subject: "The original work package" })+ attributes: {+ subject: "The original work package",+ assigned_to: current_user+ })endlet(:current_journal) docreate_journal(journable: work_package, timestamp: 1.day.ago,version: 2,- attributes: { subject: "The current work package" })+ attributes: {+ subject: "The current work package",+ assigned_to: current_user+ })endlet(:project) { create(:project) }