Commit e24e5046 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Fix match_id_and_lock_version scope

Fixes the scope so that it actually filters by id and lock version
parent b15e631e
...@@ -62,13 +62,16 @@ class CommitStatus < ApplicationRecord ...@@ -62,13 +62,16 @@ class CommitStatus < ApplicationRecord
preload(project: :namespace) preload(project: :namespace)
end end
scope :match_id_and_lock_version, -> (slice) do scope :match_id_and_lock_version, -> (items) do
# it expects that items are an array of attributes to match # it expects that items are an array of attributes to match
# each hash needs to have `id` and `lock_version` # each hash needs to have `id` and `lock_version`
slice.inject(self) do |relation, item| or_conditions = items.inject(none) do |relation, item|
match = CommitStatus.unscoped.where(item.slice(:id, :lock_version)) match = CommitStatus.default_scoped.where(item.slice(:id, :lock_version))
relation.or(match) relation.or(match)
end end
merge(or_conditions)
end end
# We use `CommitStatusEnums.failure_reasons` here so that EE can more easily # We use `CommitStatusEnums.failure_reasons` here so that EE can more easily
......
...@@ -449,6 +449,19 @@ describe CommitStatus do ...@@ -449,6 +449,19 @@ describe CommitStatus do
end end
end end
describe '.match_id_and_lock_version' do
let(:status_1) { create_status(lock_version: 1) }
let(:status_2) { create_status(lock_version: 2) }
it 'returns statuses that match the given id and lock versions' do
params = [
{ id: status_1.id, lock_version: 1 },
{ id: status_2.id, lock_version: 3 }
]
expect(described_class.match_id_and_lock_version(params)).to contain_exactly(status_1)
end
end
describe '#before_sha' do describe '#before_sha' do
subject { commit_status.before_sha } subject { commit_status.before_sha }
......
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