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

[#55962] Use custom filter for projects activated in storage

Judith Roth6d81a5df98d0
diff --git a/app/models/project.rb b/app/models/project.rb
index 1bb82198272..e4363ac4a5d 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -155,7 +155,8 @@ class Project < ApplicationRecord
friendly_id :identifier, use: :finders
- scopes :allowed_to,
+ scopes :activated_in_storage,
+ :allowed_to,
:available_custom_fields,
:visible
diff --git a/app/models/projects/scopes/activated_in_storage.rb b/app/models/projects/scopes/activated_in_storage.rb
new file mode 100644
index 00000000000..f6fb6f96ba4
--- /dev/null
+++ b/app/models/projects/scopes/activated_in_storage.rb
@@ -0,0 +1,53 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2024 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 Projects::Scopes
+ module ActivatedInStorage
+ extend ActiveSupport::Concern
+
+ class_methods do
+ def activated_in_storage(storage_ids)
+ subquery = project_storages_subquery(storage_ids:)
+ where(id: subquery)
+ end
+
+ def not_activated_in_storage(storage_ids)
+ subquery = project_storages_subquery(storage_ids:)
+ where.not(id: subquery)
+ end
+
+ private
+
+ def project_storages_subquery(storage_ids:)
+ Storages::ProjectStorage
+ .select(:project_id)
+ .where(storage_id: storage_ids)
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb b/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb
index 5254c801bfa..1c984f363c2 100644
--- a/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb
@@ -44,7 +44,7 @@ class Storages::Admin::Storages::ProjectStoragesController < ApplicationControll
@project_query = ProjectQuery.new(
name: "project-storage-mappings-#{@storage.id}"
) do |query|
- query.where(:id, "=", @storage.projects.ids)
+ query.where(:storages, "=", [@storage.id])
query.select(:name)
query.order("lft" => "asc")
end
diff --git a/modules/storages/app/models/queries/storages/projects/filter/storages_filter.rb b/modules/storages/app/models/queries/storages/projects/filter/storages_filter.rb
new file mode 100644
index 00000000000..6c8dd3eed75
--- /dev/null
+++ b/modules/storages/app/models/queries/storages/projects/filter/storages_filter.rb
@@ -0,0 +1,70 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2024 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.
+# ++
+#
+
+# This filter is used to find projects (including archived projects) that use one
+# of the given storages ids.
+module Queries::Storages::Projects::Filter
+ class StoragesFilter < ::Queries::Projects::Filters::ProjectFilter
+ def self.key
+ :storages
+ end
+
+ def type
+ :list
+ end
+
+ def allowed_values
+ @allowed_values ||= Storages::ProjectStorage
+ .includes(:storage)
+ .distinct
+ .pluck(:name, :storage_id)
+ end
+
+ def available?
+ User.current.admin?
+ end
+
+ def apply_to(_query_scope)
+ case operator
+ when "="
+ super.activated_in_storage(values)
+ when "!"
+ super.not_activated_in_storage(values)
+ else
+ raise "unsupported operator"
+ end
+ end
+
+ def where
+ nil
+ end
+ end
+end
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index 55b9948111d..73c21504845 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -252,6 +252,7 @@ module OpenProject::Storages
::Queries::Register.register(::ProjectQuery) do
filter ::Queries::Storages::Projects::Filter::StorageIdFilter
filter ::Queries::Storages::Projects::Filter::StorageUrlFilter
+ filter ::Queries::Storages::Projects::Filter::StoragesFilter
end
::Queries::Register.register(::Queries::Storages::FileLinks::FileLinkQuery) do
diff --git a/spec/models/projects/scopes/activated_in_storage_spec.rb b/spec/models/projects/scopes/activated_in_storage_spec.rb
new file mode 100644
index 00000000000..bf2d48b42c1
--- /dev/null
+++ b/spec/models/projects/scopes/activated_in_storage_spec.rb
@@ -0,0 +1,57 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2024 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"
+
+RSpec.describe Projects::Scopes::ActivatedInStorage do
+ shared_let(:project) { create(:project) }
+ shared_let(:storage) { create(:nextcloud_storage) }
+ shared_let(:project_storage) do
+ create(:project_storage, project:, storage:)
+ end
+
+ shared_let(:other_project) { create(:project) }
+ shared_let(:other_storage) { create(:one_drive_storage) }
+ shared_let(:other_project_storage) do
+ create(:project_storage, project: other_project, storage: other_storage)
+ end
+
+ shared_let(:project_without_storage) { create(:project) }
+
+ describe ".activated_in_storage" do
+ it "returns projects which use the given storage id" do
+ expect(Project.activated_in_storage([storage.id])).to contain_exactly(project)
+ end
+ end
+
+ describe ".not_activated_in_storage" do
+ it "returns projects which do not use the given storage id" do
+ expect(Project.not_activated_in_storage([storage.id])).to contain_exactly(other_project, project_without_storage)
+ end
+ end
+end