blob: 933639434495fa7a0dc30ce4018fb2c51ac7817b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# frozen_string_literal: true
require 'set' # rubocop:disable Lint/RedundantRequireStatement -- Ruby 3.1 and earlier needs this. Drop this line after Ruby 3.2+ is only supported.
module Support
# Ensure that finders' `execute` method always returns
# `ActiveRecord::Relation`.
#
# See https://gitlab.com/gitlab-org/gitlab/-/issues/298771
module FinderCollection
def self.install_check(finder_class)
return unless check?(finder_class)
finder_class.prepend CheckResult
end
ALLOWLIST_YAML = File.join(__dir__, 'finder_collection_allowlist.yml')
def self.check?(finder_class)
@allowlist ||= YAML.load_file(ALLOWLIST_YAML).to_set
@allowlist.exclude?(finder_class.name)
end
module CheckResult
def execute(...)
result = super
unless result.is_a?(ActiveRecord::Relation)
raise <<~MESSAGE
#{self.class}#execute returned `#{result.class}` instead of `ActiveRecord::Relation`.
All finder classes are expected to return `ActiveRecord::Relation`.
Read more at https://docs.gitlab.com/ee/development/reusing_abstractions.html#finders
MESSAGE
end
result
end
end
end
end
RSpec.configure do |config|
config.before(:all, type: :finder) do
Support::FinderCollection.install_check(described_class)
end
end
|