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

fix[Op#54459]: Redefine `Project.with_available_custom_fields` as a subquery

Kabiru Mwenja2baa37ed59ba
diff --git a/app/models/project.rb b/app/models/project.rb
index f164a9cd4be..d1d644f167e 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -156,7 +156,8 @@ class Project < ApplicationRecord
friendly_id :identifier, use: :finders
scopes :allowed_to,
- :visible
+ :visible,
+ :with_available_custom_fields
scope :has_module, ->(mod) {
where(["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s])
@@ -171,10 +172,6 @@ class Project < ApplicationRecord
scope :with_member, ->(user = User.current) { where(id: user.memberships.select(:project_id)) }
scope :without_member, ->(user = User.current) { where.not(id: user.memberships.select(:project_id)) }
- scope :with_available_custom_fields, ->(custom_field_ids) do
- joins(:project_custom_field_project_mappings).where(project_custom_field_project_mappings: { custom_field_id: custom_field_ids })
- end
-
scopes :activated_time_activity,
:visible_with_activated_time_activity
diff --git a/app/models/projects/scopes/with_available_custom_fields.rb b/app/models/projects/scopes/with_available_custom_fields.rb
new file mode 100644
index 00000000000..e9eee25e596
--- /dev/null
+++ b/app/models/projects/scopes/with_available_custom_fields.rb
@@ -0,0 +1,42 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2023 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 WithAvailableCustomFields
+ extend ActiveSupport::Concern
+
+ class_methods do
+ def with_available_custom_fields(custom_field_ids)
+ mappings_subquery = ProjectCustomFieldProjectMapping.select(:project_id)
+ .where(custom_field_id: custom_field_ids)
+ .distinct
+ where(id: mappings_subquery)
+ end
+ end
+ end
+end
diff --git a/spec/models/projects/scopes/with_available_custom_fields_spec.rb b/spec/models/projects/scopes/with_available_custom_fields_spec.rb
new file mode 100644
index 00000000000..9766f08d46f
--- /dev/null
+++ b/spec/models/projects/scopes/with_available_custom_fields_spec.rb
@@ -0,0 +1,44 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2023 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::WithAvailableCustomFields do
+ shared_let(:project) { create(:project) }
+ shared_let(:project_custom_field) { create(:project_custom_field) }
+
+ shared_let(:project_custom_field_mapping) do
+ create(:project_custom_field_project_mapping, project:, project_custom_field:)
+ end
+
+ describe ".with_available_custom_fields" do
+ it "returns projects with the given custom fields" do
+ expect(Project.with_available_custom_fields([project_custom_field.id])).to contain_exactly(project)
+ end
+ end
+end