Commit 8e72ad70 authored by Toon Claes's avatar Toon Claes

Add starred_by scope to Project

Add a scope to search for the projects that are starred by a certain user.
parent 07fc79e7
...@@ -242,6 +242,7 @@ class Project < ActiveRecord::Base ...@@ -242,6 +242,7 @@ class Project < ActiveRecord::Base
scope :in_namespace, ->(namespace_ids) { where(namespace_id: namespace_ids) } scope :in_namespace, ->(namespace_ids) { where(namespace_id: namespace_ids) }
scope :personal, ->(user) { where(namespace_id: user.namespace_id) } scope :personal, ->(user) { where(namespace_id: user.namespace_id) }
scope :joined, ->(user) { where('namespace_id != ?', user.namespace_id) } scope :joined, ->(user) { where('namespace_id != ?', user.namespace_id) }
scope :starred_by, ->(user) { joins(:users_star_projects).where('users_star_projects.user_id': user.id) }
scope :visible_to_user, ->(user) { where(id: user.authorized_projects.select(:id).reorder(nil)) } scope :visible_to_user, ->(user) { where(id: user.authorized_projects.select(:id).reorder(nil)) }
scope :non_archived, -> { where(archived: false) } scope :non_archived, -> { where(archived: false) }
scope :for_milestones, ->(ids) { joins(:milestones).where('milestones.id' => ids).distinct } scope :for_milestones, ->(ids) { joins(:milestones).where('milestones.id' => ids).distinct }
...@@ -350,7 +351,7 @@ class Project < ActiveRecord::Base ...@@ -350,7 +351,7 @@ class Project < ActiveRecord::Base
where("projects.id IN (#{union.to_sql})") where("projects.id IN (#{union.to_sql})")
end end
def search_by_visibility(level) def search_by_visibility(level) # DEPRECATED: remove with API V3
where(visibility_level: Gitlab::VisibilityLevel.string_options[level]) where(visibility_level: Gitlab::VisibilityLevel.string_options[level])
end end
......
...@@ -557,7 +557,7 @@ class User < ActiveRecord::Base ...@@ -557,7 +557,7 @@ class User < ActiveRecord::Base
authorized_projects(Gitlab::Access::REPORTER).where(id: projects) authorized_projects(Gitlab::Access::REPORTER).where(id: projects)
end end
def viewable_starred_projects def viewable_starred_projects # DEPRECATED: Use ProjectFinder instead. Remove together with API V3
starred_projects.where("projects.visibility_level IN (?) OR projects.id IN (?)", starred_projects.where("projects.visibility_level IN (?) OR projects.id IN (?)",
[Project::PUBLIC, Project::INTERNAL], [Project::PUBLIC, Project::INTERNAL],
authorized_projects.select(:project_id)) authorized_projects.select(:project_id))
......
...@@ -948,6 +948,20 @@ describe Project, models: true do ...@@ -948,6 +948,20 @@ describe Project, models: true do
end end
end end
describe '.starred_by' do
it 'returns only projects starred by the given user' do
user1 = create(:user)
user2 = create(:user)
project1 = create(:empty_project)
project2 = create(:empty_project)
create(:empty_project)
user1.toggle_star(project1)
user2.toggle_star(project2)
expect(Project.starred_by(user1)).to contain_exactly(project1)
end
end
describe '.visible_to_user' do describe '.visible_to_user' do
let!(:project) { create(:empty_project, :private) } let!(:project) { create(:empty_project, :private) }
let!(:user) { create(:user) } let!(:user) { create(:user) }
......
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