Commit b1dbabde authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

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

Improve query of authorized_projects. Cache results for authorized_groups, authorized_projects methods
parent 2c482097
...@@ -88,6 +88,7 @@ class User < ActiveRecord::Base ...@@ -88,6 +88,7 @@ class User < ActiveRecord::Base
has_many :assigned_issues, dependent: :destroy, foreign_key: :assignee_id, class_name: "Issue" has_many :assigned_issues, dependent: :destroy, foreign_key: :assignee_id, class_name: "Issue"
has_many :assigned_merge_requests, dependent: :destroy, foreign_key: :assignee_id, class_name: "MergeRequest" has_many :assigned_merge_requests, dependent: :destroy, foreign_key: :assignee_id, class_name: "MergeRequest"
has_many :groups_projects, through: :groups, source: :projects
has_many :personal_projects, through: :namespace, source: :projects has_many :personal_projects, through: :namespace, source: :projects
has_many :projects, through: :users_projects has_many :projects, through: :users_projects
has_many :master_projects, through: :users_projects, source: :project, 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
# Groups user has access to # Groups user has access to
def authorized_groups def authorized_groups
@group_ids ||= (groups.pluck(:id) + own_groups.pluck(:id) + authorized_projects.pluck(:namespace_id)) @authorized_groups ||= begin
Group.where(id: @group_ids) group_ids = (groups.pluck(:id) + own_groups.pluck(:id) + authorized_projects.pluck(:namespace_id))
Group.where(id: group_ids)
end
end end
# Projects user has access to # Projects user has access to
def authorized_projects def authorized_projects
@project_ids ||= (owned_projects.pluck(:id) + groups.map(&:projects).flatten.map(&:id) + projects.pluck(:id)).uniq @authorized_projects ||= begin
Project.where(id: @project_ids) project_ids = (owned_projects.pluck(:id) + groups_projects.pluck(:id) + projects.pluck(:id)).uniq
Project.where(id: project_ids).includes(:namespace)
end
end end
# Team membership in authorized projects # Team membership in authorized projects
......
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