Group methods for filtering public/visible groups

These methods will be used to get a list of groups, optionally
restricted to only those visible to a given user.
This commit is contained in:
Yorick Peterse 2015-11-18 12:27:21 +01:00
parent 01620dd7e7
commit a74d6d2043
2 changed files with 35 additions and 0 deletions

View file

@ -49,6 +49,14 @@ class Group < Namespace
def reference_pattern
User.reference_pattern
end
def public_and_given_groups(ids)
where('public IS TRUE OR namespaces.id IN (?)', ids)
end
def visible_to_user(user)
where(id: user.authorized_groups.select(:id).reorder(nil))
end
end
def to_reference(_from_project = nil)

View file

@ -38,6 +38,33 @@ describe Group do
it { is_expected.not_to validate_presence_of :owner }
end
describe '.public_and_given_groups' do
let!(:public_group) { create(:group, public: true) }
subject { described_class.public_and_given_groups([group.id]) }
it { is_expected.to eq([public_group, group]) }
end
describe '.visible_to_user' do
let!(:group) { create(:group) }
let!(:user) { create(:user) }
subject { described_class.visible_to_user(user) }
describe 'when the user has access to a group' do
before do
group.add_user(user, Gitlab::Access::MASTER)
end
it { is_expected.to eq([group]) }
end
describe 'when the user does not have access to any groups' do
it { is_expected.to eq([]) }
end
end
describe '#to_reference' do
it 'returns a String reference to the object' do
expect(group.to_reference).to eq "@#{group.name}"