Skip to content
This repository was archived by the owner on Jul 5, 2023. It is now read-only.

Commit 1acc5a9

Browse files
connorsheaHarry Doan
authored and
Harry Doan
committed
Add 'new' method to models (#179)
* Add 'new' method to model classes. * Update expected files. * Split the factory methods into a separate plugin. * Updated expected test data. * Add signature for add_factory_method.
1 parent 8c5a014 commit 1acc5a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+410
-0
lines changed

lib/sorbet-rails/config.rb

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def initialize
4848
:active_record_attribute,
4949
:active_record_assoc,
5050
:active_record_finder_methods,
51+
:active_record_factory_methods,
5152
:custom_finder_methods,
5253
:enumerable_collections,
5354
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# typed: strict
2+
require ('sorbet-rails/model_plugins/base')
3+
class SorbetRails::ModelPlugins::ActiveRecordFactoryMethods < SorbetRails::ModelPlugins::Base
4+
# Create methods like `new`, `create`, `create!`, etc.
5+
sig { override.params(root: Parlour::RbiGenerator::Namespace).void }
6+
def generate(root)
7+
model_rbi = root.create_class(self.model_class_name)
8+
9+
factory_methods = ['create', 'create!', 'new']
10+
11+
factory_methods.each do |factory_method|
12+
add_factory_method(model_rbi, factory_method)
13+
end
14+
end
15+
16+
sig { params(model_rbi: Parlour::RbiGenerator::ClassNamespace, method_name: String).void }
17+
def add_factory_method(model_rbi, method_name)
18+
model_rbi.create_method(
19+
method_name,
20+
parameters: [
21+
Parlour::RbiGenerator::Parameter.new("attributes", type: "T.untyped", default: 'nil'),
22+
Parlour::RbiGenerator::Parameter.new("&block", type: "T.untyped")
23+
],
24+
return_type: self.model_class_name,
25+
class_method: true
26+
)
27+
end
28+
end

lib/sorbet-rails/model_plugins/plugins.rb

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require('sorbet-rails/model_plugins/active_record_attribute')
88
require('sorbet-rails/model_plugins/active_record_assoc')
99
require('sorbet-rails/model_plugins/active_record_finder_methods')
10+
require('sorbet-rails/model_plugins/active_record_factory_methods')
1011
require('sorbet-rails/model_plugins/custom_finder_methods')
1112
require('sorbet-rails/model_plugins/enumerable_collections')
1213
require('sorbet-rails/model_plugins/active_storage_methods')
@@ -54,6 +55,8 @@ def get_plugin_by_name(plugin_name)
5455
ActiveRecordAssoc
5556
when :active_record_finder_methods
5657
ActiveRecordFinderMethods
58+
when :active_record_factory_methods
59+
ActiveRecordFactoryMethods
5760
when :custom_finder_methods
5861
CustomFinderMethods
5962
when :enumerable_collections

spec/test_data/v4.2/expected_potion.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ class Potion < ApplicationRecord
160160

161161
sig { params(args: T.untyped).returns(T::Boolean) }
162162
def self.one?(*args); end
163+
164+
sig { params(attributes: T.untyped, block: T.untyped).returns(Potion) }
165+
def self.create(attributes = nil, &block); end
166+
167+
sig { params(attributes: T.untyped, block: T.untyped).returns(Potion) }
168+
def self.create!(attributes = nil, &block); end
169+
170+
sig { params(attributes: T.untyped, block: T.untyped).returns(Potion) }
171+
def self.new(attributes = nil, &block); end
163172
end
164173

165174
class Potion::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v4.2/expected_spell_book.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ class SpellBook < ApplicationRecord
247247

248248
sig { params(args: T.untyped).returns(T::Boolean) }
249249
def self.one?(*args); end
250+
251+
sig { params(attributes: T.untyped, block: T.untyped).returns(SpellBook) }
252+
def self.create(attributes = nil, &block); end
253+
254+
sig { params(attributes: T.untyped, block: T.untyped).returns(SpellBook) }
255+
def self.create!(attributes = nil, &block); end
256+
257+
sig { params(attributes: T.untyped, block: T.untyped).returns(SpellBook) }
258+
def self.new(attributes = nil, &block); end
250259
end
251260

252261
class SpellBook::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v4.2/expected_squib.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,15 @@ class Squib < Wizard
343343

344344
sig { params(args: T.untyped).returns(T::Boolean) }
345345
def self.one?(*args); end
346+
347+
sig { params(attributes: T.untyped, block: T.untyped).returns(Squib) }
348+
def self.create(attributes = nil, &block); end
349+
350+
sig { params(attributes: T.untyped, block: T.untyped).returns(Squib) }
351+
def self.create!(attributes = nil, &block); end
352+
353+
sig { params(attributes: T.untyped, block: T.untyped).returns(Squib) }
354+
def self.new(attributes = nil, &block); end
346355
end
347356

348357
class Squib::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v4.2/expected_wand.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,15 @@ class Wand < ApplicationRecord
338338
sig { params(args: T.untyped).returns(T::Boolean) }
339339
def self.one?(*args); end
340340

341+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wand) }
342+
def self.create(attributes = nil, &block); end
343+
344+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wand) }
345+
def self.create!(attributes = nil, &block); end
346+
347+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wand) }
348+
def self.new(attributes = nil, &block); end
349+
341350
sig { returns(T::Array[Wand]) }
342351
def self.mythicals; end
343352
end

