Commit 6424ec936e4faa1d236579714efc825939766ce9
1 parent
f9b66aec
Exists in
master
and in
4 other branches
Refactor user associations. Drop support of Group#owner_id
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
1 changed file
with
10 additions
and
13 deletions
Show diff stats
app/models/user.rb
@@ -69,20 +69,22 @@ class User < ActiveRecord::Base | @@ -69,20 +69,22 @@ class User < ActiveRecord::Base | ||
69 | # Namespace for personal projects | 69 | # Namespace for personal projects |
70 | has_one :namespace, dependent: :destroy, foreign_key: :owner_id, class_name: "Namespace", conditions: 'type IS NULL' | 70 | has_one :namespace, dependent: :destroy, foreign_key: :owner_id, class_name: "Namespace", conditions: 'type IS NULL' |
71 | 71 | ||
72 | - # Namespaces (owned groups and own namespace) | ||
73 | - has_many :namespaces, foreign_key: :owner_id | ||
74 | - | ||
75 | # Profile | 72 | # Profile |
76 | has_many :keys, dependent: :destroy | 73 | has_many :keys, dependent: :destroy |
77 | 74 | ||
78 | # Groups | 75 | # Groups |
79 | - has_many :own_groups, class_name: "Group", foreign_key: :owner_id | ||
80 | - has_many :owned_groups, through: :users_groups, source: :group, conditions: { users_groups: { group_access: UsersGroup::OWNER } } | ||
81 | - | ||
82 | has_many :users_groups, dependent: :destroy | 76 | has_many :users_groups, dependent: :destroy |
83 | has_many :groups, through: :users_groups | 77 | has_many :groups, through: :users_groups |
78 | + has_many :owned_groups, through: :users_groups, source: :group, conditions: { users_groups: { group_access: UsersGroup::OWNER } } | ||
84 | 79 | ||
85 | # Projects | 80 | # Projects |
81 | + has_many :groups_projects, through: :groups, source: :projects | ||
82 | + has_many :personal_projects, through: :namespace, source: :projects | ||
83 | + has_many :projects, through: :users_projects | ||
84 | + has_many :created_projects, foreign_key: :creator_id, class_name: 'Project' | ||
85 | + has_many :owned_projects, through: :owned_groups, source: :projects | ||
86 | + | ||
87 | + | ||
86 | has_many :snippets, dependent: :destroy, foreign_key: :author_id, class_name: "Snippet" | 88 | has_many :snippets, dependent: :destroy, foreign_key: :author_id, class_name: "Snippet" |
87 | has_many :users_projects, dependent: :destroy | 89 | has_many :users_projects, dependent: :destroy |
88 | has_many :issues, dependent: :destroy, foreign_key: :author_id | 90 | has_many :issues, dependent: :destroy, foreign_key: :author_id |
@@ -93,11 +95,6 @@ class User < ActiveRecord::Base | @@ -93,11 +95,6 @@ class User < ActiveRecord::Base | ||
93 | has_many :assigned_issues, dependent: :destroy, foreign_key: :assignee_id, class_name: "Issue" | 95 | has_many :assigned_issues, dependent: :destroy, foreign_key: :assignee_id, class_name: "Issue" |
94 | has_many :assigned_merge_requests, dependent: :destroy, foreign_key: :assignee_id, class_name: "MergeRequest" | 96 | has_many :assigned_merge_requests, dependent: :destroy, foreign_key: :assignee_id, class_name: "MergeRequest" |
95 | 97 | ||
96 | - has_many :groups_projects, through: :groups, source: :projects | ||
97 | - has_many :personal_projects, through: :namespace, source: :projects | ||
98 | - has_many :projects, through: :users_projects | ||
99 | - has_many :own_projects, foreign_key: :creator_id, class_name: 'Project' | ||
100 | - has_many :owned_projects, through: :namespaces, source: :projects | ||
101 | 98 | ||
102 | # | 99 | # |
103 | # Validations | 100 | # Validations |
@@ -247,7 +244,7 @@ class User < ActiveRecord::Base | @@ -247,7 +244,7 @@ class User < ActiveRecord::Base | ||
247 | # Groups user has access to | 244 | # Groups user has access to |
248 | def authorized_groups | 245 | def authorized_groups |
249 | @authorized_groups ||= begin | 246 | @authorized_groups ||= begin |
250 | - group_ids = (groups.pluck(:id) + own_groups.pluck(:id) + authorized_projects.pluck(:namespace_id)) | 247 | + group_ids = (groups.pluck(:id) + authorized_projects.pluck(:namespace_id)) |
251 | Group.where(id: group_ids).order('namespaces.name ASC') | 248 | Group.where(id: group_ids).order('namespaces.name ASC') |
252 | end | 249 | end |
253 | end | 250 | end |
@@ -256,7 +253,7 @@ class User < ActiveRecord::Base | @@ -256,7 +253,7 @@ class User < ActiveRecord::Base | ||
256 | # Projects user has access to | 253 | # Projects user has access to |
257 | def authorized_projects | 254 | def authorized_projects |
258 | @authorized_projects ||= begin | 255 | @authorized_projects ||= begin |
259 | - project_ids = (owned_projects.pluck(:id) + groups_projects.pluck(:id) + projects.pluck(:id)).uniq | 256 | + project_ids = (personal_projects.pluck(:id) + groups_projects.pluck(:id) + projects.pluck(:id)).uniq |
260 | Project.where(id: project_ids).joins(:namespace).order('namespaces.name ASC') | 257 | Project.where(id: project_ids).joins(:namespace).order('namespaces.name ASC') |
261 | end | 258 | end |
262 | end | 259 | end |