Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tatuya Kamada
gitlab-ce
Commits
29789201
Commit
29789201
authored
Oct 14, 2016
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add LabelPriority model
parent
de9d3915
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
106 additions
and
8 deletions
+106
-8
app/models/label.rb
app/models/label.rb
+1
-6
app/models/label_priority.rb
app/models/label_priority.rb
+8
-0
db/migrate/20161014173530_create_label_priorities.rb
db/migrate/20161014173530_create_label_priorities.rb
+55
-0
db/schema.rb
db/schema.rb
+13
-2
spec/factories/label_priorities.rb
spec/factories/label_priorities.rb
+7
-0
spec/models/label_priority_spec.rb
spec/models/label_priority_spec.rb
+21
-0
spec/models/label_spec.rb
spec/models/label_spec.rb
+1
-0
No files found.
app/models/label.rb
View file @
29789201
...
@@ -16,6 +16,7 @@ class Label < ActiveRecord::Base
...
@@ -16,6 +16,7 @@ class Label < ActiveRecord::Base
default_value_for
:color
,
DEFAULT_COLOR
default_value_for
:color
,
DEFAULT_COLOR
has_many
:lists
,
dependent: :destroy
has_many
:lists
,
dependent: :destroy
has_many
:priorities
,
class_name:
'LabelPriority'
has_many
:label_links
,
dependent: :destroy
has_many
:label_links
,
dependent: :destroy
has_many
:issues
,
through: :label_links
,
source: :target
,
source_type:
'Issue'
has_many
:issues
,
through: :label_links
,
source: :target
,
source_type:
'Issue'
has_many
:merge_requests
,
through: :label_links
,
source: :target
,
source_type:
'MergeRequest'
has_many
:merge_requests
,
through: :label_links
,
source: :target
,
source_type:
'MergeRequest'
...
@@ -26,8 +27,6 @@ class Label < ActiveRecord::Base
...
@@ -26,8 +27,6 @@ class Label < ActiveRecord::Base
validates
:title
,
presence:
true
,
format:
{
with:
/\A[^,]+\z/
}
validates
:title
,
presence:
true
,
format:
{
with:
/\A[^,]+\z/
}
validates
:title
,
uniqueness:
{
scope:
[
:group_id
,
:project_id
]
}
validates
:title
,
uniqueness:
{
scope:
[
:group_id
,
:project_id
]
}
before_save
:nullify_priority
default_scope
{
order
(
title: :asc
)
}
default_scope
{
order
(
title: :asc
)
}
scope
:templates
,
->
{
where
(
template:
true
)
}
scope
:templates
,
->
{
where
(
template:
true
)
}
...
@@ -149,10 +148,6 @@ class Label < ActiveRecord::Base
...
@@ -149,10 +148,6 @@ class Label < ActiveRecord::Base
end
end
end
end
def
nullify_priority
self
.
priority
=
nil
if
priority
.
blank?
end
def
sanitize_title
(
value
)
def
sanitize_title
(
value
)
CGI
.
unescapeHTML
(
Sanitize
.
clean
(
value
.
to_s
))
CGI
.
unescapeHTML
(
Sanitize
.
clean
(
value
.
to_s
))
end
end
...
...
app/models/label_priority.rb
0 → 100644
View file @
29789201
class
LabelPriority
<
ActiveRecord
::
Base
belongs_to
:project
belongs_to
:label
validates
:project
,
:label
,
:priority
,
presence:
true
validates
:label_id
,
uniqueness:
{
scope: :project_id
}
validates
:priority
,
numericality:
{
only_integer:
true
,
greater_than_or_equal_to:
0
}
end
db/migrate/20161014173530_create_label_priorities.rb
0 → 100644
View file @
29789201
class
CreateLabelPriorities
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
true
DOWNTIME_REASON
=
'Prioritezed labels will not work as expected until this migration is complete.'
disable_ddl_transaction!
def
up
create_table
:label_priorities
do
|
t
|
t
.
references
:project
,
foreign_key:
{
on_delete: :cascade
},
null:
false
t
.
references
:label
,
foreign_key:
{
on_delete: :cascade
},
null:
false
t
.
integer
:priority
,
null:
false
t
.
timestamps
null:
false
end
execute
<<-
EOF
.
strip_heredoc
INSERT INTO label_priorities (project_id, label_id, priority, created_at, updated_at)
SELECT labels.project_id, labels.id, labels.priority, NOW(), NOW()
FROM labels
WHERE labels.project_id IS NOT NULL
AND labels.priority IS NOT NULL;
EOF
add_concurrent_index
:label_priorities
,
[
:project_id
,
:label_id
],
unique:
true
add_concurrent_index
:label_priorities
,
:priority
remove_column
:labels
,
:priority
end
def
down
add_column
:labels
,
:priority
,
:integer
if
Gitlab
::
Database
.
mysql?
execute
<<-
EOF
.
strip_heredoc
UPDATE labels
INNER JOIN label_priorities ON labels.id = label_priorities.label_id AND labels.project_id = label_priorities.project_id
SET labels.priority = label_priorities.priority;
EOF
else
execute
<<-
EOF
.
strip_heredoc
UPDATE labels
SET priority = label_priorities.priority
FROM label_priorities
WHERE labels.id = label_priorities.label_id
AND labels.project_id = label_priorities.project_id;
EOF
end
add_concurrent_index
:labels
,
:priority
drop_table
:label_priorities
end
end
db/schema.rb
View file @
29789201
...
@@ -519,6 +519,17 @@ ActiveRecord::Schema.define(version: 20161017095000) do
...
@@ -519,6 +519,17 @@ ActiveRecord::Schema.define(version: 20161017095000) do
add_index
"label_links"
,
[
"label_id"
],
name:
"index_label_links_on_label_id"
,
using: :btree
add_index
"label_links"
,
[
"label_id"
],
name:
"index_label_links_on_label_id"
,
using: :btree
add_index
"label_links"
,
[
"target_id"
,
"target_type"
],
name:
"index_label_links_on_target_id_and_target_type"
,
using: :btree
add_index
"label_links"
,
[
"target_id"
,
"target_type"
],
name:
"index_label_links_on_target_id_and_target_type"
,
using: :btree
create_table
"label_priorities"
,
force: :cascade
do
|
t
|
t
.
integer
"project_id"
,
null:
false
t
.
integer
"label_id"
,
null:
false
t
.
integer
"priority"
,
null:
false
t
.
datetime
"created_at"
,
null:
false
t
.
datetime
"updated_at"
,
null:
false
end
add_index
"label_priorities"
,
[
"priority"
],
name:
"index_label_priorities_on_priority"
,
using: :btree
add_index
"label_priorities"
,
[
"project_id"
,
"label_id"
],
name:
"index_label_priorities_on_project_id_and_label_id"
,
unique:
true
,
using: :btree
create_table
"labels"
,
force: :cascade
do
|
t
|
create_table
"labels"
,
force: :cascade
do
|
t
|
t
.
string
"title"
t
.
string
"title"
t
.
string
"color"
t
.
string
"color"
...
@@ -527,14 +538,12 @@ ActiveRecord::Schema.define(version: 20161017095000) do
...
@@ -527,14 +538,12 @@ ActiveRecord::Schema.define(version: 20161017095000) do
t
.
datetime
"updated_at"
t
.
datetime
"updated_at"
t
.
boolean
"template"
,
default:
false
t
.
boolean
"template"
,
default:
false
t
.
string
"description"
t
.
string
"description"
t
.
integer
"priority"
t
.
text
"description_html"
t
.
text
"description_html"
t
.
string
"type"
t
.
string
"type"
t
.
integer
"group_id"
t
.
integer
"group_id"
end
end
add_index
"labels"
,
[
"group_id"
],
name:
"index_labels_on_group_id"
,
using: :btree
add_index
"labels"
,
[
"group_id"
],
name:
"index_labels_on_group_id"
,
using: :btree
add_index
"labels"
,
[
"priority"
],
name:
"index_labels_on_priority"
,
using: :btree
add_index
"labels"
,
[
"project_id"
],
name:
"index_labels_on_project_id"
,
using: :btree
add_index
"labels"
,
[
"project_id"
],
name:
"index_labels_on_project_id"
,
using: :btree
add_index
"labels"
,
[
"title"
],
name:
"index_labels_on_title"
,
using: :btree
add_index
"labels"
,
[
"title"
],
name:
"index_labels_on_title"
,
using: :btree
...
@@ -1216,6 +1225,8 @@ ActiveRecord::Schema.define(version: 20161017095000) do
...
@@ -1216,6 +1225,8 @@ ActiveRecord::Schema.define(version: 20161017095000) do
add_foreign_key
"boards"
,
"projects"
add_foreign_key
"boards"
,
"projects"
add_foreign_key
"issue_metrics"
,
"issues"
,
on_delete: :cascade
add_foreign_key
"issue_metrics"
,
"issues"
,
on_delete: :cascade
add_foreign_key
"label_priorities"
,
"labels"
,
on_delete: :cascade
add_foreign_key
"label_priorities"
,
"projects"
,
on_delete: :cascade
add_foreign_key
"labels"
,
"namespaces"
,
column:
"group_id"
,
on_delete: :cascade
add_foreign_key
"labels"
,
"namespaces"
,
column:
"group_id"
,
on_delete: :cascade
add_foreign_key
"lists"
,
"boards"
add_foreign_key
"lists"
,
"boards"
add_foreign_key
"lists"
,
"labels"
add_foreign_key
"lists"
,
"labels"
...
...
spec/factories/label_priorities.rb
0 → 100644
View file @
29789201
FactoryGirl
.
define
do
factory
:label_priority
do
project
factory: :empty_project
label
sequence
(
:priority
)
end
end
spec/models/label_priority_spec.rb
0 → 100644
View file @
29789201
require
'spec_helper'
describe
LabelPriority
,
models:
true
do
describe
'relationships'
do
it
{
is_expected
.
to
belong_to
(
:project
)
}
it
{
is_expected
.
to
belong_to
(
:label
)
}
end
describe
'validations'
do
it
{
is_expected
.
to
validate_presence_of
(
:project
)
}
it
{
is_expected
.
to
validate_presence_of
(
:label
)
}
it
{
is_expected
.
to
validate_presence_of
(
:priority
)
}
it
{
is_expected
.
to
validate_numericality_of
(
:priority
).
only_integer
.
is_greater_than_or_equal_to
(
0
)
}
it
'validates uniqueness of label_id scoped to project_id'
do
create
(
:label_priority
)
expect
(
subject
).
to
validate_uniqueness_of
(
:label_id
).
scoped_to
(
:project_id
)
end
end
end
spec/models/label_spec.rb
View file @
29789201
...
@@ -10,6 +10,7 @@ describe Label, models: true do
...
@@ -10,6 +10,7 @@ describe Label, models: true do
it
{
is_expected
.
to
have_many
(
:issues
).
through
(
:label_links
).
source
(
:target
)
}
it
{
is_expected
.
to
have_many
(
:issues
).
through
(
:label_links
).
source
(
:target
)
}
it
{
is_expected
.
to
have_many
(
:label_links
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_many
(
:label_links
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_many
(
:lists
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_many
(
:lists
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_many
(
:priorities
).
class_name
(
'LabelPriority'
)
}
end
end
describe
'validation'
do
describe
'validation'
do
...
...
Write
Preview
Markdown
is supported
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