spec/test_data/v4.2/expected_wizard.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,15 @@ class Wizard < ApplicationRecord
343343

344344
sig { params(args: T.untyped).returns(T::Boolean) }
345345
def self.one?(*args); end
346+
347+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
348+
def self.create(attributes = nil, &block); end
349+
350+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
351+
def self.create!(attributes = nil, &block); end
352+
353+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
354+
def self.new(attributes = nil, &block); end
346355
end
347356

348357
class Wizard::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v4.2/expected_wizard_wo_spellbook.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,15 @@ class Wizard < ApplicationRecord
343343

344344
sig { params(args: T.untyped).returns(T::Boolean) }
345345
def self.one?(*args); end
346+
347+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
348+
def self.create(attributes = nil, &block); end
349+
350+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
351+
def self.create!(attributes = nil, &block); end
352+
353+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
354+
def self.new(attributes = nil, &block); end
346355
end
347356

348357
class Wizard::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v5.0/expected_internal_metadata.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,15 @@ class ActiveRecord::InternalMetadata < ActiveRecord::Base
210210

211211
sig { params(args: T.untyped).returns(T::Boolean) }
212212
def self.one?(*args); end
213+
214+
sig { params(attributes: T.untyped, block: T.untyped).returns(ActiveRecord::InternalMetadata) }
215+
def self.create(attributes = nil, &block); end
216+
217+
sig { params(attributes: T.untyped, block: T.untyped).returns(ActiveRecord::InternalMetadata) }
218+
def self.create!(attributes = nil, &block); end
219+
220+
sig { params(attributes: T.untyped, block: T.untyped).returns(ActiveRecord::InternalMetadata) }
221+
def self.new(attributes = nil, &block); end
213222
end
214223

215224
class ActiveRecord::InternalMetadata::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v5.0/expected_potion.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ class Potion < ApplicationRecord
169169

170170
sig { params(args: T.untyped).returns(T::Boolean) }
171171
def self.one?(*args); end
172+
173+
sig { params(attributes: T.untyped, block: T.untyped).returns(Potion) }
174+
def self.create(attributes = nil, &block); end
175+
176+
sig { params(attributes: T.untyped, block: T.untyped).returns(Potion) }
177+
def self.create!(attributes = nil, &block); end
178+
179+
sig { params(attributes: T.untyped, block: T.untyped).returns(Potion) }
180+
def self.new(attributes = nil, &block); end
172181
end
173182

174183
class Potion::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v5.0/expected_schema_migration.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ class ActiveRecord::SchemaMigration < ActiveRecord::Base
183183

