2020-07-15 14:09:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-08-23 14:11:55 -04:00
|
|
|
module Approvable
|
2020-07-15 14:09:09 -04:00
|
|
|
extend ActiveSupport::Concern
|
2020-09-29 11:10:08 -04:00
|
|
|
include FromUnion
|
2020-07-15 14:09:09 -04:00
|
|
|
|
|
|
|
included do
|
|
|
|
has_many :approvals, dependent: :delete_all # rubocop:disable Cop/ActiveRecordDependent
|
|
|
|
has_many :approved_by_users, through: :approvals, source: :user
|
2020-09-29 11:10:08 -04:00
|
|
|
|
|
|
|
scope :without_approvals, -> { left_outer_joins(:approvals).where(approvals: { id: nil }) }
|
|
|
|
scope :with_approvals, -> { joins(:approvals) }
|
|
|
|
scope :approved_by_users_with_ids, -> (*user_ids) do
|
|
|
|
with_approvals
|
|
|
|
.merge(Approval.with_user)
|
|
|
|
.where(users: { id: user_ids })
|
|
|
|
.group(:id)
|
|
|
|
.having("COUNT(users.id) = ?", user_ids.size)
|
|
|
|
end
|
|
|
|
scope :approved_by_users_with_usernames, -> (*usernames) do
|
|
|
|
with_approvals
|
|
|
|
.merge(Approval.with_user)
|
|
|
|
.where(users: { username: usernames })
|
|
|
|
.group(:id)
|
|
|
|
.having("COUNT(users.id) = ?", usernames.size)
|
|
|
|
end
|
2021-07-01 05:07:33 -04:00
|
|
|
|
|
|
|
scope :not_approved_by_users_with_usernames, -> (usernames) do
|
|
|
|
users = User.where(username: usernames).select(:id)
|
|
|
|
self_table = self.arel_table
|
|
|
|
app_table = Approval.arel_table
|
|
|
|
|
|
|
|
where(
|
|
|
|
Approval.where(approvals: { user_id: users })
|
|
|
|
.where(app_table[:merge_request_id].eq(self_table[:id]))
|
|
|
|
.select('true')
|
|
|
|
.arel.exists.not
|
|
|
|
)
|
|
|
|
end
|
2020-09-29 11:10:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def select_from_union(relations)
|
|
|
|
where(id: from_union(relations))
|
|
|
|
end
|
2020-07-15 14:09:09 -04:00
|
|
|
end
|
|
|
|
|
2020-07-16 11:09:38 -04:00
|
|
|
def approved_by?(user)
|
2020-07-15 14:09:09 -04:00
|
|
|
return false unless user
|
|
|
|
|
|
|
|
approved_by_users.include?(user)
|
|
|
|
end
|
2020-07-20 14:09:27 -04:00
|
|
|
|
|
|
|
def can_be_approved_by?(user)
|
|
|
|
user && !approved_by?(user) && user.can?(:approve_merge_request, self)
|
|
|
|
end
|
2021-09-03 14:10:36 -04:00
|
|
|
|
|
|
|
def can_be_unapproved_by?(user)
|
|
|
|
user && approved_by?(user) && user.can?(:approve_merge_request, self)
|
|
|
|
end
|
2020-07-15 14:09:09 -04:00
|
|
|
end
|
2022-08-23 14:11:55 -04:00
|
|
|
|
|
|
|
Approvable.prepend_mod
|