Added Project.visible_to_user

This method can be used to filter projects to those visible to a given
user.
This commit is contained in:
Yorick Peterse
2015-11-18 13:05:45 +01:00
parent a74d6d2043
commit a4fc8112df
2 changed files with 23 additions and 0 deletions
+4
View File
@@ -286,6 +286,10 @@ class Project < ActiveRecord::Base
joins(join_body).reorder('join_note_counts.amount DESC')
end
def visible_to_user(user)
where(id: user.authorized_projects.select(:id).reorder(nil))
end
end
def team
+19
View File
@@ -464,4 +464,23 @@ describe Project do
end
end
end
describe '.visible_to_user' do
let!(:project) { create(:project, :private) }
let!(:user) { create(:user) }
subject { described_class.visible_to_user(user) }
describe 'when a user has access to a project' do
before do
project.team.add_user(user, Gitlab::Access::MASTER)
end
it { is_expected.to eq([project]) }
end
describe 'when a user does not have access to any projects' do
it { is_expected.to eq([]) }
end
end
end