BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
176d6e2a
Commit
176d6e2a
authored
Dec 05, 2015
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor note awards to reuse `emoji_pattern` and improve validator
parent
83d8185f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
18 deletions
+14
-18
note.rb
app/models/note.rb
+14
-18
No files found.
app/models/note.rb
View file @
176d6e2a
...
...
@@ -39,9 +39,11 @@ class Note < ActiveRecord::Base
delegate
:name
,
to: :project
,
prefix:
true
delegate
:name
,
:email
,
to: :author
,
prefix:
true
before_validation
:set_award!
validates
:note
,
:project
,
presence:
true
validates
:note
,
uniqueness:
{
scope:
[
:author
,
:noteable_type
,
:noteable_id
]
},
if:
->
(
n
)
{
n
.
is_award
}
validates
:note
,
format:
{
with:
/\A[-_+[:alnum:]]*\z/
},
if:
->
(
n
)
{
n
.
is_award
}
validates
:note
,
inclusion:
{
in:
Emoji
.
emojis_names
},
if:
->
(
n
)
{
n
.
is_award
}
validates
:line_code
,
format:
{
with:
/\A[a-z0-9]+_\d+_\d+\Z/
},
allow_blank:
true
# Attachments are deprecated and are handled by Markdown uploader
validates
:attachment
,
file_size:
{
maximum: :max_attachment_size
}
...
...
@@ -72,7 +74,6 @@ class Note < ActiveRecord::Base
serialize
:st_diff
before_create
:set_diff
,
if:
->
(
n
)
{
n
.
line_code
.
present?
}
before_validation
:set_award!
class
<<
self
def
discussions_from_notes
(
notes
)
...
...
@@ -351,36 +352,31 @@ class Note < ActiveRecord::Base
!
system
?
end
# Checks if note is an award added
from an issue comment.
# Checks if note is an award added
as a comment
#
# If note is an award, this method sets is_award to true,
# and changes note content to award-emoji name.
#
# Awards are only supported for issue comments.
# If note is an award, this method sets is_award to true
# and changes content of the note to award name.
#
# Method is executed as a before_validation callback.
#
def
set_award!
return
unless
supports_awards?
&&
contains_emoji_only?
return
unless
awards_supported?
&&
contains_emoji_only?
self
.
is_award
=
true
self
.
note
=
award_emoji_name
end
def
supports_awards?
noteable
.
kind_of?
(
Issue
)
||
noteable
.
is_a?
(
MergeRequest
)
end
private
def
awards_supported?
noteable
.
kind_of?
(
Issue
)
||
noteable
.
is_a?
(
MergeRequest
)
end
def
contains_emoji_only?
(
note
=~
/\A:[-_+[:alnum:]]*:\s?\z/
)
?
true
:
false
emoji_only_pattern
=
/\A
#{
Gitlab
::
Markdown
::
EmojiFilter
.
emoji_pattern
}
\s?\Z/
(
note
=~
emoji_only_pattern
)
?
true
:
false
end
def
award_emoji_name
return
nil
unless
contains_emoji_only?
note
.
match
(
/\A:([-_+[:alnum:]]*):\s?/
)[
1
]
note
.
match
(
Gitlab
::
Markdown
::
EmojiFilter
.
emoji_pattern
)[
1
]
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