2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
|
|
|
|
2018-08-27 08:35:31 -04:00
|
|
|
class_methods do
|
2018-10-27 11:52:26 -04:00
|
|
|
def awarded(user, name = nil)
|
2020-05-06 23:09:46 -04:00
|
|
|
award_emoji_table = Arel::Table.new('award_emoji')
|
|
|
|
inner_query = award_emoji_table
|
|
|
|
.project('true')
|
|
|
|
.where(award_emoji_table[:user_id].eq(user.id))
|
|
|
|
.where(award_emoji_table[:awardable_type].eq(self.name))
|
|
|
|
.where(award_emoji_table[:awardable_id].eq(self.arel_table[:id]))
|
2017-08-30 03:48:55 -04:00
|
|
|
|
2020-05-06 23:09:46 -04:00
|
|
|
inner_query = inner_query.where(award_emoji_table[:name].eq(name)) if name.present?
|
|
|
|
|
|
|
|
where(inner_query.exists)
|
2017-08-30 03:48:55 -04:00
|
|
|
end
|
|
|
|
|
2020-05-06 23:09:46 -04:00
|
|
|
def not_awarded(user, name = nil)
|
|
|
|
award_emoji_table = Arel::Table.new('award_emoji')
|
|
|
|
inner_query = award_emoji_table
|
|
|
|
.project('true')
|
|
|
|
.where(award_emoji_table[:user_id].eq(user.id))
|
|
|
|
.where(award_emoji_table[:awardable_type].eq(self.name))
|
|
|
|
.where(award_emoji_table[:awardable_id].eq(self.arel_table[:id]))
|
|
|
|
|
|
|
|
inner_query = inner_query.where(award_emoji_table[:name].eq(name)) if name.present?
|
2018-10-26 22:52:06 -04:00
|
|
|
|
2020-05-06 23:09:46 -04:00
|
|
|
where(inner_query.exists.not)
|
2018-10-26 22:52:06 -04:00
|
|
|
end
|
|
|
|
|
2016-04-16 15:09:08 -04:00
|
|
|
def order_upvotes_desc
|
2018-12-06 11:57:19 -05:00
|
|
|
order_votes(AwardEmoji::UPVOTE_NAME, 'DESC')
|
|
|
|
end
|
|
|
|
|
|
|
|
def order_upvotes_asc
|
|
|
|
order_votes(AwardEmoji::UPVOTE_NAME, 'ASC')
|
2016-04-16 15:09:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def order_downvotes_desc
|
2018-12-06 11:57:19 -05:00
|
|
|
order_votes(AwardEmoji::DOWNVOTE_NAME, 'DESC')
|
2016-04-16 15:09:08 -04:00
|
|
|
end
|
|
|
|
|
2018-12-06 11:57:19 -05:00
|
|
|
# Order votes by emoji, optional sort order param `descending` defaults to true
|
|
|
|
def order_votes(emoji_name, direction)
|
2016-04-16 15:09:08 -04:00
|
|
|
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
|
|
|
|
|
2020-01-09 16:07:48 -05:00
|
|
|
joins(join_clause).group(awardable_table[:id]).reorder(
|
|
|
|
Arel.sql("COUNT(award_emoji.id) #{direction}")
|
|
|
|
)
|
2016-04-16 15:09:08 -04:00
|
|
|
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
|
|
|
|
2020-05-15 11:08:04 -04:00
|
|
|
if with_thumbs && (!project || project.show_default_award_emojis?)
|
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
|
|
|
|
|
2018-08-24 06:13:41 -04:00
|
|
|
def user_can_award?(current_user)
|
|
|
|
Ability.allowed?(current_user, :award_emoji, self)
|
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)
|
2019-06-17 21:44:43 -04:00
|
|
|
award_emoji.named(emoji_name).awarded_by(current_user).exists?
|
2016-07-03 10:04:22 -04:00
|
|
|
end
|
2016-04-16 15:09:08 -04:00
|
|
|
end
|