This repository was archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathmodel_utils.rb
165 lines (143 loc) · 5.58 KB
/
model_utils.rb
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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# typed: strict
require('sorbet-rails/model_column_utils')
module SorbetRails::ModelUtils
extend T::Sig
extend T::Helpers
include SorbetRails::ModelColumnUtils
abstract!
# if we're a HABTM class then model_class is an anonymous class (see the rails link below) and
# i'm not sure how to explain that to sorbet other than T.class_of(Class).
# This is also defined in ModelColumnUtils
# sig { abstract.returns(T.any(T.class_of(ActiveRecord::Base), T.class_of(Class))) }
# def model_class; end
sig { returns(T::Boolean) }
def habtm_class?
# checking the class name seems to be the cleanest way to figure this out, see:
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb#L54
T.must(model_class.name).start_with?('HABTM_')
end
sig { returns(String) }
def model_class_name
model_class.to_s
end
sig { returns(String) }
def model_relation_class_name
"#{model_class_name}::ActiveRecord_Relation"
end
sig { returns(String) }
def model_assoc_proxy_class_name
"#{model_class_name}::ActiveRecord_Associations_CollectionProxy"
end
sig { returns(String) }
def model_assoc_relation_class_name
"#{model_class_name}::ActiveRecord_AssociationRelation"
end
sig { returns(String) }
def model_query_methods_returning_relation_module_name
"#{model_class_name}::QueryMethodsReturningRelation"
end
sig { returns(String) }
def model_query_methods_returning_assoc_relation_module_name
"#{model_class_name}::QueryMethodsReturningAssociationRelation"
end
sig { returns(String) }
def model_relation_type_alias
types = [
self.model_relation_class_name,
self.model_assoc_proxy_class_name,
self.model_assoc_relation_class_name
].join(', ')
"T.any(#{types})"
end
sig { returns(String) }
def model_relation_type_class_name
'RelationType'
end
sig { params(module_name: String).returns(String) }
def model_module_name(module_name)
"#{model_class_name}::#{module_name}"
end
sig { params(method_name: T.any(String, Symbol)).returns(T::Boolean) }
def exists_instance_method?(method_name)
model_class.method_defined?(method_name)
end
sig { params(method_name: T.any(String, Symbol)).returns(T::Boolean) }
def exists_class_method?(method_name)
model_class.respond_to?(method_name)
end
sig {
params(
root: Parlour::RbiGenerator::Namespace,
method_name: String,
parameters: T.nilable(T::Array[::Parlour::RbiGenerator::Parameter]),
# This is meant to indicate the method is a rails-provided query method like
# where, limit, etc and not something like a named scope. It should likely
# only be set to `true` when called from the ActiveRecordQuerying plugin.
builtin_query_method: T::Boolean,
custom_return_value: T.nilable(String),
).void
}
def add_relation_query_method(
root,
method_name,
parameters: nil,
builtin_query_method: false,
custom_return_value: nil
)
# a relation querying method will be available on
# - model (as a class method)
# - activerecord relation
# - asocciation collection proxy
# - association relation
# in case (1) and (2), it returns a Model::ActiveRecord_Relation
# in case (3) and (4), it returns a Model::ActiveRecord_AssociationRelation
# 'unscoped' is a special case where it always returns a ActiveRecord_Relation
assoc_return_value = method_name == 'unscoped' ? self.model_relation_class_name : self.model_assoc_relation_class_name
# We can put methods onto modules which are extended/included by the model
# and relation classes which reduces the RBI footprint for an individual
# model. However, in Rails 5 query methods that come from scopes or enums
# get overridden in hidden-definitions so we need to explicitly define them
# on the model and relation classes.
if builtin_query_method
relation_module_rbi = root.create_module(self.model_query_methods_returning_relation_module_name)
relation_module_rbi.create_method(
method_name,
parameters: parameters,
return_type: custom_return_value || self.model_relation_class_name,
)
assoc_relation_module_rbi = root.create_module(self.model_query_methods_returning_assoc_relation_module_name)
assoc_relation_module_rbi.create_method(
method_name,
parameters: parameters,
return_type: custom_return_value || assoc_return_value,
)
else
# force generating these methods because sorbet's hidden-definitions generate & override them
model_class_rbi = root.create_class(self.model_class_name)
model_class_rbi.create_method(
method_name,
parameters: parameters,
return_type: custom_return_value || self.model_relation_class_name,
class_method: true,
)
model_relation_rbi = root.create_class(self.model_relation_class_name)
model_relation_rbi.create_method(
method_name,
parameters: parameters,
return_type: custom_return_value || self.model_relation_class_name,
)
model_assoc_relation_rbi = root.create_class(self.model_assoc_relation_class_name)
model_assoc_relation_rbi.create_method(
method_name,
parameters: parameters,
return_type: custom_return_value || assoc_return_value,
)
collection_proxy_rbi = root.create_class(self.model_assoc_proxy_class_name)
collection_proxy_rbi.create_method(
method_name,
parameters: parameters,
return_type: custom_return_value || assoc_return_value,
)
end
end
end