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
Make the lookup of Contracts and Services in longer namespaces possible
Christophe Bliard3ba029fe169f
diff --git a/lib/api/utilities/endpoints/bodied.rb b/lib/api/utilities/endpoints/bodied.rbindex 07fad6b6e83..225b8c2da97 100644--- a/lib/api/utilities/endpoints/bodied.rb+++ b/lib/api/utilities/endpoints/bodied.rb@@ -30,6 +30,8 @@ module APImodule Utilitiesmodule Endpointsclass Bodied+ include NamespacedLookup+def default_instance_generator(_model)raise NotImplementedErrorend@@ -53,11 +55,11 @@ module APIenddef initialize(model:,- api_name: model.name.demodulize,- instance_generator: default_instance_generator(model),- params_modifier: default_params_modifier,- params_source: default_params_source,- process_state: default_process_state,+ api_name: nil,+ instance_generator: nil,+ params_modifier: nil,+ params_source: nil,+ process_state: nil,before_hook: nil,parse_representer: nil,render_representer: nil,@@ -65,11 +67,11 @@ module APIprocess_contract: nil,parse_service: nil)self.model = model- self.api_name = api_name- self.instance_generator = instance_generator- self.params_modifier = params_modifier- self.params_source = params_source- self.process_state = process_state+ self.api_name = api_name || model.name.demodulize+ self.instance_generator = instance_generator || default_instance_generator(model)+ self.params_modifier = params_modifier || default_params_modifier+ self.params_source = params_source || default_params_source+ self.process_state = process_state || default_process_stateself.parse_representer = parse_representer || deduce_parse_representerself.render_representer = render_representer || deduce_render_representerself.process_contract = process_contract || deduce_process_contract@@ -155,11 +157,11 @@ module APIenddef deduce_process_service- "::#{deduce_backend_namespace}::SetAttributesService".constantize+ lookup_namespaced_class("SetAttributesService")enddef deduce_process_contract- "::#{deduce_backend_namespace}::#{update_or_create}Contract".constantize+ lookup_namespaced_class("#{update_or_create}Contract")enddef deduce_parse_representer@@ -178,14 +180,6 @@ module APIapi_name.pluralizeend- def backend_name- model.name.demodulize- end-- def deduce_backend_namespace- backend_name.pluralize- end-def update_or_createraise NotImplementedErrorenddiff --git a/lib/api/utilities/endpoints/delete.rb b/lib/api/utilities/endpoints/delete.rbindex 0f9965820ab..8209d329aa6 100644--- a/lib/api/utilities/endpoints/delete.rb+++ b/lib/api/utilities/endpoints/delete.rb@@ -30,6 +30,8 @@ module APImodule Utilitiesmodule Endpointsclass Delete+ include NamespacedLookup+def default_instance_generator(model)->(_params) doinstance_variable_get("@#{model.name.demodulize.underscore}")@@ -37,14 +39,14 @@ module APIenddef initialize(model:,- instance_generator: default_instance_generator(model),+ instance_generator: nil,process_service: nil,success_status: 204,- api_name: model.name.demodulize)+ api_name: nil)self.model = model- self.instance_generator = instance_generator+ self.instance_generator = instance_generator || default_instance_generator(model)self.process_service = process_service || deduce_process_service- self.api_name = api_name+ self.api_name = api_name || model.name.demodulizeself.success_status = success_statusend@@ -97,15 +99,7 @@ module APIenddef deduce_process_service- "::#{deduce_backend_namespace}::DeleteService".constantize- end-- def deduce_backend_namespace- demodulized_name.pluralize- end-- def demodulized_name- model.name.demodulize+ lookup_namespaced_class("DeleteService")enddef deduce_api_namespacediff --git a/lib/api/utilities/endpoints/modify.rb b/lib/api/utilities/endpoints/modify.rbindex 34ff7f09e9f..33e62ffbd5e 100644--- a/lib/api/utilities/endpoints/modify.rb+++ b/lib/api/utilities/endpoints/modify.rb@@ -78,7 +78,7 @@ module APIenddef deduce_process_service- "::#{deduce_backend_namespace}::#{update_or_create}Service".constantize+ lookup_namespaced_class("#{update_or_create}Service")enddef deduce_process_contractdiff --git a/lib/api/utilities/endpoints/namespaced_lookup.rb b/lib/api/utilities/endpoints/namespaced_lookup.rbnew file mode 100644index 00000000000..41645e6b5c0--- /dev/null+++ b/lib/api/utilities/endpoints/namespaced_lookup.rb@@ -0,0 +1,49 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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::Utilities::Endpoints::NamespacedLookup+ private++ def lookup_namespaced_class(class_name)+ possible_namespaces.each do |namespace|+ return "::#{namespace}::#{class_name}".constantize+ rescue NameError+ next+ end+ raise "Unable to lookup #{class_name} class for model #{model} in namespaces #{possible_namespaces.inspect}"+ end++ def possible_namespaces+ [+ model.name.pluralize,+ model.name.demodulize.pluralize+ ].uniq+ end+enddiff --git a/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/delete.rb b/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/delete.rbindex c36850bd0d4..876e7df0e8a 100644--- a/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/delete.rb+++ b/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/delete.rb@@ -42,9 +42,5 @@ module Bim::Bcf::API::V2_1::Endpointsdef render_representer"::Bim::Bcf::API::V2_1::#{deduce_api_namespace}::SingleRepresenter".constantizeend-- def deduce_process_service- "::Bim::Bcf::#{deduce_backend_namespace}::DeleteService".constantize- endendenddiff --git a/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/modify_mixin.rb b/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/modify_mixin.rbindex e0fa4c8dd9d..e3cf3521f41 100644--- a/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/modify_mixin.rb+++ b/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/modify_mixin.rb@@ -36,10 +36,6 @@ module Bim::Bcf::API::V2_1::Endpoints::Bim::Bcf::API::V2_1::ParseResourceParamsServiceend- def deduce_process_service- "::Bim::Bcf::#{deduce_backend_namespace}::#{update_or_create}Service".constantize- end-def deduce_in_and_out_representer"::Bim::Bcf::API::V2_1::#{deduce_api_namespace}::SingleRepresenter".constantizeenddiff --git a/modules/bim/app/controllers/bim/bcf/api/v2_1/projects_api.rb b/modules/bim/app/controllers/bim/bcf/api/v2_1/projects_api.rbindex f549a6dc87b..721cf9960a1 100644--- a/modules/bim/app/controllers/bim/bcf/api/v2_1/projects_api.rb+++ b/modules/bim/app/controllers/bim/bcf/api/v2_1/projects_api.rb@@ -49,8 +49,7 @@ module Bim::Bcf::API::V2_1get &::Bim::Bcf::API::V2_1::Endpoints::Show.new(model: Project).mountput &::Bim::Bcf::API::V2_1::Endpoints::Update- .new(model: Project,- process_service: ::Projects::UpdateService)+ .new(model: Project).mountmount ::Bim::Bcf::API::V2_1::TopicsAPI