Class: SorbetRails::ModelPlugins::CustomFinderMethods

Inherits:
Base
  • Object
show all
Defined in:
lib/sorbet-rails/model_plugins/custom_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, #serialization_coder_for_column

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_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

Methods included from SorbetRails::ModelColumnUtils

#active_record_type_to_sorbet_type, #attribute_has_unconditional_presence_validation?, #model_class, #nilable_column?, #time_zone_aware_column?, #type_for_column_def

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
# File 'lib/sorbet-rails/model_plugins/custom_finder_methods.rb', line 6

def generate(root)
  model_class_rbi = root.create_class(self.model_class_name)
  model_relation_class_rbi = root.create_class(self.model_relation_class_name)
  model_assoc_proxy_class_rbi = root.create_class(self.model_assoc_proxy_class_name)
  model_assoc_relation_rbi = root.create_class(self.model_assoc_relation_class_name)

  custom_module_name = self.model_module_name("CustomFinderMethods")
  custom_module_rbi = root.create_module(custom_module_name)

  # and include the rbi module
  model_class_rbi.create_extend(custom_module_name)
  model_relation_class_rbi.create_include(custom_module_name)
  model_assoc_proxy_class_rbi.create_include(custom_module_name)
  model_assoc_relation_rbi.create_include(custom_module_name)

  custom_module_rbi.create_method(
    "first_n",
    parameters: [ Parameter.new("limit", type: "Integer") ],
    return_type: "T::Array[#{self.model_class_name}]",
  )

  custom_module_rbi.create_method(
    "last_n",
    parameters: [ Parameter.new("limit", type: "Integer") ],
    return_type: "T::Array[#{self.model_class_name}]",
  )

  custom_module_rbi.create_method(
    "find_n",
    parameters: [ Parameter.new("*args", type: "T::Array[T.any(Integer, String)]") ],
    return_type: "T::Array[#{self.model_class_name}]",
  )

  # allow common cases find_by_id
  custom_module_rbi.create_method(
    "find_by_id",
    parameters: [ Parameter.new("id", type: "Integer") ],
    return_type: "T.nilable(#{self.model_class_name})",
  )
  custom_module_rbi.create_method(
    "find_by_id!",
    parameters: [ Parameter.new("id", type: "Integer") ],
    return_type: self.model_class_name,
  )
end