3e1a1242c6
... when the user is destroyed. 1. Normally, for a given awardable and award emoji name, a user is only allowed to create a single award emoji. 2. This validation needs to be removed for ghost users, since: - User A and User B have created award emoji - with the same name and against the same awardable - User A is deleted. Their award emoji is moved to the ghost user - User B is deleted. Their award emoji needs to be moved to the ghost user. However, this breaks the uniqueness validation, since the ghost user is only allowed to have one award emoji of a given name for a given awardable
35 lines
973 B
Ruby
35 lines
973 B
Ruby
class AwardEmoji < ActiveRecord::Base
|
|
DOWNVOTE_NAME = "thumbsdown".freeze
|
|
UPVOTE_NAME = "thumbsup".freeze
|
|
|
|
include Participable
|
|
include GhostUser
|
|
|
|
belongs_to :awardable, polymorphic: true
|
|
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?
|
|
|
|
participant :user
|
|
|
|
scope :downvotes, -> { where(name: DOWNVOTE_NAME) }
|
|
scope :upvotes, -> { where(name: UPVOTE_NAME) }
|
|
|
|
class << self
|
|
def votes_for_collection(ids, type)
|
|
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
|
|
|
|
def downvote?
|
|
self.name == DOWNVOTE_NAME
|
|
end
|
|
|
|
def upvote?
|
|
self.name == UPVOTE_NAME
|
|
end
|
|
end
|