diff options
author | Grzegorz Bizon <grzegorz@gitlab.com> | 2015-12-07 12:26:56 +0000 |
---|---|---|
committer | Grzegorz Bizon <grzegorz@gitlab.com> | 2015-12-07 12:26:56 +0000 |
commit | 359d94607c2df324bc5cd9591bded05dbe9ca157 (patch) | |
tree | 1ab19b405514e2e980bb241dd379bb1a981b8932 /app/models | |
parent | 104df74f512dd7174a49504dda0b0910ee43b787 (diff) | |
parent | 893d08c0dc6a1eba14db7694636707f30b28a7f4 (diff) | |
download | gitlab-ce-359d94607c2df324bc5cd9591bded05dbe9ca157.tar.gz |
Merge branch 'fix/award-emoji-conflict-in-notes' into 'master'
Fix problems with award-emoji-only comment
This fixes a conflict between note with only a single emoji in content
and award-emojis mechanisms.
Closes #3734
cc @vsizov
See merge request !1936
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/note.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/app/models/note.rb b/app/models/note.rb index 1c6345e735c..239a0f77f8e 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 |