Commit 41bbbb6df0300579867c4654d8662b38fa9f0a58

Authored by Andrey Kumanyaev
1 parent 7af16bbb

Update model methods

app/controllers/admin/users_controller.rb
@@ -14,7 +14,7 @@ class Admin::UsersController < Admin::ApplicationController @@ -14,7 +14,7 @@ class Admin::UsersController < Admin::ApplicationController
14 @not_in_projects = @not_in_projects.without_user(admin_user) if admin_user.authorized_projects.present? 14 @not_in_projects = @not_in_projects.without_user(admin_user) if admin_user.authorized_projects.present?
15 15
16 # Projects he already own or joined 16 # Projects he already own or joined
17 - @projects = admin_user.authorized_projects.where('projects.id in (?)', admin_user.authorized_projects.map(&:id)) 17 + @projects = admin_user.authorized_projects
18 end 18 end
19 19
20 def team_update 20 def team_update
app/models/group.rb
@@ -13,6 +13,7 @@ @@ -13,6 +13,7 @@
13 # 13 #
14 14
15 class Group < Namespace 15 class Group < Namespace
  16 +
16 def add_users_to_project_teams(user_ids, project_access) 17 def add_users_to_project_teams(user_ids, project_access)
17 UsersProject.add_users_into_projects( 18 UsersProject.add_users_into_projects(
18 projects.map(&:id), 19 projects.map(&:id),
app/models/issue.rb
@@ -25,19 +25,9 @@ class Issue &lt; ActiveRecord::Base @@ -25,19 +25,9 @@ class Issue &lt; ActiveRecord::Base
25 25
26 acts_as_taggable_on :labels 26 acts_as_taggable_on :labels
27 27
28 - class << self  
29 - def cared(user)  
30 - where('assignee_id = :user', user: user.id)  
31 - end  
32 -  
33 - def authored(user)  
34 - where('author_id = :user', user: user.id)  
35 - end  
36 -  
37 - def open_for(user)  
38 - opened.assigned(user)  
39 - end  
40 - end 28 + scope :cared, ->(user) { where(assignee_id: user) }
  29 + scope :authored, ->(user) { where(author_id: user) }
  30 + scope :open_for, ->(user) { opened.assigned(user) }
41 31
42 state_machine :state, initial: :opened do 32 state_machine :state, initial: :opened do
43 event :close do 33 event :close do
app/models/key.rb
@@ -23,7 +23,7 @@ class Key &lt; ActiveRecord::Base @@ -23,7 +23,7 @@ class Key &lt; ActiveRecord::Base
23 before_validation :strip_white_space 23 before_validation :strip_white_space
24 24
25 validates :title, presence: true, length: { within: 0..255 } 25 validates :title, presence: true, length: { within: 0..255 }
26 - validates :key, presence: true, length: { within: 0..5000 }, format: { :with => /ssh-.{3} / }, uniqueness: true 26 + validates :key, presence: true, length: { within: 0..5000 }, format: { with: /ssh-.{3} / }, uniqueness: true
27 validate :fingerprintable_key 27 validate :fingerprintable_key
28 28
29 delegate :name, :email, to: :user, prefix: true 29 delegate :name, :email, to: :user, prefix: true
@@ -48,7 +48,7 @@ class Key &lt; ActiveRecord::Base @@ -48,7 +48,7 @@ class Key &lt; ActiveRecord::Base
48 end 48 end
49 49
50 def is_deploy_key 50 def is_deploy_key
51 - !!project_id 51 + project.present?
52 end 52 end
53 53
54 # projects that has this key 54 # projects that has this key
app/models/milestone.rb
@@ -19,6 +19,7 @@ class Milestone &lt; ActiveRecord::Base @@ -19,6 +19,7 @@ class Milestone &lt; ActiveRecord::Base
19 belongs_to :project 19 belongs_to :project
20 has_many :issues 20 has_many :issues
21 has_many :merge_requests 21 has_many :merge_requests
  22 + has_many :participants, through: :issues, source: :assignee
22 23
23 scope :active, -> { with_state(:active) } 24 scope :active, -> { with_state(:active) }
24 scope :closed, -> { with_state(:closed) } 25 scope :closed, -> { with_state(:closed) }
@@ -48,10 +49,6 @@ class Milestone &lt; ActiveRecord::Base @@ -48,10 +49,6 @@ class Milestone &lt; ActiveRecord::Base
48 end 49 end
49 end 50 end
50 51
51 - def participants  
52 - User.where(id: issues.pluck(:assignee_id))  
53 - end  
54 -  
55 def open_items_count 52 def open_items_count
56 self.issues.opened.count + self.merge_requests.opened.count 53 self.issues.opened.count + self.merge_requests.opened.count
57 end 54 end
app/models/project.rb
@@ -87,11 +87,12 @@ class Project &lt; ActiveRecord::Base @@ -87,11 +87,12 @@ class Project &lt; ActiveRecord::Base
87 87
88 # Scopes 88 # Scopes
89 scope :without_user, ->(user) { where("id NOT IN (:ids)", ids: user.authorized_projects.map(&:id) ) } 89 scope :without_user, ->(user) { where("id NOT IN (:ids)", ids: user.authorized_projects.map(&:id) ) }
90 - scope :not_in_group, ->(group) { where("id NOT IN (:ids)", ids: group.project_ids ) }  
91 scope :without_team, ->(team) { team.projects.present? ? where("id NOT IN (:ids)", ids: team.projects.map(&:id)) : scoped } 90 scope :without_team, ->(team) { team.projects.present? ? where("id NOT IN (:ids)", ids: team.projects.map(&:id)) : scoped }
  91 + scope :not_in_group, ->(group) { where("id NOT IN (:ids)", ids: group.project_ids ) }
92 scope :in_team, ->(team) { where("id IN (:ids)", ids: team.projects.map(&:id)) } 92 scope :in_team, ->(team) { where("id IN (:ids)", ids: team.projects.map(&:id)) }
93 scope :in_namespace, ->(namespace) { where(namespace_id: namespace.id) } 93 scope :in_namespace, ->(namespace) { where(namespace_id: namespace.id) }
94 - scope :sorted_by_activity, ->() { order("(SELECT max(events.created_at) FROM events WHERE events.project_id = projects.id) DESC") } 94 + scope :in_group_namespace, -> { joins(:group) }
  95 + scope :sorted_by_activity, -> { order("(SELECT max(events.created_at) FROM events WHERE events.project_id = projects.id) DESC") }
95 scope :personal, ->(user) { where(namespace_id: user.namespace_id) } 96 scope :personal, ->(user) { where(namespace_id: user.namespace_id) }
96 scope :joined, ->(user) { where("namespace_id != ?", user.namespace_id) } 97 scope :joined, ->(user) { where("namespace_id != ?", user.namespace_id) }
97 scope :public_only, -> { where(public: true) } 98 scope :public_only, -> { where(public: true) }
@@ -156,7 +157,8 @@ class Project &lt; ActiveRecord::Base @@ -156,7 +157,8 @@ class Project &lt; ActiveRecord::Base
156 unless creator.can_create_project? 157 unless creator.can_create_project?
157 errors[:limit_reached] << ("Your own projects limit is #{creator.projects_limit}! Please contact administrator to increase it") 158 errors[:limit_reached] << ("Your own projects limit is #{creator.projects_limit}! Please contact administrator to increase it")
158 end 159 end
159 - rescue 160 + rescue => ex
  161 + errors[:base] << ex.message
160 errors[:base] << ("Can't check your ability to create project") 162 errors[:base] << ("Can't check your ability to create project")
161 end 163 end
162 164
app/models/user.rb
@@ -59,11 +59,10 @@ class User &lt; ActiveRecord::Base @@ -59,11 +59,10 @@ class User &lt; ActiveRecord::Base
59 # 59 #
60 60
61 # Namespace for personal projects 61 # Namespace for personal projects
62 - has_one :namespace,  
63 - dependent: :destroy,  
64 - foreign_key: :owner_id,  
65 - class_name: "Namespace",  
66 - conditions: 'type IS NULL' 62 + has_one :namespace, dependent: :destroy, foreign_key: :owner_id, class_name: "Namespace", conditions: 'type IS NULL'
  63 +
  64 + # Namespaces (owned groups and own namespace)
  65 + has_many :namespaces, foreign_key: :owner_id
67 66
68 # Profile 67 # Profile
69 has_many :keys, dependent: :destroy 68 has_many :keys, dependent: :destroy
@@ -72,15 +71,11 @@ class User &lt; ActiveRecord::Base @@ -72,15 +71,11 @@ class User &lt; ActiveRecord::Base
72 has_many :groups, class_name: "Group", foreign_key: :owner_id 71 has_many :groups, class_name: "Group", foreign_key: :owner_id
73 72
74 # Teams 73 # Teams
75 - has_many :own_teams,  
76 - class_name: "UserTeam",  
77 - foreign_key: :owner_id,  
78 - dependent: :destroy  
79 -  
80 - has_many :user_team_user_relationships, dependent: :destroy  
81 - has_many :user_teams, through: :user_team_user_relationships 74 + has_many :own_teams, dependent: :destroy, class_name: "UserTeam", foreign_key: :owner_id
  75 + has_many :user_team_user_relationships, dependent: :destroy
  76 + has_many :user_teams, through: :user_team_user_relationships
82 has_many :user_team_project_relationships, through: :user_teams 77 has_many :user_team_project_relationships, through: :user_teams
83 - has_many :team_projects, through: :user_team_project_relationships 78 + has_many :team_projects, through: :user_team_project_relationships
84 79
85 # Projects 80 # Projects
86 has_many :users_projects, dependent: :destroy 81 has_many :users_projects, dependent: :destroy
@@ -88,14 +83,14 @@ class User &lt; ActiveRecord::Base @@ -88,14 +83,14 @@ class User &lt; ActiveRecord::Base
88 has_many :notes, dependent: :destroy, foreign_key: :author_id 83 has_many :notes, dependent: :destroy, foreign_key: :author_id
89 has_many :merge_requests, dependent: :destroy, foreign_key: :author_id 84 has_many :merge_requests, dependent: :destroy, foreign_key: :author_id
90 has_many :events, dependent: :destroy, foreign_key: :author_id, class_name: "Event" 85 has_many :events, dependent: :destroy, foreign_key: :author_id, class_name: "Event"
  86 + has_many :recent_events, foreign_key: :author_id, class_name: "Event", order: "id DESC"
91 has_many :assigned_issues, dependent: :destroy, foreign_key: :assignee_id, class_name: "Issue" 87 has_many :assigned_issues, dependent: :destroy, foreign_key: :assignee_id, class_name: "Issue"
92 has_many :assigned_merge_requests, dependent: :destroy, foreign_key: :assignee_id, class_name: "MergeRequest" 88 has_many :assigned_merge_requests, dependent: :destroy, foreign_key: :assignee_id, class_name: "MergeRequest"
93 - has_many :projects, through: :users_projects  
94 89
95 - has_many :recent_events,  
96 - class_name: "Event",  
97 - foreign_key: :author_id,  
98 - order: "id DESC" 90 + has_many :personal_projects, through: :namespace, source: :projects
  91 + has_many :projects, through: :users_projects
  92 + has_many :own_projects, foreign_key: :creator_id
  93 + has_many :owned_projects, through: :namespaces, source: :projects
99 94
100 # 95 #
101 # Validations 96 # Validations
@@ -109,9 +104,7 @@ class User &lt; ActiveRecord::Base @@ -109,9 +104,7 @@ class User &lt; ActiveRecord::Base
109 format: { with: Gitlab::Regex.username_regex, 104 format: { with: Gitlab::Regex.username_regex,
110 message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" } 105 message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
111 106
112 - validates :notification_level,  
113 - inclusion: { in: Notification.notification_levels },  
114 - presence: true 107 + validates :notification_level, inclusion: { in: Notification.notification_levels }, presence: true
115 108
116 validate :namespace_uniq, if: ->(user) { user.username_changed? } 109 validate :namespace_uniq, if: ->(user) { user.username_changed? }
117 110
@@ -145,6 +138,9 @@ class User &lt; ActiveRecord::Base @@ -145,6 +138,9 @@ class User &lt; ActiveRecord::Base
145 scope :alphabetically, -> { order('name ASC') } 138 scope :alphabetically, -> { order('name ASC') }
146 scope :in_team, ->(team){ where(id: team.member_ids) } 139 scope :in_team, ->(team){ where(id: team.member_ids) }
147 scope :not_in_team, ->(team){ where('users.id NOT IN (:ids)', ids: team.member_ids) } 140 scope :not_in_team, ->(team){ where('users.id NOT IN (:ids)', ids: team.member_ids) }
  141 + scope :not_in_project, ->(project) { project.users.present? ? where("id not in (:ids)", ids: project.users.map(&:id) ) : scoped }
  142 + scope :without_projects, -> { where('id NOT IN (SELECT DISTINCT(user_id) FROM users_projects)') }
  143 +
148 scope :potential_team_members, ->(team) { team.members.any? ? active.not_in_team(team) : active } 144 scope :potential_team_members, ->(team) { team.members.any? ? active.not_in_team(team) : active }
149 145
150 # 146 #
@@ -171,18 +167,6 @@ class User &lt; ActiveRecord::Base @@ -171,18 +167,6 @@ class User &lt; ActiveRecord::Base
171 end 167 end
172 end 168 end
173 169
174 - def not_in_project(project)  
175 - if project.users.present?  
176 - where("id not in (:ids)", ids: project.users.map(&:id) )  
177 - else  
178 - scoped  
179 - end  
180 - end  
181 -  
182 - def without_projects  
183 - where('id NOT IN (SELECT DISTINCT(user_id) FROM users_projects)')  
184 - end  
185 -  
186 def create_from_omniauth(auth, ldap = false) 170 def create_from_omniauth(auth, ldap = false)
187 gitlab_auth.create_from_omniauth(auth, ldap) 171 gitlab_auth.create_from_omniauth(auth, ldap)
188 end 172 end
@@ -229,56 +213,36 @@ class User &lt; ActiveRecord::Base @@ -229,56 +213,36 @@ class User &lt; ActiveRecord::Base
229 end 213 end
230 end 214 end
231 215
232 - # Namespaces user has access to  
233 - def namespaces  
234 - namespaces = []  
235 -  
236 - # Add user account namespace  
237 - namespaces << self.namespace if self.namespace  
238 -  
239 - # Add groups you can manage  
240 - namespaces += groups.all  
241 -  
242 - namespaces  
243 - end  
244 -  
245 # Groups where user is an owner 216 # Groups where user is an owner
246 def owned_groups 217 def owned_groups
247 groups 218 groups
248 end 219 end
249 220
  221 + def owned_teams
  222 + own_teams
  223 + end
  224 +
250 # Groups user has access to 225 # Groups user has access to
251 def authorized_groups 226 def authorized_groups
252 - @authorized_groups ||= begin  
253 - groups = Group.where(id: self.authorized_projects.pluck(:namespace_id)).all  
254 - groups = groups + self.groups  
255 - groups.uniq  
256 - end 227 + @group_ids ||= (groups.pluck(:id) + authorized_projects.pluck(:namespace_id))
  228 + Group.where(id: @group_ids)
257 end 229 end
258 230
259 231
260 # Projects user has access to 232 # Projects user has access to
261 def authorized_projects 233 def authorized_projects
262 - project_ids = users_projects.pluck(:project_id)  
263 - project_ids = project_ids | owned_projects.pluck(:id)  
264 - Project.where(id: project_ids) 234 + @project_ids ||= (owned_projects.pluck(:id) + projects.pluck(:id)).uniq
  235 + Project.where(id: @project_ids)
265 end 236 end
266 237
267 - # Projects in user namespace  
268 - def personal_projects  
269 - Project.personal(self)  
270 - end  
271 -  
272 - # Projects where user is an owner  
273 - def owned_projects  
274 - Project.where("(projects.namespace_id IN (:namespaces)) OR  
275 - (projects.namespace_id IS NULL AND projects.creator_id = :user_id)",  
276 - namespaces: namespaces.map(&:id), user_id: self.id) 238 + def authorized_teams
  239 + @team_ids ||= (user_teams.pluck(:id) + own_teams.pluck(:id)).uniq
  240 + UserTeam.where(id: @team_ids)
277 end 241 end
278 242
279 # Team membership in authorized projects 243 # Team membership in authorized projects
280 def tm_in_authorized_projects 244 def tm_in_authorized_projects
281 - UsersProject.where(project_id: authorized_projects.map(&:id), user_id: self.id) 245 + UsersProject.where(project_id: authorized_projects.map(&:id), user_id: self.id)
282 end 246 end
283 247
284 def is_admin? 248 def is_admin?
@@ -344,28 +308,13 @@ class User &lt; ActiveRecord::Base @@ -344,28 +308,13 @@ class User &lt; ActiveRecord::Base
344 end 308 end
345 309
346 def several_namespaces? 310 def several_namespaces?
347 - namespaces.size > 1 311 + namespaces.many?
348 end 312 end
349 313
350 def namespace_id 314 def namespace_id
351 namespace.try :id 315 namespace.try :id
352 end 316 end
353 317
354 - def authorized_teams  
355 - @authorized_teams ||= begin  
356 - ids = []  
357 - ids << UserTeam.with_member(self).pluck('user_teams.id')  
358 - ids << UserTeam.created_by(self).pluck('user_teams.id')  
359 - ids.flatten  
360 -  
361 - UserTeam.where(id: ids)  
362 - end  
363 - end  
364 -  
365 - def owned_teams  
366 - UserTeam.where(owner_id: self.id)  
367 - end  
368 -  
369 def name_with_username 318 def name_with_username
370 "#{name} (#{username})" 319 "#{name} (#{username})"
371 end 320 end