184184
sig { params(args: T.untyped).returns(T::Boolean) }
185185
def self.one?(*args); end
186+
187+
sig { params(attributes: T.untyped, block: T.untyped).returns(ActiveRecord::SchemaMigration) }
188+
def self.create(attributes = nil, &block); end
189+
190+
sig { params(attributes: T.untyped, block: T.untyped).returns(ActiveRecord::SchemaMigration) }
191+
def self.create!(attributes = nil, &block); end
192+
193+
sig { params(attributes: T.untyped, block: T.untyped).returns(ActiveRecord::SchemaMigration) }
194+
def self.new(attributes = nil, &block); end
186195
end
187196

188197
class ActiveRecord::SchemaMigration::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v5.0/expected_spell_book.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,15 @@ class SpellBook < ApplicationRecord
256256

257257
sig { params(args: T.untyped).returns(T::Boolean) }
258258
def self.one?(*args); end
259+
260+
sig { params(attributes: T.untyped, block: T.untyped).returns(SpellBook) }
261+
def self.create(attributes = nil, &block); end
262+
263+
sig { params(attributes: T.untyped, block: T.untyped).returns(SpellBook) }
264+
def self.create!(attributes = nil, &block); end
265+
266+
sig { params(attributes: T.untyped, block: T.untyped).returns(SpellBook) }
267+
def self.new(attributes = nil, &block); end
259268
end
260269

261270
class SpellBook::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v5.0/expected_squib.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,15 @@ class Squib < Wizard
487487

488488
sig { params(args: T.untyped).returns(T::Boolean) }
489489
def self.one?(*args); end
490+
491+
sig { params(attributes: T.untyped, block: T.untyped).returns(Squib) }
492+
def self.create(attributes = nil, &block); end
493+
494+
sig { params(attributes: T.untyped, block: T.untyped).returns(Squib) }
495+
def self.create!(attributes = nil, &block); end
496+
497+
sig { params(attributes: T.untyped, block: T.untyped).returns(Squib) }
498+
def self.new(attributes = nil, &block); end
490499
end
491500

492501
class Squib::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v5.0/expected_wand.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,15 @@ class Wand < ApplicationRecord
347347
sig { params(args: T.untyped).returns(T::Boolean) }
348348
def self.one?(*args); end
349349

350+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wand) }
351+
def self.create(attributes = nil, &block); end
352+
353+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wand) }
354+
def self.create!(attributes = nil, &block); end
355+
356+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wand) }
357+
def self.new(attributes = nil, &block); end
358+
350359
sig { returns(T::Array[Wand]) }
351360
def self.mythicals; end
352361
end

spec/test_data/v5.0/expected_wizard.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,15 @@ class Wizard < ApplicationRecord
487487

488488
sig { params(args: T.untyped).returns(T::Boolean) }
489489
def self.one?(*args); end
490+
491+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
492+
def self.create(attributes = nil, &block); end
493+
494+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
495+
def self.create!(attributes = nil, &block); end
496+
497+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
498+
def self.new(attributes = nil, &block); end
490499
end
491500

492501
class Wizard::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v5.0/expected_wizard_wo_spellbook.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,15 @@ class Wizard < ApplicationRecord
487487

488488
sig { params(args: T.untyped).returns(T::Boolean) }
489489
def self.one?(*args); end
490+
491+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
492+
def self.create(attributes = nil, &block); end
493+
494+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
495+
def self.create!(attributes = nil, &block); end
496+
497+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
498+
def self.new(attributes = nil, &block); end
490499
end
491500

492501
class Wizard::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v5.1/expected_internal_metadata.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ class ActiveRecord::InternalMetadata < ActiveRecord::Base
216216

217217
sig { params(args: T.untyped).returns(T::Boolean) }
218218
def self.one?(*args); end
219+
220+
sig { params(attributes: T.untyped, block: T.untyped).returns(ActiveRecord::InternalMetadata) }
221+
def self.create(attributes = nil, &block); end
222+
223+
sig { params(attributes: T.untyped, block: T.untyped).returns(ActiveRecord::InternalMetadata) }
224+
def self.create!(attributes = nil, &block); end
225+
226+
sig { params(attributes: T.untyped, block: T.untyped).returns(ActiveRecord::InternalMetadata) }
227+
def self.new(attributes = nil, &block); end
219228
end
220229

