Skip to content

Commit d365400

Browse files
committed
Merge pull request #1110 from Shopify/uk-fix-rails-main-builds
Fix failures against Rails `main`
1 parent 11d4fd4 commit d365400

File tree

5 files changed

+105
-13
lines changed

5 files changed

+105
-13
lines changed

lib/tapioca/dsl/compilers/active_record_fixtures.rb

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@ class ActiveRecordFixtures < Compiler
4242

4343
sig { override.void }
4444
def decorate
45-
method_names = fixture_loader.ancestors # get all ancestors from class that includes AR fixtures
46-
.drop(1) # drop the anonymous class itself from the array
47-
.reject(&:name) # only collect anonymous ancestors because fixture methods are always on an anonymous module
48-
.map! do |mod|
49-
[mod.private_instance_methods(false), mod.instance_methods(false)]
50-
end
51-
.flatten # merge methods into a single list
45+
method_names = if fixture_loader.respond_to?(:fixture_sets)
46+
method_names_from_lazy_fixture_loader
47+
else
48+
method_names_from_eager_fixture_loader
49+
end
50+
5251
return if method_names.empty?
5352

5453
root.create_path(constant) do |mod|
@@ -69,11 +68,27 @@ def self.gather_constants
6968

7069
sig { returns(Class) }
7170
def fixture_loader
72-
Class.new do
71+
@fixture_loader ||= T.let(Class.new do
7372
T.unsafe(self).include(ActiveRecord::TestFixtures)
7473
T.unsafe(self).fixture_path = Rails.root.join("test", "fixtures")
7574
T.unsafe(self).fixtures(:all)
76-
end
75+
end, T.nilable(Class))
76+
end
77+
78+
sig { returns(T::Array[String]) }
79+
def method_names_from_lazy_fixture_loader
80+
T.unsafe(fixture_loader).fixture_sets.keys
81+
end
82+
83+
sig { returns(T::Array[Symbol]) }
84+
def method_names_from_eager_fixture_loader
85+
fixture_loader.ancestors # get all ancestors from class that includes AR fixtures
86+
.drop(1) # drop the anonymous class itself from the array
87+
.reject(&:name) # only collect anonymous ancestors because fixture methods are always on an anonymous module
88+
.map! do |mod|
89+
[mod.private_instance_methods(false), mod.instance_methods(false)]
90+
end
91+
.flatten # merge methods into a single list
7792
end
7893

7994
sig { params(mod: RBI::Scope, name: String).void }

lib/tapioca/helpers/test/template.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ def ruby_version(selector)
1919
::Gem::Requirement.new(selector).satisfied_by?(::Gem::Version.new(RUBY_VERSION))
2020
end
2121

22+
sig { params(selector: String).returns(T::Boolean) }
23+
def rails_version(selector)
24+
::Gem::Requirement.new(selector).satisfied_by?(ActiveSupport.gem_version)
25+
end
26+
2227
sig { params(src: String).returns(String) }
2328
def template(src)
2429
erb = if ERB_SUPPORTS_KVARGS

spec/tapioca/dsl/compilers/active_model_secure_password_spec.rb

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class User
5757
end
5858
RUBY
5959

60-
expected = <<~RBI
60+
expected = template(<<~RBI)
6161
# typed: strong
6262
6363
class User
@@ -73,8 +73,22 @@ def password; end
7373
sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
7474
def password=(unencrypted_password); end
7575
76+
<% if rails_version(">= 7.1.alpha") %>
77+
sig { returns(T.untyped) }
78+
def password_challenge; end
79+
80+
sig { params(_arg0: T.untyped).returns(T.untyped) }
81+
def password_challenge=(_arg0); end
82+
83+
sig { returns(T.untyped) }
84+
def password_confirmation; end
85+
86+
sig { params(_arg0: T.untyped).returns(T.untyped) }
87+
def password_confirmation=(_arg0); end
88+
<% else %>
7689
sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
7790
def password_confirmation=(unencrypted_password); end
91+
<% end %>
7892
end
7993
RBI
8094

@@ -90,7 +104,7 @@ class User
90104
end
91105
RUBY
92106

