This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
rails-template.rb
416 lines (354 loc) · 10.4 KB
/
rails-template.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# Generate a Rails app.
# Use the rake tasks in this repository to generate the Rails apps.
def add_gems
gem 'sorbet-rails', path: '../../../.'
sorbet_version = ENV["SORBET_VERSION"]
if sorbet_version
# mostly used to test against a stable version of Sorbet in Travis.
gem 'sorbet', sorbet_version
gem 'sorbet-runtime', sorbet_version
else
# prefer to test against latest version because sorbet is updated frequently
gem 'sorbet'
gem 'sorbet-runtime'
end
end
def add_routes
route "get 'test/index' => 'test#index'"
end
def add_environment
if ENV["RAILS_VERSION"] == "5.0"
# by default, Rails 5.0 treats only `datetime` as a time zone aware type;
# this config option brings it in line with all versions up to at least v6.0
# and makes our time-related attribute fixtures simpler to maintain
environment 'config.active_record.time_zone_aware_types = [:datetime, :time]'
end
end
def create_initializers
initializer "sorbet_rails.rb", <<~RUBY
# typed: strict
require(Rails.root.join('lib/mythical_rbi_plugin'))
SorbetRails::ModelRbiFormatter.register_plugin(MythicalRbiPlugin)
RUBY
end
def create_lib
lib "mythical_rbi_plugin.rb", <<~'RUBY'
# typed: true
class MythicalRbiPlugin < SorbetRails::ModelPlugins::Base
def generate(root)
return unless @model_class.include?(Mythical)
model_class_rbi = root.create_class(self.model_class_name)
# ActiveSupport::Concern class method will be inserted to the class
# directly. We need to also put the sig in the model class rbi directly
model_class_rbi.create_method(
'mythicals',
class_method: true,
return_type: "T::Array[#{@model_class.name}]",
)
end
end
RUBY
end
def create_helpers
file "app/helpers/foo_helper.rb", <<~RUBY
module FooHelper
end
RUBY
file "app/helpers/bar_helper.rb", <<~RUBY
module BarHelper
end
RUBY
file "app/helpers/baz_helper.rb", <<~RUBY
module BazHelper
end
RUBY
end
def create_models
file "app/models/spell_book.rb", <<~RUBY
class SpellBook < ApplicationRecord
validates :name, length: { minimum: 5 }, presence: true
# simulate when belongs_to is optional by default, but it is enforced at the DB level
belongs_to :wizard, optional: true
enum book_type: {
unclassified: 0,
biology: 1,
dark_art: 999,
}
end
RUBY
file "app/models/potion.rb", <<~RUBY
# an abstract class that has no table
class Potion < ApplicationRecord
self.abstract_class = true
belongs_to :wizard, required: false
end
RUBY
file "app/models/wand.rb", <<~RUBY
class Wand < ApplicationRecord
include Mythical
self.skip_time_zone_conversion_for_attributes = [:broken_at]
enum core_type: {
phoenix_feather: 0,
dragon_heartstring: 1,
unicorn_tail_hair: 2,
basilisk_horn: 3,
}
belongs_to :wizard, required: true
def wood_type
'Type ' + super
end
end
RUBY
# A nasty hack to add has_one_attached and has_many_attached to the models/wizard.rb file.
attachments = nil
if ['5.2', '6.0'].include?(ENV["RAILS_VERSION"])
attachments = "has_one_attached :school_photo\n has_many_attached :hats"
end
file "app/models/wizard.rb", <<~RUBY
class Wizard < ApplicationRecord
validates :name, length: { minimum: 5 }, presence: true
typed_enum house: {
Gryffindor: 0,
Hufflepuff: 1,
Ravenclaw: 2,
Slytherin: 3,
}
enum professor: {
"Severus Snape": 0,
"Minerva McGonagall": 1,
"Pomona Sprout": 2,
"Filius Flitwick": 3,
"Hagrid": 4,
}
typed_enum broom: {
nimbus: 'nimbus',
firebolt: 'firebolt',
}, _prefix: true
typed_enum quidditch_position: {
keeper: 0,
seeker: 1,
beater: 2,
chaser: 3,
}, _prefix: :quidditch
enum hair_color: {
brown: 0,
black: 1,
blonde: 2,
}, _suffix: :hair
enum eye_color: {
brown: 0,
green: 1,
blue: 2,
}, _prefix: :color, _suffix: :eyes
has_one :wand
has_many :spell_books
# simulate when belongs_to is optional by default
belongs_to :school, optional: true
scope :recent, -> { where('created_at > ?', 1.month.ago) }
#{attachments}
end
RUBY
file "app/models/concerns/mythical.rb", <<~RUBY
require 'active_support/concern'
module Mythical
extend ActiveSupport::Concern
class_methods do
def mythicals
all.to_a # yeah!
end
end
end
RUBY
file "app/models/squib.rb", <<~RUBY
class Squib < Wizard
def is_magical
false
end
end
RUBY
file "app/models/robe.rb", <<~RUBY
class Robe < ApplicationRecord
belongs_to :wizard, required: false
end
RUBY
file "app/models/school.rb", <<~RUBY
class School < ApplicationRecord
end
RUBY
end
def create_migrations
migration_superclass = "ActiveRecord::Migration[#{ENV['RAILS_VERSION']}]"
file "db/migrate/20190620000001_create_wizards.rb", <<~RUBY
class CreateWizards < #{migration_superclass}
def change
create_table :wizards do |t|
t.string :name
t.integer :house
t.integer :professor
t.string :parent_email
t.text :notes
t.timestamps null: false
end
end
end
RUBY
file "db/migrate/20190620000002_create_wands.rb", <<~RUBY
class CreateWands < #{migration_superclass}
def change
create_table :wands do |t|
t.references :wizard, unique: true, null: false
t.string :wood_type
t.integer :core_type
t.timestamps
end
end
end
RUBY
file "db/migrate/20190620000003_create_spell_books.rb", <<~RUBY
class CreateSpellBooks < #{migration_superclass}
def change
create_table :spell_books do |t|
t.string :name
t.references :wizard, null: false
t.integer :book_type, null: false, default: 0
end
end
end
RUBY
file "db/migrate/20190620000004_add_more_column_types_to_wands.rb", <<~RUBY
class AddMoreColumnTypesToWands < #{migration_superclass}
def change
add_column :wands, :flexibility, :float, null: false, default: 0.5
add_column :wands, :hardness, :decimal, null: false, precision: 10, scale: 10, default: 5
add_column :wands, :reflectance, :decimal, null: false, precision: 10, scale: 0, default: 0.5
add_column :wands, :broken, :boolean, null: false, default: false
add_column :wands, :broken_at, :datetime, null: true
add_column :wands, :chosen_at_date, :date
add_column :wands, :chosen_at_time, :time
# JSON column type is only supported on 5.2 or higher
unless ['5.0', '5.1'].include?(ENV['RAILS_VERSION'])
add_column :wands, :spell_history, :json
add_column :wands, :maker_info, :json, null: false, default: '{}'
end
end
end
RUBY
file "db/migrate/20190620000005_add_broom_to_wizard.rb", <<~RUBY
class AddBroomToWizard < #{migration_superclass}
def change
add_column :wizards, :broom, :string
end
end
RUBY
file "db/migrate/20190620000006_add_more_enums_to_wizard.rb", <<~RUBY
class AddMoreEnumsToWizard < #{migration_superclass}
def change
add_column :wizards, :quidditch_position, :integer
add_column :wizards, :hair_color, :integer
add_column :wizards, :eye_color, :integer
add_column :wizards, :hair_length, :integer
end
end
RUBY
file "db/migrate/20190620000007_add_type_to_wizard.rb", <<~RUBY
class AddTypeToWizard < #{migration_superclass}
def change
add_column :wizards, :type, :string, null: false, default: 'Wizard'
end
end
RUBY
file "db/migrate/20190620000008_add_robe_to_wizard.rb", <<~RUBY
class AddRobeToWizard < #{migration_superclass}
def change
create_table :robes do |t|
t.references :wizard
end
end
end
RUBY
file "db/migrate/20190620000009_add_school.rb", <<~RUBY
class AddSchool < #{migration_superclass}
def change
create_table :schools do |t|
t.string :name
end
add_column :wizards, :school_id, :integer
add_foreign_key :wizards, :schools
end
end
RUBY
end
def create_mailers
file "app/mailers/hogwarts_acceptance_mailer.rb", <<~RUBY
class HogwartsAcceptanceMailer < ApplicationMailer
extend T::Sig
sig { params(student: Wizard).void }
def notify(student)
# TODO: mail acceptance letter to student
end
def notify_retry(student)
# TODO: send more owls!!
end
end
RUBY
file "app/mailers/daily_prophet_mailer.rb", <<~RUBY
class DailyProphetMailer < ApplicationMailer
extend T::Sig
sig { params(wizards: T::Array[Wizard], hotnews_only: T::Boolean).void }
def notify_subscribers(wizards:, hotnews_only:)
# TODO: mail the latest news to wizards!
end
end
RUBY
end
def add_sorbet_test_files
file "typed-override.yaml", <<~YAML
true:
- ./sorbet_test_cases.rb
YAML
copy_file "./sorbet_test_cases.rb", "sorbet_test_cases.rb"
end
# Get files relative to this template when copying.
def source_paths
[__dir__]
end
# Main setup
source_paths
if ['5.0'].include?(ENV["RAILS_VERSION"])
File.open('Gemfile', 'r+') do |f|
out = ""
f.each do |line|
# We remove sdoc and web-console because they misbehave.
# sqlite needs to be limited to 1.3.x or it won't work.
out << line.gsub("gem 'sqlite3'", "gem 'sqlite3', '~> 1.3.6'") unless line =~ /gem \'(sdoc|web-console)\'.*/
end
f.pos = 0
f.print out
f.truncate(f.pos)
end
end
add_gems
after_bundle do
say "Creating application..."
add_routes
add_environment
create_initializers
create_lib
create_helpers
create_models
create_migrations
create_mailers
add_sorbet_test_files
Bundler.with_clean_env do
run "bundle exec rake db:migrate"
end
if ENV["RUN_WITH_SORBET"] != 'false'
Bundler.with_clean_env do
run "SRB_YES=true bundle exec srb init"
run "bundle exec rake rails_rbi:all"
run "bundle exec srb rbi hidden-definitions"
run "bundle exec srb rbi todo"
end
end
say "Done!"
end