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

43 lines
1.1 KiB
Ruby
Raw Normal View History

2016-04-16 19:09:08 +00:00
class AwardEmoji < ActiveRecord::Base
DOWNVOTE_NAME = "thumbsdown".freeze
UPVOTE_NAME = "thumbsup".freeze
include Participable
include GhostUser
2016-04-16 19:09:08 +00:00
belongs_to :awardable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
2016-04-16 19:09:08 +00:00
belongs_to :user
validates :awardable, :user, presence: true
validates :name, presence: true, inclusion: { in: Gitlab::Emoji.emojis_names }
validates :name, uniqueness: { scope: [:user, :awardable_type, :awardable_id] }, unless: :ghost_user?
2016-04-16 19:09:08 +00:00
participant :user
scope :downvotes, -> { where(name: DOWNVOTE_NAME) }
scope :upvotes, -> { where(name: UPVOTE_NAME) }
after_save :expire_etag_cache
after_destroy :expire_etag_cache
class << self
def votes_for_collection(ids, type)
2017-06-21 13:48:12 +00: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
end
2016-04-16 19:09:08 +00:00
def downvote?
self.name == DOWNVOTE_NAME
end
def upvote?
self.name == UPVOTE_NAME
end
def expire_etag_cache
awardable.try(:expire_etag_cache)
end
2016-04-16 19:09:08 +00:00
end