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 | 69 | # Namespace for personal projects |
70 | 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 | 72 | # Profile |
76 | 73 | has_many :keys, dependent: :destroy |
77 | 74 | |
78 | 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 | 76 | has_many :users_groups, dependent: :destroy |
83 | 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 | 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 | 88 | has_many :snippets, dependent: :destroy, foreign_key: :author_id, class_name: "Snippet" |
87 | 89 | has_many :users_projects, dependent: :destroy |
88 | 90 | has_many :issues, dependent: :destroy, foreign_key: :author_id |
... | ... | @@ -93,11 +95,6 @@ class User < ActiveRecord::Base |
93 | 95 | has_many :assigned_issues, dependent: :destroy, foreign_key: :assignee_id, class_name: "Issue" |
94 | 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 | 100 | # Validations |
... | ... | @@ -247,7 +244,7 @@ class User < ActiveRecord::Base |
247 | 244 | # Groups user has access to |
248 | 245 | def authorized_groups |
249 | 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 | 248 | Group.where(id: group_ids).order('namespaces.name ASC') |
252 | 249 | end |
253 | 250 | end |
... | ... | @@ -256,7 +253,7 @@ class User < ActiveRecord::Base |
256 | 253 | # Projects user has access to |
257 | 254 | def authorized_projects |
258 | 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 | 257 | Project.where(id: project_ids).joins(:namespace).order('namespaces.name ASC') |
261 | 258 | end |
262 | 259 | end | ... | ... |