Commit 35758162 authored by charlie ablett's avatar charlie ablett

First go at filtering out ProjectNamespaces

For methods that expect only Group, we need to ensure
we don't accidentally return some ProjectNamespace
objects.
parent 02ab3b58
......@@ -52,7 +52,7 @@ class Namespace < ApplicationRecord
belongs_to :owner, class_name: "User"
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_one :chat_team, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_one :root_storage_statistics, class_name: 'Namespace::RootStorageStatistics'
......
......@@ -28,6 +28,16 @@ RSpec.describe Namespace do
it { is_expected.to have_one :onboarding_progress }
it { is_expected.to have_one :admin_note }
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
describe 'validations' do
......@@ -273,8 +283,8 @@ RSpec.describe Namespace do
describe '.by_parent' do
it 'includes correct namespaces' do
expect(described_class.by_parent(namespace1.id)).to eq([namespace1sub])
expect(described_class.by_parent(namespace2.id)).to eq([namespace2sub])
expect(described_class.by_parent(namespace1.id)).to match_array([namespace1sub])
expect(described_class.by_parent(namespace2.id)).to match_array([namespace2sub])
expect(described_class.by_parent(nil)).to match_array([namespace, namespace1, namespace2])
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