Welcome to mirror list, hosted at ThFree Co, Russian Federation.

relation_proxy.rb « lazy_relation_loader « loaders « graphql « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bab2a272fb07eaf4fdb3b4208ac44aa1c1e4a925 (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
49
50
51
52
# frozen_string_literal: true

module Gitlab
  module Graphql
    module Loaders
      class LazyRelationLoader
        # Proxies all the method calls to Registry instance.
        # The main purpose of having this is that calling load
        # on an instance of this class will only return the records
        # associated with the main Active Record model.
        class RelationProxy
          def initialize(object, registry)
            @object = object
            @registry = registry
          end

          def load
            registry.for(object)
          end
          alias_method :to_a, :load

          def last(limit = 1)
            result = registry.limit(limit)
                           .reverse_order!
                           .for(object)

            return result.first if limit == 1 # This is the Active Record behavior

            result
          end

          private

          attr_reader :registry, :object

          # Delegate everything to registry
          def method_missing(method_name, ...)
            result = registry.public_send(method_name, ...) # rubocop:disable GitlabSecurity/PublicSend

            return self if result == registry

            result
          end

          def respond_to_missing?(method_name, include_private = false)
            registry.respond_to?(method_name, include_private)
          end
        end
      end
    end
  end
end