Commit 0f2798d498c69372137d13a8ba3ec8e87b7f0a8a
Exists in
master
and in
4 other branches
Merge branch 'feature/refactoring_scopes_pr' of https://github.com/Undev/gitlabh…
…q into Undev-feature/refactoring_scopes_pr Conflicts: db/schema.rb
Showing
12 changed files
with
73 additions
and
112 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
... | ... | @@ -30,7 +30,7 @@ class Project < ActiveRecord::Base |
30 | 30 | |
31 | 31 | attr_accessible :name, :path, :description, :default_branch, :issues_tracker, |
32 | 32 | :issues_enabled, :wall_enabled, :merge_requests_enabled, :snippets_enabled, :issues_tracker_id, |
33 | - :wiki_enabled, :public, :import_url, as: [:default, :admin] | |
33 | + :wiki_enabled, :public, :import_url, :last_activity_at, as: [:default, :admin] | |
34 | 34 | |
35 | 35 | attr_accessible :namespace_id, :creator_id, as: :admin |
36 | 36 | |
... | ... | @@ -87,17 +87,18 @@ class Project < ActiveRecord::Base |
87 | 87 | validate :check_limit, :repo_name |
88 | 88 | |
89 | 89 | # Scopes |
90 | - scope :without_user, ->(user) { where("id NOT IN (:ids)", ids: user.authorized_projects.map(&:id) ) } | |
91 | - scope :not_in_group, ->(group) { where("id NOT IN (:ids)", ids: group.project_ids ) } | |
92 | - scope :without_team, ->(team) { team.projects.present? ? where("id NOT IN (:ids)", ids: team.projects.map(&:id)) : scoped } | |
93 | - scope :in_team, ->(team) { where("id IN (:ids)", ids: team.projects.map(&:id)) } | |
90 | + scope :without_user, ->(user) { where("projects.id NOT IN (:ids)", ids: user.authorized_projects.map(&:id) ) } | |
91 | + scope :without_team, ->(team) { team.projects.present? ? where("projects.id NOT IN (:ids)", ids: team.projects.map(&:id)) : scoped } | |
92 | + scope :not_in_group, ->(group) { where("projects.id NOT IN (:ids)", ids: group.project_ids ) } | |
93 | + scope :in_team, ->(team) { where("projects.id IN (:ids)", ids: team.projects.map(&:id)) } | |
94 | 94 | scope :in_namespace, ->(namespace) { where(namespace_id: namespace.id) } |
95 | - scope :sorted_by_activity, ->() { order("(SELECT max(events.created_at) FROM events WHERE events.project_id = projects.id) DESC") } | |
95 | + scope :in_group_namespace, -> { joins(:group) } | |
96 | + scope :sorted_by_activity, -> { order("projects.last_activity_at DESC") } | |
96 | 97 | scope :personal, ->(user) { where(namespace_id: user.namespace_id) } |
97 | 98 | scope :joined, ->(user) { where("namespace_id != ?", user.namespace_id) } |
98 | 99 | scope :public_only, -> { where(public: true) } |
99 | 100 | |
100 | - enumerize :issues_tracker, :in => (Gitlab.config.issues_tracker.keys).append(:gitlab), :default => :gitlab | |
101 | + enumerize :issues_tracker, in: (Gitlab.config.issues_tracker.keys).append(:gitlab), default: :gitlab | |
101 | 102 | |
102 | 103 | class << self |
103 | 104 | def abandoned |
... | ... | @@ -190,7 +191,7 @@ class Project < ActiveRecord::Base |
190 | 191 | end |
191 | 192 | |
192 | 193 | def last_activity_date |
193 | - last_event.try(:created_at) || updated_at | |
194 | + last_activity_at || updated_at | |
194 | 195 | end |
195 | 196 | |
196 | 197 | def project_id | ... | ... |
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? |
... | ... | @@ -348,28 +312,13 @@ class User < ActiveRecord::Base |
348 | 312 | end |
349 | 313 | |
350 | 314 | def several_namespaces? |
351 | - namespaces.size > 1 | |
315 | + namespaces.many? | |
352 | 316 | end |
353 | 317 | |
354 | 318 | def namespace_id |
355 | 319 | namespace.try :id |
356 | 320 | end |
357 | 321 | |
358 | - def authorized_teams | |
359 | - @authorized_teams ||= begin | |
360 | - ids = [] | |
361 | - ids << UserTeam.with_member(self).pluck('user_teams.id') | |
362 | - ids << UserTeam.created_by(self).pluck('user_teams.id') | |
363 | - ids.flatten | |
364 | - | |
365 | - UserTeam.where(id: ids) | |
366 | - end | |
367 | - end | |
368 | - | |
369 | - def owned_teams | |
370 | - UserTeam.where(owner_id: self.id) | |
371 | - end | |
372 | - | |
373 | 322 | def name_with_username |
374 | 323 | "#{name} (#{username})" |
375 | 324 | end | ... | ... |
config/application.rb
db/migrate/20130403003950_add_last_activity_column_into_project.rb
0 → 100644
... | ... | @@ -0,0 +1,15 @@ |
1 | +class AddLastActivityColumnIntoProject < ActiveRecord::Migration | |
2 | + def up | |
3 | + add_column :projects, :last_activity_at, :datetime | |
4 | + add_index :projects, :last_activity_at | |
5 | + | |
6 | + Project.find_each do |project| | |
7 | + project.update_attribute(:last_activity_at, project.last_activity_date) | |
8 | + end | |
9 | + end | |
10 | + | |
11 | + def down | |
12 | + remove_index :projects, :last_activity_at | |
13 | + remove_column :projects, :last_activity_at | |
14 | + end | |
15 | +end | ... | ... |
db/schema.rb
... | ... | @@ -12,7 +12,6 @@ |
12 | 12 | # It's strongly recommended to check this file into your version control system. |
13 | 13 | |
14 | 14 | ActiveRecord::Schema.define(:version => 20130404164628) do |
15 | - | |
16 | 15 | create_table "events", :force => true do |t| |
17 | 16 | t.string "target_type" |
18 | 17 | t.integer "target_id" | ... | ... |
spec/models/project_spec.rb
... | ... | @@ -109,8 +109,8 @@ describe Project do |
109 | 109 | |
110 | 110 | describe 'last_activity_date' do |
111 | 111 | it 'returns the creation date of the project\'s last event if present' do |
112 | - project.stub(last_event: last_event) | |
113 | - project.last_activity_date.should == last_event.created_at | |
112 | + last_activity_event = create(:event, project: project) | |
113 | + project.last_activity_date.to_s(:db).should == last_event.created_at.to_s(:db) | |
114 | 114 | end |
115 | 115 | |
116 | 116 | it 'returns the project\'s last update date if it has no events' do | ... | ... |