Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToon Claes <toon@gitlab.com>2017-05-23 23:40:07 +0300
committerToon Claes <toon@gitlab.com>2017-05-30 23:45:59 +0300
commit8e72ad70bd2479ae5a465eac1df74f99f03ea731 (patch)
treec282901f94ffddc335496481eb278e70749f93bc
parent07fc79e7c53a4fa7c4dd33835b905dfa8a609ff8 (diff)
Add starred_by scope to Project
Add a scope to search for the projects that are starred by a certain user.
-rw-r--r--app/models/project.rb3
-rw-r--r--app/models/user.rb2
-rw-r--r--spec/models/project_spec.rb14
3 files changed, 17 insertions, 2 deletions
diff --git a/app/models/project.rb b/app/models/project.rb
index a59095cb25c..963fd594d46 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -242,6 +242,7 @@ class Project < ActiveRecord::Base
scope :in_namespace, ->(namespace_ids) { where(namespace_id: namespace_ids) }
scope :personal, ->(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 :non_archived, -> { where(archived: false) }
scope :for_milestones, ->(ids) { joins(:milestones).where('milestones.id' => ids).distinct }
@@ -350,7 +351,7 @@ class Project < ActiveRecord::Base
where("projects.id IN (#{union.to_sql})")
end
- def search_by_visibility(level)
+ def search_by_visibility(level) # DEPRECATED: remove with API V3
where(visibility_level: Gitlab::VisibilityLevel.string_options[level])
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 3f816a250c2..20894ce269a 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -557,7 +557,7 @@ class User < ActiveRecord::Base
authorized_projects(Gitlab::Access::REPORTER).where(id: projects)
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 (?)",
[Project::PUBLIC, Project::INTERNAL],
authorized_projects.select(:project_id))
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index 2ef3d5654d5..da1b29a2bda 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -948,6 +948,20 @@ describe Project, models: true do
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
let!(:project) { create(:empty_project, :private) }
let!(:user) { create(:user) }