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
[#43677] Display detailsSchemas in the NotificationsCollectionRepresenter only when it is required
Dombi Attila685c691829c9
diff --git a/lib/api/v3/notifications/notification_collection_representer.rb b/lib/api/v3/notifications/notification_collection_representer.rbindex 2df15c5d2f3..0b5387a82c1 100644--- a/lib/api/v3/notifications/notification_collection_representer.rb+++ b/lib/api/v3/notifications/notification_collection_representer.rb@@ -31,15 +31,21 @@ module APImodule Notificationsclass NotificationCollectionRepresenter < ::API::Decorators::OffsetPaginatedCollectionproperty :detailsSchemas,- getter: ->(*) { ::API::V3::Values::Schemas::ValueSchemaFactory.all },+ getter: ->(*) { details_schemas },exec_context: :decorator,- embedded: true+ embedded: true,+ if: ->(*) { details_schemas.any? }def initialize(models, self_link:, current_user:, query: {}, page: nil, per_page: nil, groups: nil)super@represented = ::API::V3::Notifications::NotificationEagerLoadingWrapper.wrap(represented)end++ def details_schemas+ @details_schemas ||=+ ::API::V3::Notifications::PropertyFactory.schemas_for(@represented)+ endendendenddiff --git a/lib/api/v3/notifications/notification_representer.rb b/lib/api/v3/notifications/notification_representer.rbindex 64e782320f9..30c48f2797d 100644--- a/lib/api/v3/notifications/notification_representer.rb+++ b/lib/api/v3/notifications/notification_representer.rb@@ -42,14 +42,8 @@ module APIas: :readIANproperty :reason,- getter: ->(*) do- case reason- when 'date_alert_start_date', 'date_alert_due_date'- 'dateAlert'- else- reason- end- end+ exec_context: :decorator,+ getter: ->(*) { PropertyFactory.reason_for(represented) }date_time_property :created_at@@ -58,9 +52,7 @@ module APIproperty :details,embedded: true,exec_context: :decorator,- getter: ->(*) do- DetailsFactory.for(represented)- end+ getter: ->(*) { PropertyFactory.details_for(represented) }link :readIAN donext if represented.read_iandiff --git a/lib/api/v3/notifications/notifications_api.rb b/lib/api/v3/notifications/notifications_api.rbindex 942bb40b5d9..c1e6c957801 100644--- a/lib/api/v3/notifications/notifications_api.rb+++ b/lib/api/v3/notifications/notifications_api.rb@@ -97,7 +97,7 @@ module APInamespace :details doroute_param :detail_id, type: Integer, desc: 'Notification Detail ID' doget do- DetailsFactory.for(@notification).at(params[:detail_id]).tap do |detail|+ PropertyFactory.details_for(@notification).at(params[:detail_id]).tap do |detail|raise API::Errors::NotFound unless detailendenddiff --git a/lib/api/v3/notifications/details_factory.rb b/lib/api/v3/notifications/property_factory.rbsimilarity index 56%rename from lib/api/v3/notifications/details_factory.rbrename to lib/api/v3/notifications/property_factory.rbindex c1f3d0daab4..9c4e641b2a9 100644--- a/lib/api/v3/notifications/details_factory.rb+++ b/lib/api/v3/notifications/property_factory.rb@@ -27,22 +27,49 @@# ++module API::V3::Notifications- module DetailsFactory+ module PropertyFactoryextend ::API::V3::Utilities::PathHelper+ PROPERTY_FOR_REASON = {+ date_alert_start_date: "start_date",+ date_alert_due_date: "due_date"+ }.freeze+module_function- def for(notification)+ # Fetch the collection of details for a notification+ def details_for(notification)concrete_factory_for(notification.reason).for(notification)end+ # Fetch the collection of schemas for the provided notifications.+ def schemas_for(notifications)+ detail_properties = notifications.map(&:reason).to_set.reduce([]) do |properties, reason|+ property = PROPERTY_FOR_REASON[reason.to_sym]+ properties << property if property+ properties+ end+ ::API::V3::Values::Schemas::ValueSchemaFactory.all_for(detail_properties)+ end++ # Returns the outward facing reason e.g. `dateAlert` as opposed to `date_alert_start_date`.+ def reason_for(notification)+ reason = notification.reason+ case reason+ when 'date_alert_start_date', 'date_alert_due_date'+ 'dateAlert'+ else+ reason+ end+ end+def concrete_factory_for(reason)@concrete_factory_for ||= Hash.new do |h, reason_key|- h[reason_key] = if API::V3::Notifications::DetailsFactory.const_defined?(reason_key.camelcase)- "API::V3::Notifications::DetailsFactory::#{reason_key.camelcase}".constantize+ h[reason_key] = if API::V3::Notifications::PropertyFactory.const_defined?(reason_key.camelcase)+ "API::V3::Notifications::PropertyFactory::#{reason_key.camelcase}".constantizeelse- API::V3::Notifications::DetailsFactory::Default+ API::V3::Notifications::PropertyFactory::Defaultendenddiff --git a/lib/api/v3/notifications/details_factory/date_alert_due_date.rb b/lib/api/v3/notifications/property_factory/date_alert_due_date.rbsimilarity index 97%rename from lib/api/v3/notifications/details_factory/date_alert_due_date.rbrename to lib/api/v3/notifications/property_factory/date_alert_due_date.rbindex 1d0d6f68d01..62bcb21333e 100644--- a/lib/api/v3/notifications/details_factory/date_alert_due_date.rb+++ b/lib/api/v3/notifications/property_factory/date_alert_due_date.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.# ++-module API::V3::Notifications::DetailsFactory+module API::V3::Notifications::PropertyFactorymodule DateAlertDueDateextend ::API::V3::Utilities::PathHelperdiff --git a/lib/api/v3/notifications/details_factory/date_alert_start_date.rb b/lib/api/v3/notifications/property_factory/date_alert_start_date.rbsimilarity index 97%rename from lib/api/v3/notifications/details_factory/date_alert_start_date.rbrename to lib/api/v3/notifications/property_factory/date_alert_start_date.rbindex 4dc0d936e6f..462dfea4fc8 100644--- a/lib/api/v3/notifications/details_factory/date_alert_start_date.rb+++ b/lib/api/v3/notifications/property_factory/date_alert_start_date.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.# ++-module API::V3::Notifications::DetailsFactory+module API::V3::Notifications::PropertyFactorymodule DateAlertStartDateextend ::API::V3::Utilities::PathHelperdiff --git a/lib/api/v3/notifications/details_factory/default.rb b/lib/api/v3/notifications/property_factory/default.rbsimilarity index 96%rename from lib/api/v3/notifications/details_factory/default.rbrename to lib/api/v3/notifications/property_factory/default.rbindex bddf4c3a45a..3167772a92e 100644--- a/lib/api/v3/notifications/details_factory/default.rb+++ b/lib/api/v3/notifications/property_factory/default.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.# ++-module API::V3::Notifications::DetailsFactory+module API::V3::Notifications::PropertyFactorymodule Defaultmodule_functiondiff --git a/lib/api/v3/values/schemas/value_schema_factory.rb b/lib/api/v3/values/schemas/value_schema_factory.rbindex bb427a1cc57..ac09dea1cac 100644--- a/lib/api/v3/values/schemas/value_schema_factory.rb+++ b/lib/api/v3/values/schemas/value_schema_factory.rb@@ -42,8 +42,8 @@ module API::V3::Values::Schemasself_link: api_v3_paths.value_schema(property.camelcase(:lower)))end- def all- SUPPORTED.map { |property| self.for(property) }+ def all_for(properties)+ properties.map { |property| self.for(property) }enddef supported?(property)diff --git a/spec/lib/api/v3/notifications/details_factory_spec.rb b/spec/lib/api/v3/notifications/details_factory_spec.rbdeleted file mode 100644index 8d832fc0800..00000000000--- a/spec/lib/api/v3/notifications/details_factory_spec.rb+++ /dev/null@@ -1,80 +0,0 @@-# --copyright-# OpenProject is an open source project management software.-# Copyright (C) 2010-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.-# ++--require 'spec_helper'--describe ::API::V3::Notifications::DetailsFactory do- let(:resource) { build_stubbed(:work_package) }-- let(:notification) do- build_stubbed(:notification,- resource:,- reason:)- end-- describe '.for' do- context 'for a date_alert_start_date notification' do- let(:reason) { :date_alert_start_date }-- it 'returns one detail' do- expect(described_class.for(notification).size)- .to eq 1- end-- it 'returns an array of `Values::Property` representers' do- expect(described_class.for(notification)[0])- .to be_a ::API::V3::Values::PropertyDateRepresenter- end- end-- context 'for a date_alert_due_date notification' do- let(:reason) { :date_alert_due_date }-- it 'returns one detail' do- expect(described_class.for(notification).size)- .to eq 1- end-- it 'returns an array of `Values::Property` representers' do- expect(described_class.for(notification)[0])- .to be_a ::API::V3::Values::PropertyDateRepresenter- end- end-- (Notification::REASONS.keys - %i[date_alert_start_date date_alert_due_date]).each do |possible_reason|- context "for a #{possible_reason} notification" do- let(:reason) { possible_reason }-- it 'is an empty array' do- expect(described_class.for(notification))- .to eq []- end- end- end- end-enddiff --git a/spec/lib/api/v3/notifications/notification_collection_representer_spec.rb b/spec/lib/api/v3/notifications/notification_collection_representer_spec.rbindex 81fe6ca29f6..961c0a69602 100644--- a/spec/lib/api/v3/notifications/notification_collection_representer_spec.rb+++ b/spec/lib/api/v3/notifications/notification_collection_representer_spec.rb@@ -30,7 +30,7 @@ require 'spec_helper'describe ::API::V3::Notifications::NotificationCollectionRepresenter dolet(:self_base_link) { '/api/v3/notifications' }- let(:user) { build_stubbed :user }+ let(:user) { build_stubbed(:user) }let(:notifications) dobuild_stubbed_list(:notification,3).tap do |items|@@ -79,11 +79,6 @@ describe ::API::V3::Notifications::NotificationCollectionRepresenter doit_behaves_like 'offset-paginated APIv3 collection', 3, 'notifications', 'Notification'- it 'renders the available detailsSchemas' do- details_schemas = ::API::V3::Values::Schemas::ValueSchemaFactory.all- expect(subject).to be_json_eql(details_schemas.to_json).at_path('_embedded/detailsSchemas')- end-context 'when passing groups' dolet(:groups) do[@@ -96,5 +91,41 @@ describe ::API::V3::Notifications::NotificationCollectionRepresenter doexpect(subject).to be_json_eql(groups.to_json).at_path('groups')endend++ describe 'detailsSchema' do+ context 'when no date alert notifications are present' do+ it 'does not renders the detailsSchemas' do+ expect(subject).not_to have_json_path('_embedded/detailsSchemas')+ end+ end++ shared_examples_for 'rendering detailsSchemas' do |date_alert_reasons|+ before do+ date_alert_reasons.each_with_index do |reason, idx|+ notifications[idx].reason = reason+ end+ end++ it 'renders the required detailsSchemas' do+ properties = date_alert_reasons.map do |reason|+ ::API::V3::Notifications::PropertyFactory::PROPERTY_FOR_REASON[reason.to_sym]+ end+ details_schemas = ::API::V3::Values::Schemas::ValueSchemaFactory.all_for(properties)+ expect(subject).to be_json_eql(details_schemas.to_json).at_path('_embedded/detailsSchemas')+ end+ end++ context 'when a date alert start date notification is present' do+ it_behaves_like 'rendering detailsSchemas', ['date_alert_start_date']+ end++ context 'when a date alert due date notification is present' do+ it_behaves_like 'rendering detailsSchemas', ['date_alert_due_date']+ end++ context 'when both date alert notifications are present' do+ it_behaves_like 'rendering detailsSchemas', ['date_alert_start_date', 'date_alert_due_date']+ end+ endendenddiff --git a/spec/lib/api/v3/notifications/property_factory_spec.rb b/spec/lib/api/v3/notifications/property_factory_spec.rbnew file mode 100644index 00000000000..68e8f33a0e5--- /dev/null+++ b/spec/lib/api/v3/notifications/property_factory_spec.rb@@ -0,0 +1,170 @@+# --copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-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.+# ++++require 'spec_helper'++describe ::API::V3::Notifications::PropertyFactory do+ let(:resource) { build_stubbed(:work_package) }++ let(:notification) do+ build_stubbed(:notification,+ resource:,+ reason:)+ end++ describe '.details_for' do+ context 'for a date_alert_start_date notification' do+ let(:reason) { :date_alert_start_date }++ it 'returns one detail' do+ expect(described_class.details_for(notification).size)+ .to eq 1+ end++ it 'returns an array of `Values::Property` representers' do+ expect(described_class.details_for(notification)[0])+ .to be_a ::API::V3::Values::PropertyDateRepresenter+ end+ end++ context 'for a date_alert_due_date notification' do+ let(:reason) { :date_alert_due_date }++ it 'returns one detail' do+ expect(described_class.details_for(notification).size)+ .to eq 1+ end++ it 'returns an array of `Values::Property` representers' do+ expect(described_class.details_for(notification)[0])+ .to be_a ::API::V3::Values::PropertyDateRepresenter+ end+ end++ (Notification::REASONS.keys - %i[date_alert_start_date date_alert_due_date]).each do |possible_reason|+ context "for a #{possible_reason} notification" do+ let(:reason) { possible_reason }++ it 'is an empty array' do+ expect(described_class.details_for(notification))+ .to eq []+ end+ end+ end+ end++ describe '.schemas_for' do+ context 'for a date_alert_start_date notification' do+ let(:reason) { :date_alert_start_date }++ it 'returns one detail' do+ expect(described_class.schemas_for([notification]).size)+ .to eq 1+ end++ it 'returns an array of `Values::Schemas::PropertySchemaRepresenter` representers' do+ expect(described_class.schemas_for([notification])[0])+ .to be_a ::API::V3::Values::Schemas::PropertySchemaRepresenter+ end++ it 'returns `Values::Schemas::Model` representer for Start Date' do+ representer = described_class.schemas_for([notification])[0].represented+ expect(representer)+ .to be_a ::API::V3::Values::Schemas::Model+ expect(representer.name)+ .to eq "Start date"+ end+ end++ context 'for a date_alert_due_date notification' do+ let(:reason) { :date_alert_due_date }++ it 'returns one detail' do+ expect(described_class.schemas_for([notification]).size)+ .to eq 1+ end++ it 'returns an array of `Values::Schemas::PropertySchemaRepresenter` representers' do+ expect(described_class.schemas_for([notification])[0])+ .to be_a ::API::V3::Values::Schemas::PropertySchemaRepresenter+ end++ it 'returns `Values::Schemas::Model` representer for Finish date' do+ representer = described_class.schemas_for([notification])[0].represented+ expect(representer)+ .to be_a ::API::V3::Values::Schemas::Model+ expect(representer.name)+ .to eq "Finish date"+ end+ end++ context 'for multiple notifications' do+ let(:reason) { :date_alert_due_date }+ let(:reason2) { :date_alert_start_date }++ let(:notification2) do+ build_stubbed(:notification,+ resource:,+ reason: reason2)+ end++ let(:notifications) { [notification, notification2] }++ it 'returns one detail' do+ expect(described_class.schemas_for(notifications).size)+ .to eq 2+ end++ it 'returns an array of `Values::Schemas::PropertySchemaRepresenter` representers' do+ expect(described_class.schemas_for(notifications))+ .to all be_a ::API::V3::Values::Schemas::PropertySchemaRepresenter+ end++ it 'returns `Values::Schemas::Model` representer for Finish date' do+ representers = described_class.schemas_for(notifications).map(&:represented)+ expect(representers)+ .to all be_a ::API::V3::Values::Schemas::Model+ expect(representers.first.name)+ .to eq "Finish date"+ expect(representers.second.name)+ .to eq "Start date"+ end+ end++ (Notification::REASONS.keys - %i[date_alert_start_date date_alert_due_date]).each do |possible_reason|+ context "for a #{possible_reason} notification" do+ let(:reason) { possible_reason }++ it 'is an empty array' do+ expect(described_class.schemas_for([notification]))+ .to eq []+ end+ end+ end+ end+end