gitlab-org--gitlab-foss/app/models/award_emoji.rb

69 lines
1.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class AwardEmoji < ApplicationRecord
DOWNVOTE_NAME = "thumbsdown"
UPVOTE_NAME = "thumbsup"
2016-04-16 15:09:08 -04:00
include Participable
include GhostUser
include Importable
2016-04-16 15:09:08 -04:00
belongs_to :awardable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
2016-04-16 15:09:08 -04:00
belongs_to :user
validates :user, presence: true
validates :awardable, presence: true, unless: :importing?
validates :name, presence: true, 'gitlab/emoji_name': true
validates :name, uniqueness: { scope: [:user, :awardable_type, :awardable_id] }, unless: -> { ghost_user? || importing? }
2016-04-16 15:09:08 -04:00
participant :user
scope :downvotes, -> { named(DOWNVOTE_NAME) }
scope :upvotes, -> { named(UPVOTE_NAME) }
scope :named, -> (names) { where(name: names) }
scope :awarded_by, -> (users) { where(user: users) }
2016-04-16 15:09:08 -04:00
after_save :expire_cache
after_destroy :expire_cache
class << self
def votes_for_collection(ids, type)
2017-06-21 09:48:12 -04:00
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')
end
# Returns the top 100 emoji awarded by the given user.
#
# The returned value is a Hash mapping emoji names to the number of times
# they were awarded:
#
# { 'thumbsup' => 2, 'thumbsdown' => 1 }
#
# user - The User to get the awards for.
# limt - The maximum number of emoji to return.
def award_counts_for_user(user, limit = 100)
limit(limit)
.where(user: user)
.group(:name)
.order('count_all DESC, name ASC')
.count
end
end
2016-04-16 15:09:08 -04:00
def downvote?
self.name == DOWNVOTE_NAME
end
def upvote?
self.name == UPVOTE_NAME
end
def expire_cache
awardable.try(:bump_updated_at)
awardable.try(:expire_etag_cache)
awardable.try(:update_upvotes_count) if upvote?
end
2016-04-16 15:09:08 -04:00
end