award_emoji.rb 1.18 KB
Newer Older
1
2
3
4
5
class AwardEmoji < ActiveRecord::Base
  DOWNVOTE_NAME = "thumbsdown".freeze
  UPVOTE_NAME   = "thumbsup".freeze

  include Participable
6
  include GhostUser
7

8
  belongs_to :awardable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
9
10
11
  belongs_to :user

  validates :awardable, :user, presence: true
12
  validates :name, presence: true, inclusion: { in: Gitlab::Emoji.emojis_names }
13
  validates :name, uniqueness: { scope: [:user, :awardable_type, :awardable_id] }, unless: :ghost_user?
14
15
16
17
18
19

  participant :user

  scope :downvotes, -> { where(name: DOWNVOTE_NAME) }
  scope :upvotes,   -> { where(name: UPVOTE_NAME) }

20
21
22
  after_save :expire_etag_cache
  after_destroy :expire_etag_cache

23
24
  class << self
    def votes_for_collection(ids, type)
25
26
27
      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')
28
29
30
    end
  end

31
32
33
34
35
36
37
  def downvote?
    self.name == DOWNVOTE_NAME
  end

  def upvote?
    self.name == UPVOTE_NAME
  end
38
39

  def expire_etag_cache
Douwe Maan's avatar
Douwe Maan committed
40
    return unless awardable.respond_to?(:expire_etag_cache)
41
42
43

    awardable.expire_etag_cache
  end
44
end