2016-06-16 23:09:34 +05:30
|
|
|
class AwardEmoji < ActiveRecord::Base
|
|
|
|
DOWNVOTE_NAME = "thumbsdown".freeze
|
|
|
|
UPVOTE_NAME = "thumbsup".freeze
|
|
|
|
|
|
|
|
include Participable
|
2017-08-17 22:00:37 +05:30
|
|
|
include GhostUser
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
belongs_to :awardable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
|
2016-06-16 23:09:34 +05:30
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
validates :awardable, :user, presence: true
|
2016-08-24 12:49:21 +05:30
|
|
|
validates :name, presence: true, inclusion: { in: Gitlab::Emoji.emojis_names }
|
2017-08-17 22:00:37 +05:30
|
|
|
validates :name, uniqueness: { scope: [:user, :awardable_type, :awardable_id] }, unless: :ghost_user?
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
participant :user
|
|
|
|
|
|
|
|
scope :downvotes, -> { where(name: DOWNVOTE_NAME) }
|
|
|
|
scope :upvotes, -> { where(name: UPVOTE_NAME) }
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
class << self
|
|
|
|
def votes_for_collection(ids, type)
|
2017-09-10 17:25:29 +05:30
|
|
|
select('name', 'awardable_id', 'COUNT(*) as count')
|
|
|
|
.where('name IN (?) AND awardable_type = ? AND awardable_id IN (?)', [DOWNVOTE_NAME, UPVOTE_NAME], type, ids)
|
|
|
|
.group('name', 'awardable_id')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def downvote?
|
|
|
|
self.name == DOWNVOTE_NAME
|
|
|
|
end
|
|
|
|
|
|
|
|
def upvote?
|
|
|
|
self.name == UPVOTE_NAME
|
|
|
|
end
|
|
|
|
end
|