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

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



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sorbet-rails/model_plugins/active_record_finder_methods.rb', line 71

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:) ⇒ Object



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_finder_methods.rb', line 21

def create_finder_methods_for(class_rbi, class_method:)
  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
  )

  ["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

  # 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?"].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)

  model_relation_class_rbi = root.create_class(self.model_relation_class_name)
  create_finder_methods_for(model_relation_class_rbi, class_method: 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