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

[Op#5715]: Include custom fields and project attributes in available custom fields scope

Kabiru Mwenjac9c50ff86b4a
diff --git a/app/models/projects/scopes/available_custom_fields.rb b/app/models/projects/scopes/available_custom_fields.rb
index 8d5860250af..e8a18cbd97b 100644
--- a/app/models/projects/scopes/available_custom_fields.rb
+++ b/app/models/projects/scopes/available_custom_fields.rb
@@ -32,20 +32,30 @@ module Projects::Scopes
class_methods do
def with_available_custom_fields(custom_field_ids)
- subquery = project_custom_fields_project_mapping_subquery(custom_field_ids:)
- where(id: subquery)
+ where(id: project_custom_fields_join_table_subquery(custom_field_ids:))
+ end
+
+ def with_available_project_custom_fields(custom_field_ids)
+ where(id: project_custom_fields_project_mapping_subquery(custom_field_ids:))
end
def without_available_custom_fields(custom_field_ids)
- subquery = project_custom_fields_project_mapping_subquery(custom_field_ids:)
- where.not(id: subquery)
+ where.not(id: project_custom_fields_join_table_subquery(custom_field_ids:))
+ end
+
+ def without_available_project_custom_fields(custom_field_ids)
+ where.not(id: project_custom_fields_project_mapping_subquery(custom_field_ids:))
end
private
def project_custom_fields_project_mapping_subquery(custom_field_ids:)
- ProjectCustomFieldProjectMapping.select(:project_id)
- .where(custom_field_id: custom_field_ids)
+ project_custom_fields_join_table_subquery(custom_field_ids:, join_table: ProjectCustomFieldProjectMapping)
+ end
+
+ def project_custom_fields_join_table_subquery(custom_field_ids:, join_table: CustomFieldsProject)
+ join_table.select(:project_id)
+ .where(custom_field_id: custom_field_ids)
end
end
end
diff --git a/spec/models/projects/scopes/available_custom_fields_spec.rb b/spec/models/projects/scopes/available_custom_fields_spec.rb
index 09edf5cd4b2..5891726e0cb 100644
--- a/spec/models/projects/scopes/available_custom_fields_spec.rb
+++ b/spec/models/projects/scopes/available_custom_fields_spec.rb
@@ -30,21 +30,40 @@ require "spec_helper"
RSpec.describe Projects::Scopes::AvailableCustomFields do
shared_let(:project) { create(:project) }
+
+ shared_let(:custom_field) { create(:custom_field) }
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
+ shared_let(:custom_field_mapping) do
+ create(:custom_fields_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)
+ expect(Project.with_available_custom_fields([custom_field.id])).to contain_exactly(project)
+ end
+ end
+
+ describe ".with_available_project_custom_fields" do
+ it "returns projects with the given project custom fields" do
+ expect(Project.with_available_project_custom_fields([project_custom_field.id]))
+ .to contain_exactly(project)
end
end
describe ".without_available_custom_fields" do
it "returns projects without the given custom fields" do
- expect(Project.without_available_custom_fields([project_custom_field.id])).to be_empty
+ expect(Project.without_available_custom_fields([custom_field.id])).to be_empty
+ end
+ end
+
+ describe ".without_available_project_custom_fields" do
+ it "returns projects without the given project custom fields" do
+ expect(Project.without_available_project_custom_fields([project_custom_field.id])).to be_empty
end
end
end