Class: SorbetRails::ModelPlugins::ActiveRecordQuerying

Inherits:
Base
  • Object
show all
Defined in:
lib/sorbet-rails/model_plugins/active_record_querying.rb

Constant Summary

Constants inherited from Base

Base::Parameter

Instance Attribute Summary

Attributes inherited from Base

#available_classes, #model_class

Instance Method Summary collapse

Methods inherited from Base

#attribute_has_unconditional_presence_validation?, #initialize

Methods included from SorbetRails::ModelUtils

#add_relation_query_method, #exists_class_method?, #exists_instance_method?, #habtm_class?, #model_assoc_proxy_class_name, #model_assoc_relation_class_name, #model_class, #model_class_name, #model_module_name, #model_query_methods_returning_assoc_relation_module_name, #model_query_methods_returning_relation_module_name, #model_relation_class_name, #model_relation_type_alias, #model_relation_type_class_name

Constructor Details

This class inherits a constructor from SorbetRails::ModelPlugins::Base

Instance Method Details

#generate(root) ⇒ Object



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
53
54
55
56
57
58
59
60
61
# File 'lib/sorbet-rails/model_plugins/active_record_querying.rb', line 6

def generate(root)
  # All is a named scope that most method from ActiveRecord::Querying delegate to
  # rails/activerecord/lib/active_record/querying.rb:21
  add_relation_query_method(
    root,
    "all",
    builtin_query_method: true,
  )
  add_relation_query_method(
    root,
    "unscoped",
    parameters: [
      Parameter.new("&block", type: "T.nilable(T.proc.void)"),
    ],
    builtin_query_method: true,
  )

  # It's not possible to typedef all methods in ActiveRecord::Querying module to have the
  # matching type. By generating model-specific sig, we can typedef these methods to return
  # <Model>::Relation class.
  # rails/activerecord/lib/active_record/querying.rb
  model_query_relation_methods = [
    :select, :reselect, :order, :reorder, :group, :limit, :offset, :joins, :left_joins, :left_outer_joins,
    :where, :rewhere, :preload, :extract_associated, :eager_load, :includes, :from, :lock, :readonly, :or,
    :having, :create_with, :distinct, :references, :none, :unscope, :optimizer_hints, :merge, :except, :only,
  ]
  model_query_relation_methods.each do |method_name|
    add_relation_query_method(
      root,
      method_name.to_s,
      parameters: [
        Parameter.new("*args", type: "T.untyped"),
      ],
      builtin_query_method: true,
    ) if exists_class_method?(method_name)
  end

  add_relation_query_method(
    root,
    "extending",
    parameters: [
      Parameter.new("*args", type: "T.untyped"),
      Parameter.new("&block", type: "T.nilable(T.proc.void)"),
    ],
    builtin_query_method: true,
  )

  # These are technically "query methods" but they aren't chainable so instead of
  # adding conditionals to `add_relation_query_method` to handle this we'll just
  # handle them here.
  relation_module_rbi = root.create_module(self.model_query_methods_returning_relation_module_name)
  create_in_batches_method(relation_module_rbi, inner_type: self.model_relation_class_name)

  assoc_relation_module_rbi = root.create_module(self.model_query_methods_returning_assoc_relation_module_name)
  create_in_batches_method(assoc_relation_module_rbi, inner_type: self.model_assoc_relation_class_name)
end