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

45 lines
1.2 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module ApprovableBase
extend ActiveSupport::Concern
2021-01-03 14:25:43 +05:30
include FromUnion
2020-07-28 23:09:34 +05:30
included do
has_many :approvals, dependent: :delete_all # rubocop:disable Cop/ActiveRecordDependent
has_many :approved_by_users, through: :approvals, source: :user
2021-01-03 14:25:43 +05:30
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
end
class_methods do
def select_from_union(relations)
where(id: from_union(relations))
end
2020-07-28 23:09:34 +05:30
end
def approved_by?(user)
return false unless user
approved_by_users.include?(user)
end
def can_be_approved_by?(user)
user && !approved_by?(user) && user.can?(:approve_merge_request, self)
end
end