Commit 1d889a79fb85a39bf240444b1beaa23efec035c3

Authored by Dmitriy Zaporozhets
1 parent ff9a2e2a

Fix group duplication on dashboard and project order in group

app/controllers/groups_controller.rb
@@ -54,10 +54,12 @@ class GroupsController < ApplicationController @@ -54,10 +54,12 @@ class GroupsController < ApplicationController
54 end 54 end
55 55
56 def projects 56 def projects
57 - @projects ||= if can?(current_user, :manage_group, @group)  
58 - @group.projects.all  
59 - else  
60 - current_user.projects_sorted_by_activity.where(namespace_id: @group.id) 57 + @projects ||= begin
  58 + if can?(current_user, :manage_group, @group)
  59 + @group.projects
  60 + else
  61 + current_user.projects.where(namespace_id: @group.id)
  62 + end.sorted_by_activity.all
61 end 63 end
62 end 64 end
63 65
app/models/project.rb
@@ -73,7 +73,7 @@ class Project < ActiveRecord::Base @@ -73,7 +73,7 @@ class Project < ActiveRecord::Base
73 scope :public_only, where(private_flag: false) 73 scope :public_only, where(private_flag: false)
74 scope :without_user, ->(user) { where("id NOT IN (:ids)", ids: user.projects.map(&:id) ) } 74 scope :without_user, ->(user) { where("id NOT IN (:ids)", ids: user.projects.map(&:id) ) }
75 scope :not_in_group, ->(group) { where("id NOT IN (:ids)", ids: group.project_ids ) } 75 scope :not_in_group, ->(group) { where("id NOT IN (:ids)", ids: group.project_ids ) }
76 - scope :authorized_for, ->(user) { joins(:users_projects) { where(user_id: user.id) } } 76 + scope :sorted_by_activity, ->() { order("(SELECT max(events.created_at) FROM events WHERE events.project_id = projects.id) DESC") }
77 77
78 class << self 78 class << self
79 def active 79 def active
@@ -285,4 +285,9 @@ class Project &lt; ActiveRecord::Base @@ -285,4 +285,9 @@ class Project &lt; ActiveRecord::Base
285 merge_requests 285 merge_requests
286 end 286 end
287 end 287 end
  288 +
  289 + def self.authorized_for user
  290 + projects = includes(:users_projects, :namespace)
  291 + projects = projects.where("users_projects.user_id = :user_id or projects.owner_id = :user_id or namespaces.owner_id = :user_id", user_id: user.id)
  292 + end
288 end 293 end
app/models/user.rb
@@ -127,7 +127,8 @@ class User &lt; ActiveRecord::Base @@ -127,7 +127,8 @@ class User &lt; ActiveRecord::Base
127 def accessed_groups 127 def accessed_groups
128 @accessed_groups ||= begin 128 @accessed_groups ||= begin
129 groups = Group.where(id: self.projects.pluck(:namespace_id)).all 129 groups = Group.where(id: self.projects.pluck(:namespace_id)).all
130 - groups + self.groups 130 + groups = groups + self.groups
  131 + groups.uniq
131 end 132 end
132 end 133 end
133 end 134 end
app/roles/account.rb
@@ -80,7 +80,7 @@ module Account @@ -80,7 +80,7 @@ module Account
80 end 80 end
81 81
82 def projects_sorted_by_activity 82 def projects_sorted_by_activity
83 - projects.order("(SELECT max(events.created_at) FROM events WHERE events.project_id = projects.id) DESC") 83 + projects.sorted_by_activity
84 end 84 end
85 85
86 def namespaces 86 def namespaces