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
Kazuhiko Shiozaki
gitlab-ce
Commits
e48391b8
Commit
e48391b8
authored
Dec 01, 2015
by
Robert Speicher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add custom ColorValidator
parent
b3200c8c
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
47 additions
and
14 deletions
+47
-14
app/models/broadcast_message.rb
app/models/broadcast_message.rb
+4
-4
app/models/label.rb
app/models/label.rb
+1
-3
app/validators/color_validator.rb
app/validators/color_validator.rb
+20
-0
features/steps/admin/labels.rb
features/steps/admin/labels.rb
+1
-1
features/steps/project/issues/labels.rb
features/steps/project/issues/labels.rb
+1
-1
spec/models/broadcast_message_spec.rb
spec/models/broadcast_message_spec.rb
+15
-0
spec/requests/api/labels_spec.rb
spec/requests/api/labels_spec.rb
+5
-5
No files found.
app/models/broadcast_message.rb
View file @
e48391b8
...
...
@@ -16,12 +16,12 @@
class
BroadcastMessage
<
ActiveRecord
::
Base
include
Sortable
validates
:message
,
presence:
true
validates
:message
,
presence:
true
validates
:starts_at
,
presence:
true
validates
:ends_at
,
presence:
true
validates
:ends_at
,
presence:
true
validates
:color
,
format:
{
with:
/\A\#[0-9A-Fa-f]{3}{1,2}+\Z/
},
allow_blank
:
true
validates
:font
,
format:
{
with:
/\A\#[0-9A-Fa-f]{3}{1,2}+\Z/
},
allow_blank
:
true
validates
:color
,
allow_blank:
true
,
color
:
true
validates
:font
,
allow_blank:
true
,
color
:
true
def
self
.
current
where
(
"ends_at > :now AND starts_at < :now"
,
now:
Time
.
zone
.
now
).
last
...
...
app/models/label.rb
View file @
e48391b8
...
...
@@ -27,9 +27,7 @@ class Label < ActiveRecord::Base
has_many
:label_links
,
dependent: :destroy
has_many
:issues
,
through: :label_links
,
source: :target
,
source_type:
'Issue'
validates
:color
,
format:
{
with:
/\A#[0-9A-Fa-f]{6}\Z/
},
allow_blank:
false
validates
:color
,
color:
true
,
allow_blank:
false
validates
:project
,
presence:
true
,
unless:
Proc
.
new
{
|
service
|
service
.
template?
}
# Don't allow '?', '&', and ',' for label titles
...
...
app/validators/color_validator.rb
0 → 100644
View file @
e48391b8
# ColorValidator
#
# Custom validator for web color codes. It requires the leading hash symbol and
# will accept RGB triplet or hexadecimal formats.
#
# Example:
#
# class User < ActiveRecord::Base
# validates :background_color, allow_blank: true, color: true
# end
#
class
ColorValidator
<
ActiveModel
::
EachValidator
PATTERN
=
/\A\#[0-9A-Fa-f]{3}{1,2}+\Z/
.
freeze
def
validate_each
(
record
,
attribute
,
value
)
unless
value
=~
PATTERN
record
.
errors
.
add
(
attribute
,
"must be a valid color code"
)
end
end
end
features/steps/admin/labels.rb
View file @
e48391b8
...
...
@@ -71,7 +71,7 @@ class Spinach::Features::AdminIssuesLabels < Spinach::FeatureSteps
step
'I should see label color error message'
do
page
.
within
'.label-form'
do
expect
(
page
).
to
have_content
'Color
is invalid
'
expect
(
page
).
to
have_content
'Color
must be a valid color code
'
end
end
...
...
features/steps/project/issues/labels.rb
View file @
e48391b8
...
...
@@ -55,7 +55,7 @@ class Spinach::Features::ProjectIssuesLabels < Spinach::FeatureSteps
step
'I should see label color error message'
do
page
.
within
'.label-form'
do
expect
(
page
).
to
have_content
'Color
is invalid
'
expect
(
page
).
to
have_content
'Color
must be a valid color code
'
end
end
...
...
spec/models/broadcast_message_spec.rb
View file @
e48391b8
...
...
@@ -20,6 +20,21 @@ describe BroadcastMessage do
it
{
is_expected
.
to
be_valid
}
describe
'validations'
do
let
(
:triplet
)
{
'#000'
}
let
(
:hex
)
{
'#AABBCC'
}
it
{
is_expected
.
to
allow_value
(
nil
).
for
(
:color
)
}
it
{
is_expected
.
to
allow_value
(
triplet
).
for
(
:color
)
}
it
{
is_expected
.
to
allow_value
(
hex
).
for
(
:color
)
}
it
{
is_expected
.
not_to
allow_value
(
'000'
).
for
(
:color
)
}
it
{
is_expected
.
to
allow_value
(
nil
).
for
(
:font
)
}
it
{
is_expected
.
to
allow_value
(
triplet
).
for
(
:font
)
}
it
{
is_expected
.
to
allow_value
(
hex
).
for
(
:font
)
}
it
{
is_expected
.
not_to
allow_value
(
'000'
).
for
(
:font
)
}
end
describe
:current
do
it
"should return last message if time match"
do
broadcast_message
=
create
(
:broadcast_message
,
starts_at:
Time
.
now
.
yesterday
,
ends_at:
Time
.
now
.
tomorrow
)
...
...
spec/requests/api/labels_spec.rb
View file @
e48391b8
...
...
@@ -47,7 +47,7 @@ describe API::API, api: true do
name:
'Foo'
,
color:
'#FFAA'
expect
(
response
.
status
).
to
eq
(
400
)
expect
(
json_response
[
'message'
][
'color'
]).
to
eq
([
'
is invalid
'
])
expect
(
json_response
[
'message'
][
'color'
]).
to
eq
([
'
must be a valid color code
'
])
end
it
'should return 400 for too long color code'
do
...
...
@@ -55,7 +55,7 @@ describe API::API, api: true do
name:
'Foo'
,
color:
'#FFAAFFFF'
expect
(
response
.
status
).
to
eq
(
400
)
expect
(
json_response
[
'message'
][
'color'
]).
to
eq
([
'
is invalid
'
])
expect
(
json_response
[
'message'
][
'color'
]).
to
eq
([
'
must be a valid color code
'
])
end
it
'should return 400 for invalid name'
do
...
...
@@ -151,12 +151,12 @@ describe API::API, api: true do
expect
(
json_response
[
'message'
][
'title'
]).
to
eq
([
'is invalid'
])
end
it
'should return 400
for invalid name
'
do
it
'should return 400
when color code is too short
'
do
put
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'label1'
,
color:
'#FF'
expect
(
response
.
status
).
to
eq
(
400
)
expect
(
json_response
[
'message'
][
'color'
]).
to
eq
([
'
is invalid
'
])
expect
(
json_response
[
'message'
][
'color'
]).
to
eq
([
'
must be a valid color code
'
])
end
it
'should return 400 for too long color code'
do
...
...
@@ -164,7 +164,7 @@ describe API::API, api: true do
name:
'Foo'
,
color:
'#FFAAFFFF'
expect
(
response
.
status
).
to
eq
(
400
)
expect
(
json_response
[
'message'
][
'color'
]).
to
eq
([
'
is invalid
'
])
expect
(
json_response
[
'message'
][
'color'
]).
to
eq
([
'
must be a valid color code
'
])
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