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 ...@@ -72,6 +72,7 @@ class Note < ActiveRecord::Base
serialize :st_diff serialize :st_diff
before_create :set_diff, if: ->(n) { n.line_code.present? } before_create :set_diff, if: ->(n) { n.line_code.present? }
before_validation :set_award!
class << self class << self
def discussions_from_notes(notes) def discussions_from_notes(notes)
...@@ -349,4 +350,36 @@ class Note < ActiveRecord::Base ...@@ -349,4 +350,36 @@ class Note < ActiveRecord::Base
def editable? def editable?
!system? !system?
end 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 end
...@@ -5,11 +5,6 @@ module Notes ...@@ -5,11 +5,6 @@ module Notes
note.author = current_user note.author = current_user
note.system = false note.system = false
if award_emoji_note?
note.is_award = true
note.note = emoji_name
end
if note.save if note.save
notification_service.new_note(note) notification_service.new_note(note)
...@@ -33,24 +28,5 @@ module Notes ...@@ -33,24 +28,5 @@ module Notes
note.project.execute_hooks(note_data, :note_hooks) note.project.execute_hooks(note_data, :note_hooks)
note.project.execute_services(note_data, :note_hooks) note.project.execute_services(note_data, :note_hooks)
end 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
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