221230
class ActiveRecord::InternalMetadata::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v5.1/expected_potion.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ class Potion < ApplicationRecord
175175

176176
sig { params(args: T.untyped).returns(T::Boolean) }
177177
def self.one?(*args); end
178+
179+
sig { params(attributes: T.untyped, block: T.untyped).returns(Potion) }
180+
def self.create(attributes = nil, &block); end
181+
182+
sig { params(attributes: T.untyped, block: T.untyped).returns(Potion) }
183+
def self.create!(attributes = nil, &block); end
184+
185+
sig { params(attributes: T.untyped, block: T.untyped).returns(Potion) }
186+
def self.new(attributes = nil, &block); end
178187
end
179188

180189
class Potion::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v5.1/expected_schema_migration.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ class ActiveRecord::SchemaMigration < ActiveRecord::Base
189189

190190
sig { params(args: T.untyped).returns(T::Boolean) }
191191
def self.one?(*args); end
192+
193+
sig { params(attributes: T.untyped, block: T.untyped).returns(ActiveRecord::SchemaMigration) }
194+
def self.create(attributes = nil, &block); end
195+
196+
sig { params(attributes: T.untyped, block: T.untyped).returns(ActiveRecord::SchemaMigration) }
197+
def self.create!(attributes = nil, &block); end
198+
199+
sig { params(attributes: T.untyped, block: T.untyped).returns(ActiveRecord::SchemaMigration) }
200+
def self.new(attributes = nil, &block); end
192201
end
193202

194203
class ActiveRecord::SchemaMigration::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v5.1/expected_spell_book.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,15 @@ class SpellBook < ApplicationRecord
262262

263263
sig { params(args: T.untyped).returns(T::Boolean) }
264264
def self.one?(*args); end
265+
266+
sig { params(attributes: T.untyped, block: T.untyped).returns(SpellBook) }
267+
def self.create(attributes = nil, &block); end
268+
269+
sig { params(attributes: T.untyped, block: T.untyped).returns(SpellBook) }
270+
def self.create!(attributes = nil, &block); end
271+
272+
sig { params(attributes: T.untyped, block: T.untyped).returns(SpellBook) }
273+
def self.new(attributes = nil, &block); end
265274
end
266275

267276
class SpellBook::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v5.1/expected_squib.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,15 @@ class Squib < Wizard
493493

494494
sig { params(args: T.untyped).returns(T::Boolean) }
495495
def self.one?(*args); end
496+
497+
sig { params(attributes: T.untyped, block: T.untyped).returns(Squib) }
498+
def self.create(attributes = nil, &block); end
499+
500+
sig { params(attributes: T.untyped, block: T.untyped).returns(Squib) }
501+
def self.create!(attributes = nil, &block); end
502+
503+
sig { params(attributes: T.untyped, block: T.untyped).returns(Squib) }
504+
def self.new(attributes = nil, &block); end
496505
end
497506

498507
class Squib::ActiveRecord_Relation < ActiveRecord::Relation

spec/test_data/v5.1/expected_wand.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,15 @@ class Wand < ApplicationRecord
353353
sig { params(args: T.untyped).returns(T::Boolean) }
354354
def self.one?(*args); end
355355

356+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wand) }
357+
def self.create(attributes = nil, &block); end
358+
359+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wand) }
360+
def self.create!(attributes = nil, &block); end
361+
362+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wand) }
363+
def self.new(attributes = nil, &block); end
364+
356365
sig { returns(T::Array[Wand]) }
357366
def self.mythicals; end
358367
end

spec/test_data/v5.1/expected_wizard.rbi

+9
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,15 @@ class Wizard < ApplicationRecord
493493

494494
sig { params(args: T.untyped).returns(T::Boolean) }
495495
def self.one?(*args); end
496+
497+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
498+
def self.create(attributes = nil, &block); end
499+
500+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
501+
def self.create!(attributes = nil, &block); end
502+
503+
sig { params(attributes: T.untyped, block: T.untyped).returns(Wizard) }
504+
def self.new(attributes = nil, &block); end
496505
end
497506

498507
class Wizard::ActiveRecord_Relation < ActiveRecord::Relation

0 commit comments

Comments
 (0)