debian-mirror-gitlab/app/models/concerns/awardable.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

109 lines
3.1 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
module Awardable
extend ActiveSupport::Concern
included do
2017-09-10 17:25:29 +05:30
has_many :award_emoji, -> { includes(:user).order(:id) }, as: :awardable, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
if self < Participable
2016-08-24 12:49:21 +05:30
# By default we always load award_emoji user association
participant :award_emoji
end
end
2018-11-20 20:47:30 +05:30
class_methods do
2018-12-13 13:39:08 +05:30
def awarded(user, name = nil)
2020-05-24 23:13:21 +05:30
award_emoji_table = Arel::Table.new('award_emoji')
inner_query = award_emoji_table
.project('true')
.where(award_emoji_table[:user_id].eq(user.id))
2022-07-23 23:45:48 +05:30
.where(award_emoji_table[:awardable_type].eq(base_class.name))
2020-05-24 23:13:21 +05:30
.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-03-17 18:26:18 +05:30
2020-05-24 23:13:21 +05:30
where(inner_query.exists)
2018-03-17 18:26:18 +05:30
end
2020-05-24 23:13:21 +05:30
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))
2022-07-23 23:45:48 +05:30
.where(award_emoji_table[:awardable_type].eq(base_class.name))
2020-05-24 23:13:21 +05:30
.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-12-13 13:39:08 +05:30
2020-05-24 23:13:21 +05:30
where(inner_query.exists.not)
2018-12-13 13:39:08 +05:30
end
def order_upvotes_desc
2019-02-15 15:39:39 +05:30
order_votes(AwardEmoji::UPVOTE_NAME, 'DESC')
end
def order_upvotes_asc
order_votes(AwardEmoji::UPVOTE_NAME, 'ASC')
end
def order_downvotes_desc
2019-02-15 15:39:39 +05:30
order_votes(AwardEmoji::DOWNVOTE_NAME, 'DESC')
end
2019-02-15 15:39:39 +05:30
# Order votes by emoji, optional sort order param `descending` defaults to true
def order_votes(emoji_name, direction)
awardable_table = self.arel_table
awards_table = AwardEmoji.arel_table
2022-07-23 23:45:48 +05:30
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(base_class.name).and(awards_table[:name].eq(emoji_name))))
.join_sources
2020-03-13 15:44:24 +05:30
joins(join_clause).group(awardable_table[:id]).reorder(
Arel.sql("COUNT(award_emoji.id) #{direction}")
)
end
end
def grouped_awards(with_thumbs: true)
2016-08-24 12:49:21 +05:30
# By default we always load award_emoji user association
awards = award_emoji.group_by(&:name)
2020-05-24 23:13:21 +05:30
if with_thumbs && (!project || project.show_default_award_emojis?)
awards[AwardEmoji::UPVOTE_NAME] ||= []
awards[AwardEmoji::DOWNVOTE_NAME] ||= []
end
awards
end
def downvotes
award_emoji.downvotes.count
end
def upvotes
award_emoji.upvotes.count
end
def emoji_awardable?
true
end
2018-11-20 20:47:30 +05:30
def user_can_award?(current_user)
Ability.allowed?(current_user, :award_emoji, self)
2016-09-29 09:46:39 +05:30
end
def user_authored?(current_user)
author = self.respond_to?(:author) ? self.author : self.user
author == current_user
end
def awarded_emoji?(emoji_name, current_user)
2019-12-04 20:38:33 +05:30
award_emoji.named(emoji_name).awarded_by(current_user).exists?
2016-08-24 12:49:21 +05:30
end
end