summaryrefslogtreecommitdiff
path: root/app/models/note.rb
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2015-12-07 14:48:53 +0100
committerDouwe Maan <douwe@gitlab.com>2015-12-07 14:48:53 +0100
commitd611a3879816d07f5842e2bdcea69b0813777107 (patch)
tree5f0df5c5d68731198b74b2df3d27070ef632d79c /app/models/note.rb
parent0dcff1342b38c093a05c9edd13d422ee0b37b7bd (diff)
parente88fd58671f5407d80fafe1070d48b750b9f2e50 (diff)
downloadgitlab-ce-d611a3879816d07f5842e2bdcea69b0813777107.tar.gz
Merge branch 'master' into reference-pipeline-and-caching
Diffstat (limited to 'app/models/note.rb')
-rw-r--r--app/models/note.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/app/models/note.rb b/app/models/note.rb
index 41815625e66..dd892c9273e 100644
--- a/app/models/note.rb
+++ b/app/models/note.rb
@@ -39,8 +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, 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 }
@@ -348,4 +351,31 @@ class Note < ActiveRecord::Base
def editable?
!system?
end
+
+ # Checks if note is an award added as a comment
+ #
+ # 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 awards_supported? && contains_emoji_only?
+ self.is_award = true
+ self.note = award_emoji_name
+ end
+
+ private
+
+ def awards_supported?
+ noteable.kind_of?(Issue) || noteable.is_a?(MergeRequest)
+ end
+
+ def contains_emoji_only?
+ note =~ /\A#{Gitlab::Markdown::EmojiFilter.emoji_pattern}\s?\Z/
+ end
+
+ def award_emoji_name
+ note.match(Gitlab::Markdown::EmojiFilter.emoji_pattern)[1]
+ end
end