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

refactor: extract common code

Christophe Bliard4448efff122a
diff --git a/modules/storages/lib/api/v3/file_links/file_links_api.rb b/modules/storages/lib/api/v3/file_links/file_links_api.rb
index fb8405342fe..fe581ebfdfb 100644
--- a/modules/storages/lib/api/v3/file_links/file_links_api.rb
+++ b/modules/storages/lib/api/v3/file_links/file_links_api.rb
@@ -33,16 +33,12 @@ module API
module V3
module FileLinks
class FileLinksAPI < ::API::OpenProjectAPI
- # helpers are defined by the grape framework. They provide methods that can be called from within the
- # endpoint context.
- helpers do
- def visible_file_links_scope
- ::Storages::FileLink.visible(current_user)
- end
- end
+ # helpers is defined by the grape framework. They make methods from the
+ # module available from within the endpoint context.
+ helpers API::V3::Utilities::StoragesHelpers
before do
- raise ::API::Errors::NotFound unless OpenProject::FeatureDecisions.storages_module_active?
+ reply_with_not_found_if_module_inactive
end
# The `:resources` keyword defines the API namespace -> /api/v3/file_links/...
diff --git a/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb b/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
index 5a0a2ffca23..20c4efc90dd 100644
--- a/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
+++ b/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
@@ -34,16 +34,12 @@ module API
module V3
module FileLinks
class WorkPackagesFileLinksAPI < ::API::OpenProjectAPI
- # helpers are defined by the grape framework. They provide methods that can be called from within the
- # endpoint context.
- helpers do
- def visible_file_links_scope
- ::Storages::FileLink.visible(current_user)
- end
- end
+ # helpers is defined by the grape framework. They make methods from the
+ # module available from within the endpoint context.
+ helpers API::V3::Utilities::StoragesHelpers
before do
- raise ::API::Errors::NotFound unless OpenProject::FeatureDecisions.storages_module_active?
+ reply_with_not_found_if_module_inactive
end
# The `:resources` keyword defines the API namespace -> /api/v3/work_packages/:id/file_links/...
diff --git a/modules/storages/lib/api/v3/storages/storages_api.rb b/modules/storages/lib/api/v3/storages/storages_api.rb
index ceabe1f47bb..0f399921112 100644
--- a/modules/storages/lib/api/v3/storages/storages_api.rb
+++ b/modules/storages/lib/api/v3/storages/storages_api.rb
@@ -35,14 +35,12 @@ module API
module Storages
# OpenProjectAPI is a simple subclass of Grape::API that handles patches.
class StoragesAPI < ::API::OpenProjectAPI
- helpers do
- def visible_storages_scope
- ::Storages::Storage.visible(current_user)
- end
- end
+ # helpers is defined by the grape framework. They make methods from the
+ # module available from within the endpoint context.
+ helpers API::V3::Utilities::StoragesHelpers
before do
- raise ::API::Errors::NotFound unless OpenProject::FeatureDecisions.storages_module_active?
+ reply_with_not_found_if_module_inactive
end
# The `:resources` keyword defines the API namespace -> /api/v3/storages/...
diff --git a/modules/storages/lib/api/v3/utilities/storages_helpers.rb b/modules/storages/lib/api/v3/utilities/storages_helpers.rb
new file mode 100644
index 00000000000..d8b85c6927b
--- /dev/null
+++ b/modules/storages/lib/api/v3/utilities/storages_helpers.rb
@@ -0,0 +1,43 @@
+#-- 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::Utilities
+ module StoragesHelpers
+ def reply_with_not_found_if_module_inactive
+ raise ::API::Errors::NotFound unless OpenProject::FeatureDecisions.storages_module_active?
+ end
+
+ def visible_storages_scope
+ ::Storages::Storage.visible(current_user)
+ end
+
+ def visible_file_links_scope
+ ::Storages::FileLink.visible(current_user)
+ end
+ end
+end