Class: SorbetRails::ModelPlugins::ActiveRecordFinderMethods

Inherits:
Base
  • Object
show all
Defined in:
lib/sorbet-rails/model_plugins/active_record_finder_methods.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

#initialize

Methods included from SorbetRails::ModelUtils

#add_relation_query_method, #exists_class_method?, #exists_instance_method?, #model_assoc_proxy_class_name, #model_assoc_relation_class_name, #model_class, #model_class_name, #model_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

#create_finder_method_pair(class_rbi, method_name, class_method) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sorbet-rails/model_plugins/active_record_finder_methods.rb', line 119

def create_finder_method_pair(class_rbi, method_name, class_method)
  class_rbi.create_method(
    method_name,
    return_type: "T.nilable(#{self.model_class_name})",
    class_method: class_method,
  )
  class_rbi.create_method(
    "#{method_name}!",
    return_type: self.model_class_name,
    class_method: class_method,
  )
end

#create_finder_methods_for(class_rbi, class_method:, include_methods_that_return_self: true) ⇒ Object



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sorbet-rails/model_plugins/active_record_finder_methods.rb', line 27

def create_finder_methods_for(class_rbi, class_method:, include_methods_that_return_self: true)
  # The intent of this is to prevent these methods from being generated
  # unnecessarily when T.attached_class or Elem can be used to reduce
  # the number of method signatures that have to be generated.
  if include_methods_that_return_self
    class_rbi.create_method(
      "find",
      parameters: [ Parameter.new("*args", type: "T.untyped") ],
      return_type: self.model_class_name,
      class_method: class_method,
    )
    class_rbi.create_method(
      "find_by",
      parameters: [ Parameter.new("*args", type: "T.untyped") ],
      return_type: "T.nilable(#{self.model_class_name})",
      class_method: class_method,
    )
    class_rbi.create_method(
      "find_by!",
      parameters: [ Parameter.new("*args", type: "T.untyped") ],
      return_type: self.model_class_name,
      class_method: class_method
    )
    class_rbi.create_method(
      "find_or_initialize_by",
      parameters: [
        Parameter.new("attributes", type: "T.untyped") ,
        Parameter.new(
          "&block",
          type: "T.nilable(T.proc.params(object: #{self.model_class_name}).void)",
        ),
      ],
      return_type: self.model_class_name,
      class_method: class_method
    )
    class_rbi.create_method(
      "find_or_create_by",
      parameters: [
        Parameter.new("attributes", type: "T.untyped") ,
        Parameter.new(
          "&block",
          type: "T.nilable(T.proc.params(object: #{self.model_class_name}).void)"
        ),
      ],
      return_type: self.model_class_name,
      class_method: class_method
    )
    class_rbi.create_method(
      "find_or_create_by!",
      parameters: [
        Parameter.new("attributes", type: "T.untyped") ,
        Parameter.new(
          "&block",
          type: "T.nilable(T.proc.params(object: #{self.model_class_name}).void)",
        ),
      ],
      return_type: self.model_class_name,
      class_method: class_method
    )

    ["first", "second", "third", "third_to_last", "second_to_last", "last"].
      each do |method_name|
        create_finder_method_pair(class_rbi, method_name, class_method)
      end
  end

  # Checker methods
  class_rbi.create_method(
    "exists?",
    parameters: [ Parameter.new("conditions", type: "T.untyped", default: "nil") ],
    return_type: "T::Boolean",
    class_method: class_method,
  )

  ["any?", "many?", "none?", "one?", "empty?"].each do |method_name|
    class_rbi.create_method(
      method_name,
      parameters: [ Parameter.new("*args", type: "T.untyped") ],
      return_type: "T::Boolean",
      class_method: class_method,
    )
  end
end

#generate(root) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sorbet-rails/model_plugins/active_record_finder_methods.rb', line 6

def generate(root)
  model_class_rbi = root.create_class(self.model_class_name)
  create_finder_methods_for(model_class_rbi, class_method: true, include_methods_that_return_self: false)

  model_relation_class_rbi = root.create_class(self.model_relation_class_name)
  create_finder_methods_for(model_relation_class_rbi, class_method: false, include_methods_that_return_self: false)

  model_assoc_proxy_class_rbi = root.create_class(self.model_assoc_proxy_class_name)
  create_finder_methods_for(model_assoc_proxy_class_rbi, class_method: false)

  model_assoc_relation_rbi = root.create_class(self.model_assoc_relation_class_name)
  create_finder_methods_for(model_assoc_relation_rbi, class_method: false)
end