Commit ba08811d authored by Grzegorz Bizon's avatar Grzegorz Bizon

Move note emoji-award implementation to note model (feature envy)

parent 22d87b74
......@@ -72,6 +72,7 @@ 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)
......@@ -349,4 +350,36 @@ class Note < ActiveRecord::Base
def editable?
!system?
end
# Checks if note is an award added from an issue 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.
#
# Method is executed as a before_validation callback.
#
def set_award!
return unless issue_comment? && contains_emoji_only?
self.is_award = true
self.note = award_emoji_name
end
private
def issue_comment?
noteable.kind_of?(Issue)
end
def contains_emoji_only?
(note =~ /\A:[-_+[:alnum:]]*:\s?\z/) ? true : false
end
def award_emoji_name
return nil unless contains_emoji_only?
note.match(/\A:([-_+[:alnum:]]*):\s?/)[1]
end
end
......@@ -5,11 +5,6 @@ module Notes
note.author = current_user
note.system = false
if award_emoji_note?
note.is_award = true
note.note = emoji_name
end
if note.save
notification_service.new_note(note)
......@@ -33,24 +28,5 @@ module Notes
note.project.execute_hooks(note_data, :note_hooks)
note.project.execute_services(note_data, :note_hooks)
end
private
def award_emoji_note?
# We support award-emojis only in issue discussion
issue_comment? && contains_emoji_only?
end
def contains_emoji_only?
params[:note] =~ /\A:[-_+[:alnum:]]*:\s?\z/
end
def issue_comment?
params[:noteable_type] == 'Issue'
end
def emoji_name
params[:note].match(/\A:([-_+[:alnum:]]*):\s?/)[1]
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment