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
56deda54164d
Commit
56deda54
authored
Jan 19, 2013
by
Andrey Kumanyaev
Browse files
Added UserTeam core models (team and m-t-m relationships) and updated other models
parent
e7b304ffd6e8
Changes
14
Hide whitespace changes
Inline
Side-by-side
app/models/concerns/issuable.rb
View file @
56deda54
...
...
@@ -22,6 +22,7 @@
scope
:opened
,
where
(
closed:
false
)
scope
:closed
,
where
(
closed:
true
)
scope
:of_group
,
->
(
group
)
{
where
(
project_id:
group
.
project_ids
)
}
scope
:of_user_team
,
->
(
team
)
{
where
(
project_id:
team
.
project_ids
,
assignee_id:
team
.
member_ids
)
}
scope
:assigned
,
->
(
u
)
{
where
(
assignee_id:
u
.
id
)}
scope
:recent
,
order
(
"created_at DESC"
)
...
...
app/models/project.rb
View file @
56deda54
...
...
@@ -33,6 +33,7 @@
attr_accessor
:error_code
# Relations
belongs_to
:group
,
foreign_key:
"namespace_id"
,
conditions:
"type = 'Group'"
belongs_to
:creator
,
foreign_key:
"creator_id"
,
class_name:
"User"
belongs_to
:group
,
foreign_key:
"namespace_id"
,
conditions:
"type = 'Group'"
belongs_to
:namespace
...
...
@@ -37,7 +38,19 @@
belongs_to
:namespace
belongs_to
:creator
,
class_name:
"User"
,
foreign_key:
"creator_id"
has_one
:last_event
,
class_name:
'Event'
,
order:
'events.created_at DESC'
,
foreign_key:
'project_id'
has_one
:gitlab_ci_service
,
dependent: :destroy
has_many
:events
,
dependent: :destroy
has_many
:merge_requests
,
dependent: :destroy
has_many
:issues
,
dependent: :destroy
,
order:
"closed, created_at DESC"
has_many
:milestones
,
dependent: :destroy
has_many
:users_projects
,
dependent: :destroy
has_many
:notes
,
dependent: :destroy
has_many
:snippets
,
dependent: :destroy
has_many
:deploy_keys
,
dependent: :destroy
,
class_name:
"Key"
,
foreign_key:
"project_id"
has_many
:hooks
,
dependent: :destroy
,
class_name:
"ProjectHook"
has_many
:wikis
,
dependent: :destroy
has_many
:protected_branches
,
dependent: :destroy
has_many
:user_team_project_relationships
,
dependent: :destroy
has_many
:users
,
through: :users_projects
...
...
@@ -42,18 +55,8 @@
has_many
:users
,
through: :users_projects
has_many
:events
,
dependent: :destroy
has_many
:merge_requests
,
dependent: :destroy
has_many
:issues
,
dependent: :destroy
,
order:
"closed, created_at DESC"
has_many
:milestones
,
dependent: :destroy
has_many
:users_projects
,
dependent: :destroy
has_many
:notes
,
dependent: :destroy
has_many
:snippets
,
dependent: :destroy
has_many
:deploy_keys
,
dependent: :destroy
,
foreign_key:
"project_id"
,
class_name:
"Key"
has_many
:hooks
,
dependent: :destroy
,
class_name:
"ProjectHook"
has_many
:wikis
,
dependent: :destroy
has_many
:protected_branches
,
dependent: :destroy
has_one
:last_event
,
class_name:
'Event'
,
order:
'events.created_at DESC'
,
foreign_key:
'project_id'
has_one
:gitlab_ci_service
,
dependent: :destroy
has_many
:user_teams
,
through: :user_team_project_relationships
has_many
:user_team_user_relationships
,
through: :user_teams
has_many
:user_teams_members
,
through: :user_team_user_relationships
delegate
:name
,
to: :owner
,
allow_nil:
true
,
prefix:
true
...
...
@@ -77,6 +80,8 @@
# Scopes
scope
:without_user
,
->
(
user
)
{
where
(
"id NOT IN (:ids)"
,
ids:
user
.
authorized_projects
.
map
(
&
:id
)
)
}
scope
:not_in_group
,
->
(
group
)
{
where
(
"id NOT IN (:ids)"
,
ids:
group
.
project_ids
)
}
scope
:without_team
,
->
(
team
)
{
where
(
"id NOT IN (:ids)"
,
ids:
team
.
projects
.
map
(
&
:id
))
}
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
:personal
,
->
(
user
)
{
where
(
namespace_id:
user
.
namespace_id
)
}
...
...
app/models/user.rb
View file @
56deda54
...
...
@@ -45,6 +45,14 @@
attr_accessor
:force_random_password
# Namespace for personal projects
has_one
:namespace
,
class_name:
"Namespace"
,
foreign_key: :owner_id
,
conditions:
'type IS NULL'
,
dependent: :destroy
has_many
:groups
,
class_name:
"Group"
,
foreign_key: :owner_id
has_one
:namespace
,
dependent: :destroy
,
foreign_key: :owner_id
,
class_name:
"Namespace"
,
conditions:
'type IS NULL'
has_many
:keys
,
dependent: :destroy
has_many
:users_projects
,
dependent: :destroy
has_many
:issues
,
dependent: :destroy
,
foreign_key: :author_id
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
: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"
...
...
@@ -50,13 +58,14 @@
has_many
:keys
,
dependent: :destroy
has_many
:users_projects
,
dependent: :destroy
has_many
:issues
,
foreign_key: :author_id
,
dependent: :destroy
has_many
:notes
,
foreign_key: :author_id
,
dependent: :destroy
has_many
:merge_requests
,
foreign_key: :author_id
,
dependent: :destroy
has_many
:events
,
class_name:
"Event"
,
foreign_key: :author_id
,
dependent: :destroy
has_many
:recent_events
,
class_name:
"Event"
,
foreign_key: :author_id
,
order:
"id DESC"
has_many
:assigned_issues
,
class_name:
"Issue"
,
foreign_key: :assignee_id
,
dependent: :destroy
has_many
:assigned_merge_requests
,
class_name:
"MergeRequest"
,
foreign_key: :assignee_id
,
dependent: :destroy
has_many
:groups
,
class_name:
"Group"
,
foreign_key: :owner_id
has_many
:recent_events
,
class_name:
"Event"
,
foreign_key: :author_id
,
order:
"id DESC"
has_many
:projects
,
through: :users_projects
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
has_many
:team_projects
,
through: :user_team_project_relationships
validates
:name
,
presence:
true
validates
:bio
,
length:
{
within:
0
..
255
}
...
...
@@ -80,6 +89,8 @@
scope
:blocked
,
where
(
blocked:
true
)
scope
:active
,
where
(
blocked:
false
)
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
)
}
#
# Class methods
...
...
app/models/user_team.rb
0 → 100644
View file @
56deda54
class
UserTeam
<
ActiveRecord
::
Base
attr_accessible
:name
,
:owner_id
,
:path
belongs_to
:owner
,
class_name:
User
has_many
:user_team_project_relationships
,
dependent: :destroy
has_many
:user_team_user_relationships
,
dependent: :destroy
has_many
:projects
,
through: :user_team_project_relationships
has_many
:members
,
through: :user_team_user_relationships
,
source: :user
validates
:name
,
presence:
true
,
uniqueness:
true
validates
:owner
,
presence:
true
validates
:path
,
uniqueness:
true
,
presence:
true
,
length:
{
within:
1
..
255
},
format:
{
with:
Gitlab
::
Regex
.
path_regex
,
message:
"only letters, digits & '_' '-' '.' allowed. Letter should be first"
}
scope
:with_member
,
->
(
user
){
joins
(
:user_team_user_relationships
).
where
(
user_team_user_relationships:
{
user_id:
user
.
id
})
}
scope
:created_by
,
->
(
user
){
where
(
owner_id:
user
)
}
class
<<
self
def
search
query
where
(
"name LIKE :query OR path LIKE :query"
,
query:
"%
#{
query
}
%"
)
end
def
global_id
'GLN'
end
def
access_roles
UsersProject
.
access_roles
end
end
def
to_param
path
end
def
assign_to_projects
(
projects
,
access
)
projects
.
each
do
|
project
|
assign_to_project
(
project
,
access
)
end
end
def
assign_to_project
(
project
,
access
)
Gitlab
::
UserTeamManager
.
assign
(
self
,
project
,
access
)
end
def
resign_from_project
(
project
)
Gitlab
::
UserTeamManager
.
resign
(
self
,
project
)
end
def
add_members
(
users
,
access
,
group_admin
)
users
.
each
do
|
user
|
add_member
(
user
,
access
,
group_admin
)
end
end
def
add_member
(
user
,
access
,
group_admin
)
Gitlab
::
UserTeamManager
.
add_member_into_team
(
self
,
user
,
access
,
group_admin
)
end
def
remove_member
(
user
)
Gitlab
::
UserTeamManager
.
remove_member_from_team
(
self
,
user
)
end
def
max_project_access
(
project
)
user_team_project_relationships
.
find_by_project_id
(
project
).
greatest_access
end
def
human_max_project_access
(
project
)
self
.
class
.
access_roles
.
invert
[
max_project_access
(
project
)]
end
def
default_projects_access
(
member
)
user_team_user_relationships
.
find_by_user_id
(
member
).
permission
end
def
human_default_projects_access
(
member
)
self
.
class
.
access_roles
.
invert
[
default_projects_access
(
member
)]
end
def
admin?
(
member
)
user_team_user_relationships
.
with_user
(
member
).
first
.
group_admin?
end
end
app/models/user_team_project_relationship.rb
0 → 100644
View file @
56deda54
class
UserTeamProjectRelationship
<
ActiveRecord
::
Base
attr_accessible
:greatest_access
,
:project_id
,
:user_team_id
belongs_to
:user_team
belongs_to
:project
validates
:project
,
presence:
true
validates
:user_team
,
presence:
true
validate
:check_greatest_access
scope
:with_project
,
->
(
project
){
where
(
project_id:
project
.
id
)
}
private
def
check_greatest_access
errors
.
add
(
:base
,
:incorrect_access_code
)
unless
correct_access?
end
def
correct_access?
return
false
if
greatest_access
.
blank?
return
true
if
UsersProject
.
access_roles
.
has_value?
(
greatest_access
)
false
end
end
app/models/user_team_user_relationship.rb
0 → 100644
View file @
56deda54
class
UserTeamUserRelationship
<
ActiveRecord
::
Base
attr_accessible
:group_admin
,
:permission
,
:user_id
,
:user_team_id
belongs_to
:user_team
belongs_to
:user
validates
:user_team
,
presence:
true
validates
:user
,
presence:
true
scope
:with_user
,
->
(
user
)
{
where
(
user_id:
user
.
id
)
}
def
user_name
user
.
name
end
end
app/models/users_project.rb
View file @
56deda54
...
...
@@ -39,4 +39,5 @@
scope
:reporters
,
where
(
project_access:
REPORTER
)
scope
:developers
,
where
(
project_access:
DEVELOPER
)
scope
:masters
,
where
(
project_access:
MASTER
)
scope
:in_project
,
->
(
project
)
{
where
(
project_id:
project
.
id
)
}
...
...
@@ -42,4 +43,6 @@
scope
:in_project
,
->
(
project
)
{
where
(
project_id:
project
.
id
)
}
scope
:in_projects
,
->
(
projects
)
{
where
(
project_id:
projects
.
map
(
&
:id
))
}
scope
:with_user
,
->
(
user
)
{
where
(
user_id:
user
.
id
)
}
class
<<
self
...
...
db/migrate/20121219183753_create_user_teams.rb
0 → 100644
View file @
56deda54
class
CreateUserTeams
<
ActiveRecord
::
Migration
def
change
create_table
:user_teams
do
|
t
|
t
.
string
:name
t
.
string
:path
t
.
integer
:owner_id
t
.
timestamps
end
end
end
db/migrate/20121220064104_create_user_team_project_relationships.rb
0 → 100644
View file @
56deda54
class
CreateUserTeamProjectRelationships
<
ActiveRecord
::
Migration
def
change
create_table
:user_team_project_relationships
do
|
t
|
t
.
integer
:project_id
t
.
integer
:user_team_id
t
.
integer
:greatest_access
t
.
timestamps
end
end
end
db/migrate/20121220064453_create_user_team_user_relationships.rb
0 → 100644
View file @
56deda54
class
CreateUserTeamUserRelationships
<
ActiveRecord
::
Migration
def
change
create_table
:user_team_user_relationships
do
|
t
|
t
.
integer
:user_id
t
.
integer
:user_team_id
t
.
boolean
:group_admin
t
.
integer
:permission
t
.
timestamps
end
end
end
db/schema.rb
View file @
56deda54
...
...
@@ -213,6 +213,31 @@
t
.
string
"name"
end
create_table
"user_team_project_relationships"
,
:force
=>
true
do
|
t
|
t
.
integer
"project_id"
t
.
integer
"user_team_id"
t
.
integer
"greatest_access"
t
.
datetime
"created_at"
,
:null
=>
false
t
.
datetime
"updated_at"
,
:null
=>
false
end
create_table
"user_team_user_relationships"
,
:force
=>
true
do
|
t
|
t
.
integer
"user_id"
t
.
integer
"user_team_id"
t
.
boolean
"group_admin"
t
.
integer
"permission"
t
.
datetime
"created_at"
,
:null
=>
false
t
.
datetime
"updated_at"
,
:null
=>
false
end
create_table
"user_teams"
,
:force
=>
true
do
|
t
|
t
.
string
"name"
t
.
string
"path"
t
.
integer
"owner_id"
t
.
datetime
"created_at"
,
:null
=>
false
t
.
datetime
"updated_at"
,
:null
=>
false
end
create_table
"users"
,
:force
=>
true
do
|
t
|
t
.
string
"email"
,
:default
=>
""
,
:null
=>
false
t
.
string
"encrypted_password"
,
:default
=>
""
,
:null
=>
false
...
...
spec/models/user_team_project_relationship_spec.rb
0 → 100644
View file @
56deda54
require
'spec_helper'
describe
UserTeamProjectRelationship
do
pending
"add some examples to (or delete)
#{
__FILE__
}
"
end
spec/models/user_team_spec.rb
0 → 100644
View file @
56deda54
require
'spec_helper'
describe
UserTeam
do
pending
"add some examples to (or delete)
#{
__FILE__
}
"
end
spec/models/user_team_user_relationship_spec.rb
0 → 100644
View file @
56deda54
require
'spec_helper'
describe
UserTeamUserRelationship
do
pending
"add some examples to (or delete)
#{
__FILE__
}
"
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