Commit 29df055b authored by Jan Provaznik's avatar Jan Provaznik

Add scope to exclude project namespaces

There will be multiple places where we will have to exclude project
namespaces when getting Namespace so let's add a scope for it.
parent e8d273b4
......@@ -116,6 +116,9 @@ class Namespace < ApplicationRecord
# TODO: change to `type: Namespaces::UserNamespace.sti_name` when
# working on issue https://gitlab.com/gitlab-org/gitlab/-/issues/341070
scope :user_namespaces, -> { where(type: [nil, Namespaces::UserNamespace.sti_name]) }
# TODO: this can be simplified with `type != 'Project'` when working on issue
# https://gitlab.com/gitlab-org/gitlab/-/issues/341070
scope :without_project_namespaces, -> { where("type IS DISTINCT FROM ?", Namespaces::ProjectNamespace.sti_name) }
scope :sort_by_type, -> { order(Gitlab::Database.nulls_first_order(:type)) }
scope :include_route, -> { includes(:route) }
scope :by_parent, -> (parent) { where(parent_id: parent) }
......
......@@ -299,6 +299,15 @@ RSpec.describe Namespace do
expect(described_class.sorted_by_similarity_and_parent_id_desc('Namespace')).to eq([namespace2, namespace1, namespace2sub, namespace1sub, namespace])
end
end
describe '.without_project_namespaces' do
let_it_be(:user_namespace) { create(:user_namespace) }
let_it_be(:project_namespace) { create(:project_namespace) }
it 'excludes project namespaces' do
expect(described_class.without_project_namespaces).to match_array([namespace, namespace1, namespace2, namespace1sub, namespace2sub, user_namespace, project_namespace.parent])
end
end
end
describe 'delegate' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment