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
e28058c4
Commit
e28058c4
authored
Sep 21, 2016
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validate if project label title does not exist at group level
parent
cfedd42b
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
59 additions
and
3 deletions
+59
-3
app/models/label.rb
app/models/label.rb
+3
-2
app/models/project_label.rb
app/models/project_label.rb
+14
-0
config/locales/en.yml
config/locales/en.yml
+1
-0
spec/factories/labels.rb
spec/factories/labels.rb
+6
-0
spec/models/label_spec.rb
spec/models/label_spec.rb
+1
-1
spec/models/project_label_spec.rb
spec/models/project_label_spec.rb
+34
-0
No files found.
app/models/label.rb
View file @
e28058c4
...
@@ -24,13 +24,14 @@ class Label < ActiveRecord::Base
...
@@ -24,13 +24,14 @@ class Label < ActiveRecord::Base
# Don't allow ',' for label titles
# Don't allow ',' for label titles
validates
:title
,
presence:
true
,
format:
{
with:
/\A[^,]+\z/
}
validates
:title
,
presence:
true
,
format:
{
with:
/\A[^,]+\z/
}
validates
:title
,
uniqueness:
true
,
unless: :template?
validates
:title
,
uniqueness:
{
scope:
[
:group_id
,
:project_id
]
}
before_save
:nullify_priority
before_save
:nullify_priority
default_scope
{
order
(
title: :asc
)
}
default_scope
{
order
(
title: :asc
)
}
scope
:templates
,
->
{
where
(
template:
true
)
}
scope
:templates
,
->
{
where
(
template:
true
)
}
scope
:with_title
,
->
(
title
)
{
where
(
title:
title
)
}
def
self
.
prioritized
def
self
.
prioritized
where
.
not
(
priority:
nil
).
reorder
(
:priority
,
:title
)
where
.
not
(
priority:
nil
).
reorder
(
:priority
,
:title
)
...
...
app/models/project_label.rb
View file @
e28058c4
...
@@ -2,4 +2,18 @@ class ProjectLabel < Label
...
@@ -2,4 +2,18 @@ class ProjectLabel < Label
belongs_to
:project
belongs_to
:project
validates
:project
,
presence:
true
validates
:project
,
presence:
true
validate
:title_must_not_exist_at_group_level
delegate
:group
,
to: :project
,
allow_nil:
true
private
def
title_must_not_exist_at_group_level
return
unless
group
.
present?
if
group
.
labels
.
with_title
(
self
.
title
).
exists?
errors
.
add
(
:title
,
:label_already_exists_at_group_level
,
group:
group
.
name
)
end
end
end
end
config/locales/en.yml
View file @
e28058c4
...
@@ -5,6 +5,7 @@ en:
...
@@ -5,6 +5,7 @@ en:
hello
:
"
Hello
world"
hello
:
"
Hello
world"
errors
:
errors
:
messages
:
messages
:
label_already_exists_at_group_level
:
"
already
exists
at
group
level
for
%{group}.
Please
choose
another
one."
wrong_size
:
"
is
the
wrong
size
(should
be
%{file_size})"
wrong_size
:
"
is
the
wrong
size
(should
be
%{file_size})"
size_too_small
:
"
is
too
small
(should
be
at
least
%{file_size})"
size_too_small
:
"
is
too
small
(should
be
at
least
%{file_size})"
size_too_big
:
"
is
too
big
(should
be
at
most
%{file_size})"
size_too_big
:
"
is
too
big
(should
be
at
most
%{file_size})"
...
...
spec/factories/labels.rb
View file @
e28058c4
...
@@ -4,4 +4,10 @@ FactoryGirl.define do
...
@@ -4,4 +4,10 @@ FactoryGirl.define do
color
"#990000"
color
"#990000"
project
project
end
end
factory
:group_label
,
class:
GroupLabel
do
sequence
(
:title
)
{
|
n
|
"label
#{
n
}
"
}
color
"#990000"
group
end
end
end
spec/models/label_spec.rb
View file @
e28058c4
...
@@ -13,7 +13,7 @@ describe Label, models: true do
...
@@ -13,7 +13,7 @@ describe Label, models: true do
end
end
describe
'validation'
do
describe
'validation'
do
it
{
is_expected
.
to
validate_uniqueness_of
(
:title
)
}
it
{
is_expected
.
to
validate_uniqueness_of
(
:title
)
.
scoped_to
([
:group_id
,
:project_id
])
}
it
'validates color code'
do
it
'validates color code'
do
is_expected
.
not_to
allow_value
(
'G-ITLAB'
).
for
(
:color
)
is_expected
.
not_to
allow_value
(
'G-ITLAB'
).
for
(
:color
)
...
...
spec/models/project_label_spec.rb
View file @
e28058c4
...
@@ -7,5 +7,39 @@ describe ProjectLabel, models: true do
...
@@ -7,5 +7,39 @@ describe ProjectLabel, models: true do
describe
'validations'
do
describe
'validations'
do
it
{
is_expected
.
to
validate_presence_of
(
:project
)
}
it
{
is_expected
.
to
validate_presence_of
(
:project
)
}
context
'validates if title must not exist at group level'
do
let
(
:group
)
{
create
(
:group
,
name:
'gitlab-org'
)
}
let
(
:project
)
{
create
(
:empty_project
,
group:
group
)
}
before
do
create
(
:group_label
,
group:
group
,
title:
'Bug'
)
end
it
'returns error if title already exists at group level'
do
label
=
described_class
.
new
(
project:
project
,
title:
'Bug'
)
label
.
valid?
expect
(
label
.
errors
[
:title
]).
to
include
'already exists at group level for gitlab-org. Please choose another one.'
end
it
'does not returns error if title does not exist at group level'
do
label
=
described_class
.
new
(
project:
project
,
title:
'Security'
)
label
.
valid?
expect
(
label
.
errors
[
:title
]).
to
be_empty
end
it
'does not returns error if project does not belong to group'
do
another_project
=
create
(
:empty_project
)
label
=
described_class
.
new
(
project:
another_project
,
title:
'Bug'
)
label
.
valid?
expect
(
label
.
errors
[
:title
]).
to
be_empty
end
end
end
end
end
end
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