Use a query in Project#protected_branch?
This changes Project#protected_branch? to use a query to check if a branch is protected, instead of loading all ProtectedBranch records into memory just to check if the list of names includes a given branch name.
This commit is contained in:
parent
47698071d9
commit
e28d1fa3cc
|
@ -764,7 +764,7 @@ class Project < ActiveRecord::Base
|
||||||
|
|
||||||
# Check if current branch name is marked as protected in the system
|
# Check if current branch name is marked as protected in the system
|
||||||
def protected_branch?(branch_name)
|
def protected_branch?(branch_name)
|
||||||
protected_branches_names.include?(branch_name)
|
protected_branches.where(name: branch_name).any?
|
||||||
end
|
end
|
||||||
|
|
||||||
def developers_can_push_to_protected_branch?(branch_name)
|
def developers_can_push_to_protected_branch?(branch_name)
|
||||||
|
|
|
@ -798,4 +798,18 @@ describe Project, models: true do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#protected_branch?' do
|
||||||
|
let(:project) { create(:empty_project) }
|
||||||
|
|
||||||
|
it 'returns true when a branch is a protected branch' do
|
||||||
|
project.protected_branches.create!(name: 'foo')
|
||||||
|
|
||||||
|
expect(project.protected_branch?('foo')).to eq(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns false when a branch is not a protected branch' do
|
||||||
|
expect(project.protected_branch?('foo')).to eq(false)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue