Skip to content

Commit 5df059c

Browse files
committed
ActiveRecordRelations support for generic definitions in AR models
1 parent 21a9a6d commit 5df059c

File tree

3 files changed

+782
-1
lines changed

3 files changed

+782
-1
lines changed

lib/tapioca/dsl/compilers/active_record_relations.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,16 @@ def common_relation_methods_module
278278

279279
sig { returns(String) }
280280
def constant_name
281-
@constant_name ||= T.let(T.must(qualified_name_of(constant)), T.nilable(String))
281+
@constant_name ||= T.let(constant_name_of(constant), T.nilable(String))
282+
end
283+
284+
sig { params(constant: ConstantType).returns(String) }
285+
def constant_name_of(constant)
286+
if T::Generic === constant
287+
generic_name_of(constant)
288+
else
289+
T.must(qualified_name_of(constant))
290+
end
282291
end
283292

284293
sig { params(method_name: Symbol).returns(T::Boolean) }

lib/tapioca/helpers/rbi_helper.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,33 @@ def valid_parameter_name?(name)
138138
rescue SyntaxError
139139
false
140140
end
141+
142+
sig { params(constant: T.all(Module, T::Generic)).returns(String) }
143+
def generic_name_of(constant)
144+
type_name = T.must(qualified_name_of(constant))
145+
return type_name if type_name =~ /\[.*\]$/
146+
147+
type_variables = Runtime::GenericTypeRegistry.lookup_type_variables(constant)
148+
return type_name unless type_variables
149+
150+
type_variables = type_variables.reject(&:fixed?)
151+
return type_name if type_variables.empty?
152+
153+
type_variable_names = type_variables.map { "T.untyped" }.join(", ")
154+
155+
"#{type_name}[#{type_variable_names}]"
156+
end
157+
158+
sig { params(constant: Module).returns(T.nilable(String)) }
159+
def qualified_name_of(constant)
160+
name = name_of(constant)
161+
return if name.nil?
162+
163+
if name.start_with?("::")
164+
name
165+
else
166+
"::#{name}"
167+
end
168+
end
141169
end
142170
end

0 commit comments

Comments
 (0)