2016-04-16 15:09:08 -04:00
|
|
|
module Awardable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2017-06-08 11:16:27 -04:00
|
|
|
has_many :award_emoji, -> { includes(:user).order(:id) }, as: :awardable, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
|
2016-04-16 15:09:08 -04:00
|
|
|
|
|
|
|
if self < Participable
|
2016-06-21 01:26:43 -04:00
|
|
|
# By default we always load award_emoji user association
|
|
|
|
participant :award_emoji
|
2016-04-16 15:09:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2017-08-30 03:48:55 -04:00
|
|
|
def awarded(user, name)
|
|
|
|
sql = <<~EOL
|
|
|
|
EXISTS (
|
|
|
|
SELECT TRUE
|
|
|
|
FROM award_emoji
|
|
|
|
WHERE user_id = :user_id AND
|
|
|
|
name = :name AND
|
|
|
|
awardable_type = :awardable_type AND
|
|
|
|
awardable_id = #{self.arel_table.name}.id
|
|
|
|
)
|
|
|
|
EOL
|
|
|
|
|
|
|
|
where(sql, user_id: user.id, name: name, awardable_type: self.name)
|
|
|
|
end
|
|
|
|
|
2016-04-16 15:09:08 -04:00
|
|
|
def order_upvotes_desc
|
|
|
|
order_votes_desc(AwardEmoji::UPVOTE_NAME)
|
|
|
|
end
|
|
|
|
|
|
|
|
def order_downvotes_desc
|
|
|
|
order_votes_desc(AwardEmoji::DOWNVOTE_NAME)
|
|
|
|
end
|
|
|
|
|
|
|
|
def order_votes_desc(emoji_name)
|
|
|
|
awardable_table = self.arel_table
|
|
|
|
awards_table = AwardEmoji.arel_table
|
|
|
|
|
|
|
|
join_clause = awardable_table.join(awards_table, Arel::Nodes::OuterJoin).on(
|
|
|
|
awards_table[:awardable_id].eq(awardable_table[:id]).and(
|
|
|
|
awards_table[:awardable_type].eq(self.name).and(
|
|
|
|
awards_table[:name].eq(emoji_name)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
).join_sources
|
|
|
|
|
|
|
|
joins(join_clause).group(awardable_table[:id]).reorder("COUNT(award_emoji.id) DESC")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-11 16:43:58 -04:00
|
|
|
def grouped_awards(with_thumbs: true)
|
2016-06-21 01:26:43 -04:00
|
|
|
# By default we always load award_emoji user association
|
|
|
|
awards = award_emoji.group_by(&:name)
|
2016-04-16 15:09:08 -04:00
|
|
|
|
|
|
|
if with_thumbs
|
2016-05-11 16:43:58 -04:00
|
|
|
awards[AwardEmoji::UPVOTE_NAME] ||= []
|
|
|
|
awards[AwardEmoji::DOWNVOTE_NAME] ||= []
|
2016-04-16 15:09:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
awards
|
|
|
|
end
|
|
|
|
|
|
|
|
def downvotes
|
2016-05-11 16:43:58 -04:00
|
|
|
award_emoji.downvotes.count
|
2016-04-16 15:09:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def upvotes
|
2016-05-11 16:43:58 -04:00
|
|
|
award_emoji.upvotes.count
|
2016-04-16 15:09:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def emoji_awardable?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2016-08-17 07:21:06 -04:00
|
|
|
def awardable_votes?(name)
|
|
|
|
AwardEmoji::UPVOTE_NAME == name || AwardEmoji::DOWNVOTE_NAME == name
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_can_award?(current_user, name)
|
2016-08-30 15:57:47 -04:00
|
|
|
if user_authored?(current_user)
|
|
|
|
!awardable_votes?(normalize_name(name))
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2016-08-17 07:21:06 -04:00
|
|
|
end
|
|
|
|
|
2016-09-04 04:51:12 -04:00
|
|
|
def user_authored?(current_user)
|
|
|
|
author = self.respond_to?(:author) ? self.author : self.user
|
|
|
|
|
|
|
|
author == current_user
|
|
|
|
end
|
|
|
|
|
2016-04-16 15:09:08 -04:00
|
|
|
def awarded_emoji?(emoji_name, current_user)
|
|
|
|
award_emoji.where(name: emoji_name, user: current_user).exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_award_emoji(name, current_user)
|
|
|
|
return unless emoji_awardable?
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2016-07-03 10:04:22 -04:00
|
|
|
award_emoji.create(name: normalize_name(name), user: current_user)
|
2016-04-16 15:09:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_award_emoji(name, current_user)
|
|
|
|
award_emoji.where(name: name, user: current_user).destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
def toggle_award_emoji(emoji_name, current_user)
|
|
|
|
if awarded_emoji?(emoji_name, current_user)
|
|
|
|
remove_award_emoji(emoji_name, current_user)
|
|
|
|
else
|
|
|
|
create_award_emoji(emoji_name, current_user)
|
|
|
|
end
|
|
|
|
end
|
2016-07-03 10:04:22 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def normalize_name(name)
|
2017-03-06 12:59:26 -05:00
|
|
|
Gitlab::Emoji.normalize_emoji_name(name)
|
2016-07-03 10:04:22 -04:00
|
|
|
end
|
2016-04-16 15:09:08 -04:00
|
|
|
end
|