93-
expected = <<~RBI
107+
expected = template(<<~RBI)
94108
# typed: strong
95109
96110
class User
@@ -103,8 +117,22 @@ def token; end
103117
sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
104118
def token=(unencrypted_password); end
105119
120+
<% if rails_version(">= 7.1.alpha") %>
121+
sig { returns(T.untyped) }
122+
def token_challenge; end
123+
124+
sig { params(_arg0: T.untyped).returns(T.untyped) }
125+
def token_challenge=(_arg0); end
126+
127+
sig { returns(T.untyped) }
128+
def token_confirmation; end
129+
130+
sig { params(_arg0: T.untyped).returns(T.untyped) }
131+
def token_confirmation=(_arg0); end
132+
<% else %>
106133
sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
107134
def token_confirmation=(unencrypted_password); end
135+
<% end %>
108136
end
109137
RBI
110138

@@ -121,7 +149,7 @@ class User
121149
end
122150
RUBY
123151

124-
expected = <<~RBI
152+
expected = template(<<~RBI)
125153
# typed: strong
126154
127155
class User
@@ -140,17 +168,45 @@ def password; end
140168
sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
141169
def password=(unencrypted_password); end
142170
171+
<% if rails_version(">= 7.1.alpha") %>
172+
sig { returns(T.untyped) }
173+
def password_challenge; end
174+
175+
sig { params(_arg0: T.untyped).returns(T.untyped) }
176+
def password_challenge=(_arg0); end
177+
178+
sig { returns(T.untyped) }
179+
def password_confirmation; end
180+
181+
sig { params(_arg0: T.untyped).returns(T.untyped) }
182+
def password_confirmation=(_arg0); end
183+
<% else %>
143184
sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
144185
def password_confirmation=(unencrypted_password); end
186+
<% end %>
145187
146188
sig { returns(T.untyped) }
147189
def token; end
148190
149191
sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
150192
def token=(unencrypted_password); end
151193
194+
<% if rails_version(">= 7.1.alpha") %>
195+
sig { returns(T.untyped) }
196+
def token_challenge; end
197+
198+
sig { params(_arg0: T.untyped).returns(T.untyped) }
199+
def token_challenge=(_arg0); end
200+
201+
sig { returns(T.untyped) }
202+
def token_confirmation; end
203+
204+
sig { params(_arg0: T.untyped).returns(T.untyped) }
205+
def token_confirmation=(_arg0); end
206+
<% else %>
152207
sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
153208
def token_confirmation=(unencrypted_password); end
209+
<% end %>
154210
end
155211
RBI
156212

spec/tapioca/dsl/compilers/active_record_associations_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,9 @@ def shop=(value); end
14471447
class Dummy < Rails::Application
14481448
config.eager_load = true
14491449
config.active_storage.service = :local
1450-
config.active_record.legacy_connection_handling = false
1450+
if ActiveRecord::Base.respond_to?(:legacy_connection_handling=)
1451+
config.active_record.legacy_connection_handling = false
1452+
end
14511453
config.active_storage.service_configurations = {
14521454
local: {
14531455
service: "Disk",

spec/tapioca/dsl/compilers/active_record_relations_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,16 @@ def upsert_all(attributes, returning: nil, unique_by: nil); end
349349
350350
sig { params(args: T.untyped, blk: T.untyped).returns(PrivateAssociationRelationWhereChain) }
351351
def where(*args, &blk); end
352+
<% if rails_version(">= 7.1.alpha") %>
353+
354+
sig { params(args: T.untyped, blk: T.untyped).returns(PrivateAssociationRelation) }
355+
def with(*args, &blk); end
356+
<% end %>
357+
<% if rails_version(">= 7.0") %>
352358
353359
sig { params(args: T.untyped, blk: T.untyped).returns(PrivateAssociationRelation) }
354360
def without(*args, &blk); end
361+
<% end %>
355362
end
356363
357364
module GeneratedRelationMethods
@@ -477,9 +484,16 @@ def unscope(*args, &blk); end
477484
478485
sig { params(args: T.untyped, blk: T.untyped).returns(PrivateRelationWhereChain) }
479486
def where(*args, &blk); end
487+
<% if rails_version(">= 7.1.alpha") %>
488+
489+
sig { params(args: T.untyped, blk: T.untyped).returns(PrivateRelation) }
490+
def with(*args, &blk); end
491+
<% end %>
492+
<% if rails_version(">= 7.0") %>
480493
481494
sig { params(args: T.untyped, blk: T.untyped).returns(PrivateRelation) }
482495
def without(*args, &blk); end
496+
<% end %>
483497
end
484498
485499
class PrivateAssociationRelation < ::ActiveRecord::AssociationRelation

0 commit comments

Comments
 (0)