Commit 9ba45b78 authored by Mark Chao's avatar Mark Chao

Allow eager loading rules

To eager load rules, reuse the association without scope.
Filter those rules using `regular?` method.
parent 38916cff
......@@ -9,7 +9,7 @@ module EE
def preload_for_collection
@preload_for_collection ||= case collection_type
when 'MergeRequest'
super.push(:approvals)
super.push(:approvals, :approval_rules)
else
super
end
......
......@@ -39,4 +39,9 @@ class ApprovalMergeRequestRule < ApplicationRecord
self.approved_approver_ids = merge_request.approvals.map(&:user_id) & approvers.map(&:id)
end
def regular
!code_owner?
end
alias_method :regular?, :regular
end
......@@ -9,6 +9,11 @@ class ApprovalProjectRule < ApplicationRecord
scope :regular, -> { all }
scope :code_owner, -> { none }
def regular
true
end
alias_method :regular?, :regular
def code_owner
false
end
......
......@@ -36,7 +36,7 @@ class ApprovalState
end
def approval_rules_overwritten?
merge_request.approval_rules.regular.exists?
merge_request.approval_rules.any?(&:regular?)
end
alias_method :approvers_overwritten?, :approval_rules_overwritten?
......@@ -135,10 +135,10 @@ class ApprovalState
def regular_rules
strong_memoize(:regular_rules) do
rule_source = approval_rules_overwritten? ? merge_request : project
rules = rule_source.approval_rules.regular
rules = rule_source.approval_rules.select(&:regular?)
unless project.feature_available?(:multiple_approval_rules)
rules = rules.order(id: :asc).limit(1)
rules = rules[0, 1]
end
wrap_rules(rules)
......@@ -147,7 +147,7 @@ class ApprovalState
def code_owner_rules
strong_memoize(:code_owner_rules) do
wrap_rules(merge_request.approval_rules.code_owner)
wrap_rules(merge_request.approval_rules.select(&:code_owner?))
end
end
......
......@@ -14,6 +14,22 @@ describe ApprovalMergeRequestRule do
end
end
describe '#regular' do
it 'returns true for regular records' do
subject = create(:approval_merge_request_rule, merge_request: merge_request)
expect(subject.regular).to eq(true)
expect(subject.regular?).to eq(true)
end
it 'returns false for code owner records' do
subject = create(:approval_merge_request_rule, merge_request: merge_request, code_owner: true)
expect(subject.regular).to eq(false)
expect(subject.regular?).to eq(false)
end
end
describe '#approvers' do
before do
create(:group) do |group|
......
......@@ -21,6 +21,13 @@ describe ApprovalProjectRule do
end
end
describe '#regular' do
it 'returns true' do
expect(subject.regular).to eq(true)
expect(subject.regular?).to eq(true)
end
end
describe '#code_owner' do
it 'returns false' do
expect(subject.code_owner).to eq(false)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment