Commit b1dbabdec30d8e03afb0155b9494cfe2ef351352

Authored by Dmitriy Zaporozhets
1 parent 2c482097

Improve query of authorized_projects. Cache results for authorized_groups, autho…

…rized_projects methods
Showing 1 changed file with 9 additions and 4 deletions   Show diff stats
app/models/user.rb
@@ -88,6 +88,7 @@ class User < ActiveRecord::Base @@ -88,6 +88,7 @@ class User < ActiveRecord::Base
88 has_many :assigned_issues, dependent: :destroy, foreign_key: :assignee_id, class_name: "Issue" 88 has_many :assigned_issues, dependent: :destroy, foreign_key: :assignee_id, class_name: "Issue"
89 has_many :assigned_merge_requests, dependent: :destroy, foreign_key: :assignee_id, class_name: "MergeRequest" 89 has_many :assigned_merge_requests, dependent: :destroy, foreign_key: :assignee_id, class_name: "MergeRequest"
90 90
  91 + has_many :groups_projects, through: :groups, source: :projects
91 has_many :personal_projects, through: :namespace, source: :projects 92 has_many :personal_projects, through: :namespace, source: :projects
92 has_many :projects, through: :users_projects 93 has_many :projects, through: :users_projects
93 has_many :master_projects, through: :users_projects, source: :project, 94 has_many :master_projects, through: :users_projects, source: :project,
@@ -233,15 +234,19 @@ class User < ActiveRecord::Base @@ -233,15 +234,19 @@ class User < ActiveRecord::Base
233 234
234 # Groups user has access to 235 # Groups user has access to
235 def authorized_groups 236 def authorized_groups
236 - @group_ids ||= (groups.pluck(:id) + own_groups.pluck(:id) + authorized_projects.pluck(:namespace_id))  
237 - Group.where(id: @group_ids) 237 + @authorized_groups ||= begin
  238 + group_ids = (groups.pluck(:id) + own_groups.pluck(:id) + authorized_projects.pluck(:namespace_id))
  239 + Group.where(id: group_ids)
  240 + end
238 end 241 end
239 242
240 243
241 # Projects user has access to 244 # Projects user has access to
242 def authorized_projects 245 def authorized_projects
243 - @project_ids ||= (owned_projects.pluck(:id) + groups.map(&:projects).flatten.map(&:id) + projects.pluck(:id)).uniq  
244 - Project.where(id: @project_ids) 246 + @authorized_projects ||= begin
  247 + project_ids = (owned_projects.pluck(:id) + groups_projects.pluck(:id) + projects.pluck(:id)).uniq
  248 + Project.where(id: project_ids).includes(:namespace)
  249 + end
245 end 250 end
246 251
247 # Team membership in authorized projects 252 # Team membership in authorized projects