Commit f0dbb59b authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '341823-cablett-namespace-scopes' into 'master'

Filter out ProjectNamespaces for Namespace children association

See merge request gitlab-org/gitlab!72548
parents 886d9e04 35758162
...@@ -52,7 +52,7 @@ class Namespace < ApplicationRecord ...@@ -52,7 +52,7 @@ class Namespace < ApplicationRecord
belongs_to :owner, class_name: "User" belongs_to :owner, class_name: "User"
belongs_to :parent, class_name: "Namespace" belongs_to :parent, class_name: "Namespace"
has_many :children, class_name: "Namespace", foreign_key: :parent_id has_many :children, -> { where(type: Group.sti_name) }, class_name: "Namespace", foreign_key: :parent_id
has_many :custom_emoji, inverse_of: :namespace has_many :custom_emoji, inverse_of: :namespace
has_one :chat_team, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_one :chat_team, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_one :root_storage_statistics, class_name: 'Namespace::RootStorageStatistics' has_one :root_storage_statistics, class_name: 'Namespace::RootStorageStatistics'
......
...@@ -28,6 +28,16 @@ RSpec.describe Namespace do ...@@ -28,6 +28,16 @@ RSpec.describe Namespace do
it { is_expected.to have_one :onboarding_progress } it { is_expected.to have_one :onboarding_progress }
it { is_expected.to have_one :admin_note } it { is_expected.to have_one :admin_note }
it { is_expected.to have_many :pending_builds } it { is_expected.to have_many :pending_builds }
describe '#children' do
let_it_be(:group) { create(:group) }
let_it_be(:subgroup) { create(:group, parent: group) }
let_it_be(:project_namespace) { create(:project_namespace, parent: group) }
it 'excludes project namespaces' do
expect(group.children).to match_array([subgroup])
end
end
end end
describe 'validations' do describe 'validations' do
...@@ -273,8 +283,8 @@ RSpec.describe Namespace do ...@@ -273,8 +283,8 @@ RSpec.describe Namespace do
describe '.by_parent' do describe '.by_parent' do
it 'includes correct namespaces' do it 'includes correct namespaces' do
expect(described_class.by_parent(namespace1.id)).to eq([namespace1sub]) expect(described_class.by_parent(namespace1.id)).to match_array([namespace1sub])
expect(described_class.by_parent(namespace2.id)).to eq([namespace2sub]) expect(described_class.by_parent(namespace2.id)).to match_array([namespace2sub])
expect(described_class.by_parent(nil)).to match_array([namespace, namespace1, namespace2]) expect(described_class.by_parent(nil)).to match_array([namespace, namespace1, namespace2])
end end
end end
......
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