Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
heptapod
heptapod
Commits
4b85b3876733
Commit
4b85b387
authored
Apr 03, 2013
by
Andrey Kumanyaev
Browse files
Update model methods
parent
b46fd2f06ecf
Changes
7
Hide whitespace changes
Inline
Side-by-side
app/controllers/admin/users_controller.rb
View file @
4b85b387
...
...
@@ -14,7 +14,7 @@
@not_in_projects
=
@not_in_projects
.
without_user
(
admin_user
)
if
admin_user
.
authorized_projects
.
present?
# Projects he already own or joined
@projects
=
admin_user
.
authorized_projects
.
where
(
'projects.id in (?)'
,
admin_user
.
authorized_projects
.
map
(
&
:id
))
@projects
=
admin_user
.
authorized_projects
end
def
team_update
...
...
app/models/group.rb
View file @
4b85b387
...
...
@@ -13,6 +13,7 @@
#
class
Group
<
Namespace
def
add_users_to_project_teams
(
user_ids
,
project_access
)
UsersProject
.
add_users_into_projects
(
projects
.
map
(
&
:id
),
...
...
app/models/issue.rb
View file @
4b85b387
...
...
@@ -25,19 +25,9 @@
acts_as_taggable_on
:labels
class
<<
self
def
cared
(
user
)
where
(
'assignee_id = :user'
,
user:
user
.
id
)
end
def
authored
(
user
)
where
(
'author_id = :user'
,
user:
user
.
id
)
end
def
open_for
(
user
)
opened
.
assigned
(
user
)
end
end
scope
:cared
,
->
(
user
)
{
where
(
assignee_id:
user
)
}
scope
:authored
,
->
(
user
)
{
where
(
author_id:
user
)
}
scope
:open_for
,
->
(
user
)
{
opened
.
assigned
(
user
)
}
state_machine
:state
,
initial: :opened
do
event
:close
do
...
...
app/models/key.rb
View file @
4b85b387
...
...
@@ -23,7 +23,7 @@
before_validation
:strip_white_space
validates
:title
,
presence:
true
,
length:
{
within:
0
..
255
}
validates
:key
,
presence:
true
,
length:
{
within:
0
..
5000
},
format:
{
:
with
=>
/ssh-.{3} /
},
uniqueness:
true
validates
:key
,
presence:
true
,
length:
{
within:
0
..
5000
},
format:
{
with
:
/ssh-.{3} /
},
uniqueness:
true
validate
:fingerprintable_key
delegate
:name
,
:email
,
to: :user
,
prefix:
true
...
...
@@ -48,7 +48,7 @@
end
def
is_deploy_key
!!
project
_id
project
.
present?
end
# projects that has this key
...
...
app/models/milestone.rb
View file @
4b85b387
...
...
@@ -19,6 +19,7 @@
belongs_to
:project
has_many
:issues
has_many
:merge_requests
has_many
:participants
,
through: :issues
,
source: :assignee
scope
:active
,
->
{
with_state
(
:active
)
}
scope
:closed
,
->
{
with_state
(
:closed
)
}
...
...
@@ -48,10 +49,6 @@
end
end
def
participants
User
.
where
(
id:
issues
.
pluck
(
:assignee_id
))
end
def
open_items_count
self
.
issues
.
opened
.
count
+
self
.
merge_requests
.
opened
.
count
end
...
...
app/models/project.rb
View file @
4b85b387
...
...
@@ -87,4 +87,5 @@
# Scopes
scope
:without_user
,
->
(
user
)
{
where
(
"id NOT IN (:ids)"
,
ids:
user
.
authorized_projects
.
map
(
&
:id
)
)
}
scope
:without_team
,
->
(
team
)
{
team
.
projects
.
present?
?
where
(
"id NOT IN (:ids)"
,
ids:
team
.
projects
.
map
(
&
:id
))
:
scoped
}
scope
:not_in_group
,
->
(
group
)
{
where
(
"id NOT IN (:ids)"
,
ids:
group
.
project_ids
)
}
...
...
@@ -90,4 +91,3 @@
scope
:not_in_group
,
->
(
group
)
{
where
(
"id NOT IN (:ids)"
,
ids:
group
.
project_ids
)
}
scope
:without_team
,
->
(
team
)
{
team
.
projects
.
present?
?
where
(
"id NOT IN (:ids)"
,
ids:
team
.
projects
.
map
(
&
:id
))
:
scoped
}
scope
:in_team
,
->
(
team
)
{
where
(
"id IN (:ids)"
,
ids:
team
.
projects
.
map
(
&
:id
))
}
scope
:in_namespace
,
->
(
namespace
)
{
where
(
namespace_id:
namespace
.
id
)
}
...
...
@@ -92,6 +92,7 @@
scope
:in_team
,
->
(
team
)
{
where
(
"id IN (:ids)"
,
ids:
team
.
projects
.
map
(
&
:id
))
}
scope
:in_namespace
,
->
(
namespace
)
{
where
(
namespace_id:
namespace
.
id
)
}
scope
:sorted_by_activity
,
->
()
{
order
(
"(SELECT max(events.created_at) FROM events WHERE events.project_id = projects.id) DESC"
)
}
scope
:in_group_namespace
,
->
{
joins
(
:group
)
}
scope
:sorted_by_activity
,
->
{
order
(
"(SELECT max(events.created_at) FROM events WHERE events.project_id = projects.id) DESC"
)
}
scope
:personal
,
->
(
user
)
{
where
(
namespace_id:
user
.
namespace_id
)
}
scope
:joined
,
->
(
user
)
{
where
(
"namespace_id != ?"
,
user
.
namespace_id
)
}
scope
:public_only
,
->
{
where
(
public:
true
)
}
...
...
@@ -156,7 +157,8 @@
unless
creator
.
can_create_project?
errors
[
:limit_reached
]
<<
(
"Your own projects limit is
#{
creator
.
projects_limit
}
! Please contact administrator to increase it"
)
end
rescue
rescue
=>
ex
errors
[
:base
]
<<
ex
.
message
errors
[
:base
]
<<
(
"Can't check your ability to create project"
)
end
...
...
app/models/user.rb
View file @
4b85b387
...
...
@@ -59,11 +59,10 @@
#
# Namespace for personal projects
has_one
:namespace
,
dependent: :destroy
,
foreign_key: :owner_id
,
class_name:
"Namespace"
,
conditions:
'type IS NULL'
has_one
:namespace
,
dependent: :destroy
,
foreign_key: :owner_id
,
class_name:
"Namespace"
,
conditions:
'type IS NULL'
# Namespaces (owned groups and own namespace)
has_many
:namespaces
,
foreign_key: :owner_id
# Profile
has_many
:keys
,
dependent: :destroy
...
...
@@ -72,11 +71,7 @@
has_many
:groups
,
class_name:
"Group"
,
foreign_key: :owner_id
# Teams
has_many
:own_teams
,
class_name:
"UserTeam"
,
foreign_key: :owner_id
,
dependent: :destroy
has_many
:user_team_user_relationships
,
dependent: :destroy
has_many
:user_teams
,
through: :user_team_user_relationships
has_many
:own_teams
,
dependent: :destroy
,
class_name:
"UserTeam"
,
foreign_key: :owner_id
has_many
:user_team_user_relationships
,
dependent: :destroy
has_many
:user_teams
,
through: :user_team_user_relationships
has_many
:user_team_project_relationships
,
through: :user_teams
...
...
@@ -82,5 +77,5 @@
has_many
:user_team_project_relationships
,
through: :user_teams
has_many
:team_projects
,
through: :user_team_project_relationships
has_many
:team_projects
,
through: :user_team_project_relationships
# Projects
has_many
:users_projects
,
dependent: :destroy
...
...
@@ -88,5 +83,6 @@
has_many
:notes
,
dependent: :destroy
,
foreign_key: :author_id
has_many
:merge_requests
,
dependent: :destroy
,
foreign_key: :author_id
has_many
:events
,
dependent: :destroy
,
foreign_key: :author_id
,
class_name:
"Event"
has_many
:recent_events
,
foreign_key: :author_id
,
class_name:
"Event"
,
order:
"id DESC"
has_many
:assigned_issues
,
dependent: :destroy
,
foreign_key: :assignee_id
,
class_name:
"Issue"
has_many
:assigned_merge_requests
,
dependent: :destroy
,
foreign_key: :assignee_id
,
class_name:
"MergeRequest"
...
...
@@ -91,4 +87,3 @@
has_many
:assigned_issues
,
dependent: :destroy
,
foreign_key: :assignee_id
,
class_name:
"Issue"
has_many
:assigned_merge_requests
,
dependent: :destroy
,
foreign_key: :assignee_id
,
class_name:
"MergeRequest"
has_many
:projects
,
through: :users_projects
...
...
@@ -94,8 +89,8 @@
has_many
:
recent_even
ts
,
class_name:
"Event"
,
foreign_key: :
auth
or_id
,
order:
"id DESC"
has_many
:
personal_projects
,
through: :namespace
,
source: :projec
ts
has_many
:projects
,
through: :users_projects
has_many
:own_projects
,
foreign_key: :
creat
or_id
has_many
:owned_projects
,
through: :namespaces
,
source: :projects
#
# Validations
...
...
@@ -109,9 +104,7 @@
format:
{
with:
Gitlab
::
Regex
.
username_regex
,
message:
"only letters, digits & '_' '-' '.' allowed. Letter should be first"
}
validates
:notification_level
,
inclusion:
{
in:
Notification
.
notification_levels
},
presence:
true
validates
:notification_level
,
inclusion:
{
in:
Notification
.
notification_levels
},
presence:
true
validate
:namespace_uniq
,
if:
->
(
user
)
{
user
.
username_changed?
}
...
...
@@ -145,6 +138,9 @@
scope
:alphabetically
,
->
{
order
(
'name ASC'
)
}
scope
:in_team
,
->
(
team
){
where
(
id:
team
.
member_ids
)
}
scope
:not_in_team
,
->
(
team
){
where
(
'users.id NOT IN (:ids)'
,
ids:
team
.
member_ids
)
}
scope
:not_in_project
,
->
(
project
)
{
project
.
users
.
present?
?
where
(
"id not in (:ids)"
,
ids:
project
.
users
.
map
(
&
:id
)
)
:
scoped
}
scope
:without_projects
,
->
{
where
(
'id NOT IN (SELECT DISTINCT(user_id) FROM users_projects)'
)
}
scope
:potential_team_members
,
->
(
team
)
{
team
.
members
.
any?
?
active
.
not_in_team
(
team
)
:
active
}
#
...
...
@@ -171,18 +167,6 @@
end
end
def
not_in_project
(
project
)
if
project
.
users
.
present?
where
(
"id not in (:ids)"
,
ids:
project
.
users
.
map
(
&
:id
)
)
else
scoped
end
end
def
without_projects
where
(
'id NOT IN (SELECT DISTINCT(user_id) FROM users_projects)'
)
end
def
create_from_omniauth
(
auth
,
ldap
=
false
)
gitlab_auth
.
create_from_omniauth
(
auth
,
ldap
)
end
...
...
@@ -229,21 +213,8 @@
end
end
# Namespaces user has access to
def
namespaces
namespaces
=
[]
# Add user account namespace
namespaces
<<
self
.
namespace
if
self
.
namespace
# Add groups you can manage
namespaces
+=
groups
.
all
namespaces
end
# Groups where user is an owner
def
owned_groups
groups
end
...
...
@@ -245,7 +216,11 @@
# Groups where user is an owner
def
owned_groups
groups
end
def
owned_teams
own_teams
end
# Groups user has access to
def
authorized_groups
...
...
@@ -250,12 +225,9 @@
# Groups user has access to
def
authorized_groups
@authorized_groups
||=
begin
groups
=
Group
.
where
(
id:
self
.
authorized_projects
.
pluck
(
:namespace_id
)).
all
groups
=
groups
+
self
.
groups
groups
.
uniq
end
@group_ids
||=
(
groups
.
pluck
(
:id
)
+
authorized_projects
.
pluck
(
:namespace_id
))
Group
.
where
(
id:
@group_ids
)
end
# Projects user has access to
def
authorized_projects
...
...
@@ -257,10 +229,9 @@
end
# Projects user has access to
def
authorized_projects
project_ids
=
users_projects
.
pluck
(
:project_id
)
project_ids
=
project_ids
|
owned_projects
.
pluck
(
:id
)
Project
.
where
(
id:
project_ids
)
@project_ids
||=
(
owned_projects
.
pluck
(
:id
)
+
projects
.
pluck
(
:id
)).
uniq
Project
.
where
(
id:
@project_ids
)
end
...
...
@@ -265,16 +236,9 @@
end
# Projects in user namespace
def
personal_projects
Project
.
personal
(
self
)
end
# Projects where user is an owner
def
owned_projects
Project
.
where
(
"(projects.namespace_id IN (:namespaces)) OR
(projects.namespace_id IS NULL AND projects.creator_id = :user_id)"
,
namespaces:
namespaces
.
map
(
&
:id
),
user_id:
self
.
id
)
def
authorized_teams
@team_ids
||=
(
user_teams
.
pluck
(
:id
)
+
own_teams
.
pluck
(
:id
)).
uniq
UserTeam
.
where
(
id:
@team_ids
)
end
# Team membership in authorized projects
def
tm_in_authorized_projects
...
...
@@ -277,8 +241,8 @@
end
# Team membership in authorized projects
def
tm_in_authorized_projects
UsersProject
.
where
(
project_id:
authorized_projects
.
map
(
&
:id
),
user_id:
self
.
id
)
UsersProject
.
where
(
project_id:
authorized_projects
.
map
(
&
:id
),
user_id:
self
.
id
)
end
def
is_admin?
...
...
@@ -344,10 +308,10 @@
end
def
several_namespaces?
namespaces
.
size
>
1
namespaces
.
many?
end
def
namespace_id
namespace
.
try
:id
end
...
...
@@ -348,24 +312,9 @@
end
def
namespace_id
namespace
.
try
:id
end
def
authorized_teams
@authorized_teams
||=
begin
ids
=
[]
ids
<<
UserTeam
.
with_member
(
self
).
pluck
(
'user_teams.id'
)
ids
<<
UserTeam
.
created_by
(
self
).
pluck
(
'user_teams.id'
)
ids
.
flatten
UserTeam
.
where
(
id:
ids
)
end
end
def
owned_teams
UserTeam
.
where
(
owner_id:
self
.
id
)
end
def
name_with_username
"
#{
name
}
(
#{
username
}
)"
end
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment