Commit 41bbbb6df0300579867c4654d8662b38fa9f0a58
1 parent
7af16bbb
Exists in
master
and in
4 other branches
Update model methods
Showing
7 changed files
with
43 additions
and
104 deletions
Show diff stats
app/controllers/admin/users_controller.rb
| ... | ... | @@ -14,7 +14,7 @@ class Admin::UsersController < Admin::ApplicationController |
| 14 | 14 | @not_in_projects = @not_in_projects.without_user(admin_user) if admin_user.authorized_projects.present? |
| 15 | 15 | |
| 16 | 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 | 18 | end |
| 19 | 19 | |
| 20 | 20 | def team_update | ... | ... |
app/models/group.rb
app/models/issue.rb
| ... | ... | @@ -25,19 +25,9 @@ class Issue < ActiveRecord::Base |
| 25 | 25 | |
| 26 | 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 | 32 | state_machine :state, initial: :opened do |
| 43 | 33 | event :close do | ... | ... |
app/models/key.rb
| ... | ... | @@ -23,7 +23,7 @@ class Key < ActiveRecord::Base |
| 23 | 23 | before_validation :strip_white_space |
| 24 | 24 | |
| 25 | 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 | 27 | validate :fingerprintable_key |
| 28 | 28 | |
| 29 | 29 | delegate :name, :email, to: :user, prefix: true |
| ... | ... | @@ -48,7 +48,7 @@ class Key < ActiveRecord::Base |
| 48 | 48 | end |
| 49 | 49 | |
| 50 | 50 | def is_deploy_key |
| 51 | - !!project_id | |
| 51 | + project.present? | |
| 52 | 52 | end |
| 53 | 53 | |
| 54 | 54 | # projects that has this key | ... | ... |
app/models/milestone.rb
| ... | ... | @@ -19,6 +19,7 @@ class Milestone < ActiveRecord::Base |
| 19 | 19 | belongs_to :project |
| 20 | 20 | has_many :issues |
| 21 | 21 | has_many :merge_requests |
| 22 | + has_many :participants, through: :issues, source: :assignee | |
| 22 | 23 | |
| 23 | 24 | scope :active, -> { with_state(:active) } |
| 24 | 25 | scope :closed, -> { with_state(:closed) } |
| ... | ... | @@ -48,10 +49,6 @@ class Milestone < ActiveRecord::Base |
| 48 | 49 | end |
| 49 | 50 | end |
| 50 | 51 | |
| 51 | - def participants | |
| 52 | - User.where(id: issues.pluck(:assignee_id)) | |
| 53 | - end | |
| 54 | - | |
| 55 | 52 | def open_items_count |
| 56 | 53 | self.issues.opened.count + self.merge_requests.opened.count |
| 57 | 54 | end | ... | ... |
app/models/project.rb
| ... | ... | @@ -87,11 +87,12 @@ class Project < ActiveRecord::Base |
| 87 | 87 | |
| 88 | 88 | # Scopes |
| 89 | 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 | 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 | 92 | scope :in_team, ->(team) { where("id IN (:ids)", ids: team.projects.map(&:id)) } |
| 93 | 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 | 96 | scope :personal, ->(user) { where(namespace_id: user.namespace_id) } |
| 96 | 97 | scope :joined, ->(user) { where("namespace_id != ?", user.namespace_id) } |
| 97 | 98 | scope :public_only, -> { where(public: true) } |
| ... | ... | @@ -156,7 +157,8 @@ class Project < ActiveRecord::Base |
| 156 | 157 | unless creator.can_create_project? |
| 157 | 158 | errors[:limit_reached] << ("Your own projects limit is #{creator.projects_limit}! Please contact administrator to increase it") |
| 158 | 159 | end |
| 159 | - rescue | |
| 160 | + rescue => ex | |
| 161 | + errors[:base] << ex.message | |
| 160 | 162 | errors[:base] << ("Can't check your ability to create project") |
| 161 | 163 | end |
| 162 | 164 | ... | ... |
app/models/user.rb
| ... | ... | @@ -59,11 +59,10 @@ class User < ActiveRecord::Base |
| 59 | 59 | # |
| 60 | 60 | |
| 61 | 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 | 67 | # Profile |
| 69 | 68 | has_many :keys, dependent: :destroy |
| ... | ... | @@ -72,15 +71,11 @@ class User < ActiveRecord::Base |
| 72 | 71 | has_many :groups, class_name: "Group", foreign_key: :owner_id |
| 73 | 72 | |
| 74 | 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 | 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 | 80 | # Projects |
| 86 | 81 | has_many :users_projects, dependent: :destroy |
| ... | ... | @@ -88,14 +83,14 @@ class User < ActiveRecord::Base |
| 88 | 83 | has_many :notes, dependent: :destroy, foreign_key: :author_id |
| 89 | 84 | has_many :merge_requests, dependent: :destroy, foreign_key: :author_id |
| 90 | 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 | 87 | has_many :assigned_issues, dependent: :destroy, foreign_key: :assignee_id, class_name: "Issue" |
| 92 | 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 | 96 | # Validations |
| ... | ... | @@ -109,9 +104,7 @@ class User < ActiveRecord::Base |
| 109 | 104 | format: { with: Gitlab::Regex.username_regex, |
| 110 | 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 | 109 | validate :namespace_uniq, if: ->(user) { user.username_changed? } |
| 117 | 110 | |
| ... | ... | @@ -145,6 +138,9 @@ class User < ActiveRecord::Base |
| 145 | 138 | scope :alphabetically, -> { order('name ASC') } |
| 146 | 139 | scope :in_team, ->(team){ where(id: team.member_ids) } |
| 147 | 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 | 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 < ActiveRecord::Base |
| 171 | 167 | end |
| 172 | 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 | 170 | def create_from_omniauth(auth, ldap = false) |
| 187 | 171 | gitlab_auth.create_from_omniauth(auth, ldap) |
| 188 | 172 | end |
| ... | ... | @@ -229,56 +213,36 @@ class User < ActiveRecord::Base |
| 229 | 213 | end |
| 230 | 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 | 216 | # Groups where user is an owner |
| 246 | 217 | def owned_groups |
| 247 | 218 | groups |
| 248 | 219 | end |
| 249 | 220 | |
| 221 | + def owned_teams | |
| 222 | + own_teams | |
| 223 | + end | |
| 224 | + | |
| 250 | 225 | # Groups user has access to |
| 251 | 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 | 229 | end |
| 258 | 230 | |
| 259 | 231 | |
| 260 | 232 | # Projects user has access to |
| 261 | 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 | 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 | 241 | end |
| 278 | 242 | |
| 279 | 243 | # Team membership in authorized projects |
| 280 | 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 | 246 | end |
| 283 | 247 | |
| 284 | 248 | def is_admin? |
| ... | ... | @@ -344,28 +308,13 @@ class User < ActiveRecord::Base |
| 344 | 308 | end |
| 345 | 309 | |
| 346 | 310 | def several_namespaces? |
| 347 | - namespaces.size > 1 | |
| 311 | + namespaces.many? | |
| 348 | 312 | end |
| 349 | 313 | |
| 350 | 314 | def namespace_id |
| 351 | 315 | namespace.try :id |
| 352 | 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 | 318 | def name_with_username |
| 370 | 319 | "#{name} (#{username})" |
| 371 | 320 | end | ... | ... |