@@ -17,9 +17,9 @@ class Generator
17
17
def initialize ( klass , dependencies :)
18
18
@klass = klass
19
19
@dependencies = dependencies
20
- @klass_name = Util . module_name ( klass )
20
+ @klass_name = Util . module_name ( klass , abs : false )
21
21
22
- namespaces = klass_name . split ( '::' ) . tap { |names | names . pop }
22
+ namespaces = klass_name ( abs : false ) . split ( '::' ) . tap { |names | names . pop }
23
23
@dependencies << namespaces . join ( '::' ) unless namespaces . empty?
24
24
end
25
25
@@ -30,7 +30,7 @@ def generate
30
30
private def klass_decl
31
31
<<~RBS
32
32
#{ header }
33
- extend _ActiveRecord_Relation_ClassMethods[#{ klass_name } , #{ relation_class_name } , #{ pk_type } ]
33
+ extend :: _ActiveRecord_Relation_ClassMethods[#{ klass_name } , #{ relation_class_name } , #{ pk_type } ]
34
34
35
35
#{ columns }
36
36
#{ associations }
@@ -62,7 +62,7 @@ def generate
62
62
63
63
private def generated_relation_methods_decl
64
64
<<~RBS
65
- module GeneratedRelationMethods
65
+ module #{ generated_relation_methods_name ( abs : false ) }
66
66
#{ enum_scope_methods ( singleton : false ) }
67
67
#{ scopes ( singleton : false ) }
68
68
#{ delegated_type_scope ( singleton : false ) }
@@ -72,33 +72,33 @@ module GeneratedRelationMethods
72
72
73
73
private def relation_decl
74
74
<<~RBS
75
- class #{ relation_class_name } < ::ActiveRecord::Relation
76
- include GeneratedRelationMethods
77
- include _ActiveRecord_Relation[#{ klass_name } , #{ pk_type } ]
78
- include Enumerable[#{ klass_name } ]
75
+ class #{ relation_class_name ( abs : false ) } < ::ActiveRecord::Relation
76
+ include #{ generated_relation_methods_name }
77
+ include :: _ActiveRecord_Relation[#{ klass_name } , #{ pk_type } ]
78
+ include :: Enumerable[#{ klass_name } ]
79
79
end
80
80
RBS
81
81
end
82
82
83
83
private def collection_proxy_decl
84
84
<<~RBS
85
85
class ActiveRecord_Associations_CollectionProxy < ::ActiveRecord::Associations::CollectionProxy
86
- include GeneratedRelationMethods
87
- include _ActiveRecord_Relation[#{ klass_name } , #{ pk_type } ]
86
+ include #{ generated_relation_methods_name }
87
+ include :: _ActiveRecord_Relation[#{ klass_name } , #{ pk_type } ]
88
88
end
89
89
RBS
90
90
end
91
91
92
92
private def header
93
93
namespace = +''
94
- klass_name . split ( '::' ) . map do |mod_name |
94
+ klass_name ( abs : false ) . split ( '::' ) . map do |mod_name |
95
95
namespace += "::#{ mod_name } "
96
96
mod_object = Object . const_get ( namespace )
97
97
case mod_object
98
98
when Class
99
99
# @type var superclass: Class
100
100
superclass = _ = mod_object . superclass
101
- superclass_name = Util . module_name ( superclass )
101
+ superclass_name = Util . module_name ( superclass , abs : false )
102
102
@dependencies << superclass_name
103
103
104
104
"class #{ mod_name } < ::#{ superclass_name } "
@@ -111,7 +111,7 @@ class ActiveRecord_Associations_CollectionProxy < ::ActiveRecord::Associations::
111
111
end
112
112
113
113
private def footer
114
- "end\n " * klass_name . split ( '::' ) . size
114
+ "end\n " * klass_name ( abs : false ) . split ( '::' ) . size
115
115
end
116
116
117
117
private def associations
@@ -133,9 +133,9 @@ class ActiveRecord_Associations_CollectionProxy < ::ActiveRecord::Associations::
133
133
134
134
<<~RUBY . chomp
135
135
def #{ a . name } : () -> #{ collection_type }
136
- def #{ a . name } =: (#{ collection_type } | Array[#{ type } ]) -> (#{ collection_type } | Array[#{ type } ])
137
- def #{ singular_name } _ids: () -> Array[Integer]
138
- def #{ singular_name } _ids=: (Array[Integer]) -> Array[Integer]
136
+ def #{ a . name } =: (#{ collection_type } | :: Array[#{ type } ]) -> (#{ collection_type } | :: Array[#{ type } ])
137
+ def #{ singular_name } _ids: () -> :: Array[:: Integer]
138
+ def #{ singular_name } _ids=: (:: Array[:: Integer]) -> :: Array[:: Integer]
139
139
RUBY
140
140
end . join ( "\n " )
141
141
end
@@ -190,17 +190,17 @@ def reload_#{a.name}: () -> #{type_optional}
190
190
case reflection . macro
191
191
when :has_one_attached
192
192
<<~EOS
193
- def #{ name } : () -> ActiveStorage::Attached::One
194
- def #{ name } =: (ActionDispatch::Http::UploadedFile) -> ActionDispatch::Http::UploadedFile
195
- | (Rack::Test::UploadedFile) -> Rack::Test::UploadedFile
196
- | (ActiveStorage::Blob) -> ActiveStorage::Blob
197
- | (String) -> String
198
- | ({ io: IO, filename: String, content_type: String? }) -> { io: IO, filename: String, content_type: String? }
193
+ def #{ name } : () -> :: ActiveStorage::Attached::One
194
+ def #{ name } =: (:: ActionDispatch::Http::UploadedFile) -> :: ActionDispatch::Http::UploadedFile
195
+ | (:: Rack::Test::UploadedFile) -> :: Rack::Test::UploadedFile
196
+ | (:: ActiveStorage::Blob) -> :: ActiveStorage::Blob
197
+ | (:: String) -> :: String
198
+ | ({ io: :: IO, filename: :: String, content_type: :: String? }) -> { io: :: IO, filename: :: String, content_type: :: String? }
199
199
| (nil) -> nil
200
200
EOS
201
201
when :has_many_attached
202
202
<<~EOS
203
- def #{ name } : () -> ActiveStorage::Attached::Many
203
+ def #{ name } : () -> :: ActiveStorage::Attached::Many
204
204
def #{ name } =: (untyped) -> untyped
205
205
EOS
206
206
else
@@ -460,7 +460,7 @@ def authenticate_#{attribute}: (String) -> (#{klass_name} | false)
460
460
private def parse_model_file
461
461
return @parse_model_file if defined? ( @parse_model_file )
462
462
463
- path = Rails . root . join ( 'app/models/' , klass_name . underscore + '.rb' )
463
+ path = Rails . root . join ( 'app/models/' , klass_name ( abs : false ) . underscore + '.rb' )
464
464
return @parse_model_file = nil unless path . exist?
465
465
return [ ] unless path . exist?
466
466
@@ -479,15 +479,24 @@ def authenticate_#{attribute}: (String) -> (#{klass_name} | false)
479
479
end
480
480
end
481
481
482
- private def relation_class_name
483
- "ActiveRecord_Relation"
482
+ private def relation_class_name ( abs : true )
483
+ abs ? " #{ klass_name } ::ActiveRecord_Relation" : "ActiveRecord_Relation"
484
484
end
485
485
486
+ private def klass_name ( abs : true )
487
+ abs ? "::#{ @klass_name } " : @klass_name
488
+ end
489
+
490
+ private def generated_relation_methods_name ( abs : true )
491
+ abs ? "#{ klass_name } ::GeneratedRelationMethods" : "GeneratedRelationMethods"
492
+ end
493
+
494
+
486
495
private def columns
487
496
mod_sig = +"module GeneratedAttributeMethods\n "
488
497
mod_sig << klass . columns . map do |col |
489
498
class_name = if enum_definitions . any? { |hash | hash . key? ( col . name ) || hash . key? ( col . name . to_sym ) }
490
- 'String'
499
+ ':: String'
491
500
else
492
501
sql_type_to_class ( col . type )
493
502
end
@@ -502,12 +511,12 @@ def #{col.name}_change: () -> [#{class_name_opt}, #{class_name_opt}]
502
511
def #{ col . name } _will_change!: () -> void
503
512
def #{ col . name } _was: () -> #{ class_name_opt }
504
513
def #{ col . name } _previously_changed?: () -> bool
505
- def #{ col . name } _previous_change: () -> Array[#{ class_name_opt } ]?
514
+ def #{ col . name } _previous_change: () -> :: Array[#{ class_name_opt } ]?
506
515
def #{ col . name } _previously_was: () -> #{ class_name_opt }
507
516
def #{ col . name } _before_last_save: () -> #{ class_name_opt }
508
- def #{ col . name } _change_to_be_saved: () -> Array[#{ class_name_opt } ]?
517
+ def #{ col . name } _change_to_be_saved: () -> :: Array[#{ class_name_opt } ]?
509
518
def #{ col . name } _in_database: () -> #{ class_name_opt }
510
- def saved_change_to_#{ col . name } : () -> Array[#{ class_name_opt } ]?
519
+ def saved_change_to_#{ col . name } : () -> :: Array[#{ class_name_opt } ]?
511
520
def saved_change_to_#{ col . name } ?: () -> bool
512
521
def will_save_change_to_#{ col . name } ?: () -> bool
513
522
def restore_#{ col . name } !: () -> void
@@ -528,33 +537,33 @@ def clear_#{col.name}_change: () -> void
528
537
private def sql_type_to_class ( t )
529
538
case t
530
539
when :integer
531
- 'Integer'
540
+ ':: Integer'
532
541
when :float
533
- 'Float'
542
+ ':: Float'
534
543
when :decimal
535
- 'BigDecimal'
544
+ ':: BigDecimal'
536
545
when :string , :text , :citext , :uuid , :binary
537
- 'String'
546
+ ':: String'
538
547
when :datetime
539
- 'ActiveSupport::TimeWithZone'
548
+ ':: ActiveSupport::TimeWithZone'
540
549
when :boolean
541
550
"bool"
542
551
when :jsonb , :json
543
552
"untyped"
544
553
when :date
545
- 'Date'
554
+ ':: Date'
546
555
when :time
547
- 'Time'
556
+ ':: Time'
548
557
when :inet
549
- "IPAddr"
558
+ ":: IPAddr"
550
559
else
551
560
# Unknown column type, give up
552
561
'untyped'
553
562
end
554
563
end
555
564
556
565
private
557
- attr_reader :klass , :klass_name
566
+ attr_reader :klass
558
567
end
559
568
end
560
569
end
0 commit comments