Commit b813a4888a8755a0944fbe96c6fd35485137bf3c
1 parent
6424ec93
Exists in
master
and in
4 other branches
Remove User#namespaces method from code
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
3 changed files
with
9 additions
and
7 deletions
Show diff stats
app/helpers/namespaces_helper.rb
| 1 | module NamespacesHelper | 1 | module NamespacesHelper |
| 2 | def namespaces_options(selected = :current_user, scope = :default) | 2 | def namespaces_options(selected = :current_user, scope = :default) |
| 3 | - groups = current_user.owned_groups.select {|n| n.type == 'Group'} | ||
| 4 | - users = current_user.namespaces.reject {|n| n.type == 'Group'} | 3 | + groups = current_user.owned_groups |
| 4 | + users = [current_user.namespace] | ||
| 5 | 5 | ||
| 6 | group_opts = ["Groups", groups.sort_by(&:human_name).map {|g| [g.human_name, g.id]} ] | 6 | group_opts = ["Groups", groups.sort_by(&:human_name).map {|g| [g.human_name, g.id]} ] |
| 7 | users_opts = [ "Users", users.sort_by(&:human_name).map {|u| [u.human_name, u.id]} ] | 7 | users_opts = [ "Users", users.sort_by(&:human_name).map {|u| [u.human_name, u.id]} ] |
app/models/user.rb
| @@ -82,8 +82,6 @@ class User < ActiveRecord::Base | @@ -82,8 +82,6 @@ class User < ActiveRecord::Base | ||
| 82 | has_many :personal_projects, through: :namespace, source: :projects | 82 | has_many :personal_projects, through: :namespace, source: :projects |
| 83 | has_many :projects, through: :users_projects | 83 | has_many :projects, through: :users_projects |
| 84 | has_many :created_projects, foreign_key: :creator_id, class_name: 'Project' | 84 | has_many :created_projects, foreign_key: :creator_id, class_name: 'Project' |
| 85 | - has_many :owned_projects, through: :owned_groups, source: :projects | ||
| 86 | - | ||
| 87 | 85 | ||
| 88 | has_many :snippets, dependent: :destroy, foreign_key: :author_id, class_name: "Snippet" | 86 | has_many :snippets, dependent: :destroy, foreign_key: :author_id, class_name: "Snippet" |
| 89 | has_many :users_projects, dependent: :destroy | 87 | has_many :users_projects, dependent: :destroy |
| @@ -258,6 +256,12 @@ class User < ActiveRecord::Base | @@ -258,6 +256,12 @@ class User < ActiveRecord::Base | ||
| 258 | end | 256 | end |
| 259 | end | 257 | end |
| 260 | 258 | ||
| 259 | + def owned_projects | ||
| 260 | + @owned_projects ||= begin | ||
| 261 | + Project.where(namespace_id: owned_groups.pluck(:id).push(namespace.id)).joins(:namespace) | ||
| 262 | + end | ||
| 263 | + end | ||
| 264 | + | ||
| 261 | # Team membership in authorized projects | 265 | # Team membership in authorized projects |
| 262 | def tm_in_authorized_projects | 266 | def tm_in_authorized_projects |
| 263 | UsersProject.where(project_id: authorized_projects.map(&:id), user_id: self.id) | 267 | UsersProject.where(project_id: authorized_projects.map(&:id), user_id: self.id) |
| @@ -330,7 +334,7 @@ class User < ActiveRecord::Base | @@ -330,7 +334,7 @@ class User < ActiveRecord::Base | ||
| 330 | end | 334 | end |
| 331 | 335 | ||
| 332 | def several_namespaces? | 336 | def several_namespaces? |
| 333 | - namespaces.many? || owned_groups.any? | 337 | + owned_groups.any? |
| 334 | end | 338 | end |
| 335 | 339 | ||
| 336 | def namespace_id | 340 | def namespace_id |
spec/models/user_spec.rb
| @@ -135,7 +135,6 @@ describe User do | @@ -135,7 +135,6 @@ describe User do | ||
| 135 | end | 135 | end |
| 136 | 136 | ||
| 137 | it { @user.several_namespaces?.should be_true } | 137 | it { @user.several_namespaces?.should be_true } |
| 138 | - it { @user.namespaces.should include(@user.namespace) } | ||
| 139 | it { @user.authorized_groups.should == [@group] } | 138 | it { @user.authorized_groups.should == [@group] } |
| 140 | it { @user.owned_groups.should == [@group] } | 139 | it { @user.owned_groups.should == [@group] } |
| 141 | end | 140 | end |
| @@ -162,7 +161,6 @@ describe User do | @@ -162,7 +161,6 @@ describe User do | ||
| 162 | end | 161 | end |
| 163 | 162 | ||
| 164 | it { @user.several_namespaces?.should be_false } | 163 | it { @user.several_namespaces?.should be_false } |
| 165 | - it { @user.namespaces.should == [@user.namespace] } | ||
| 166 | end | 164 | end |
| 167 | 165 | ||
| 168 | describe 'blocking user' do | 166 | describe 'blocking user' do |