Skip to content

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

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.rb
index 07fad6b6e83..225b8c2da97 100644
--- a/lib/api/utilities/endpoints/bodied.rb
+++ b/lib/api/utilities/endpoints/bodied.rb
@@ -30,6 +30,8 @@ module API
module Utilities
module Endpoints
class Bodied
+ include NamespacedLookup
+
def default_instance_generator(_model)
raise NotImplementedError
end
@@ -53,11 +55,11 @@ module API
end
def 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 API
process_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_state
self.parse_representer = parse_representer || deduce_parse_representer
self.render_representer = render_representer || deduce_render_representer
self.process_contract = process_contract || deduce_process_contract
@@ -155,11 +157,11 @@ module API
end
def deduce_process_service
- "::#{deduce_backend_namespace}::SetAttributesService".constantize
+ lookup_namespaced_class("SetAttributesService")
end
def deduce_process_contract
- "::#{deduce_backend_namespace}::#{update_or_create}Contract".constantize
+ lookup_namespaced_class("#{update_or_create}Contract")
end
def deduce_parse_representer
@@ -178,14 +180,6 @@ module API
api_name.pluralize
end
- def backend_name
- model.name.demodulize
- end
-
- def deduce_backend_namespace
- backend_name.pluralize
- end
-
def update_or_create
raise NotImplementedError
end
diff --git a/lib/api/utilities/endpoints/delete.rb b/lib/api/utilities/endpoints/delete.rb
index 0f9965820ab..8209d329aa6 100644
--- a/lib/api/utilities/endpoints/delete.rb
+++ b/lib/api/utilities/endpoints/delete.rb
@@ -30,6 +30,8 @@ module API
module Utilities
module Endpoints
class Delete
+ include NamespacedLookup
+
def default_instance_generator(model)
->(_params) do
instance_variable_get("@#{model.name.demodulize.underscore}")
@@ -37,14 +39,14 @@ module API
end
def 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.demodulize
self.success_status = success_status
end
@@ -97,15 +99,7 @@ module API
end
def 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")
end
def deduce_api_namespace
diff --git a/lib/api/utilities/endpoints/modify.rb b/lib/api/utilities/endpoints/modify.rb
index 34ff7f09e9f..33e62ffbd5e 100644
--- a/lib/api/utilities/endpoints/modify.rb
+++ b/lib/api/utilities/endpoints/modify.rb
@@ -78,7 +78,7 @@ module API
end
def deduce_process_service
- "::#{deduce_backend_namespace}::#{update_or_create}Service".constantize
+ lookup_namespaced_class("#{update_or_create}Service")
end
def deduce_process_contract
diff --git a/lib/api/utilities/endpoints/namespaced_lookup.rb b/lib/api/utilities/endpoints/namespaced_lookup.rb
new file mode 100644
index 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
+end
diff --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.rb
index 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::Endpoints
def render_representer
"::Bim::Bcf::API::V2_1::#{deduce_api_namespace}::SingleRepresenter".constantize
end
-
- def deduce_process_service
- "::Bim::Bcf::#{deduce_backend_namespace}::DeleteService".constantize
- end
end
end
diff --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.rb
index 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::ParseResourceParamsService
end
- 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".constantize
end
diff --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.rb
index 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_1
get &::Bim::Bcf::API::V2_1::Endpoints::Show.new(model: Project).mount
put &::Bim::Bcf::API::V2_1::Endpoints::Update
- .new(model: Project,
- process_service: ::Projects::UpdateService)
+ .new(model: Project)
.mount
mount ::Bim::Bcf::API::V2_1::TopicsAPI