sorbet-rails 0.5.6 → 0.5.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +0 -2
- data/CONTRIBUTING.md +2 -4
- data/Gemfile +0 -4
- data/README.md +41 -7
- data/Rakefile +3 -14
- data/lib/bundled_rbi/parameters.rbi +4 -26
- data/lib/bundled_rbi/pluck_to_tstruct.rbi +22 -0
- data/lib/bundled_rbi/type_assert.rbi +27 -0
- data/lib/sorbet-rails/gem_plugins/elastic_search_plugin.rb +40 -0
- data/lib/sorbet-rails/gem_plugins/shrine_plugin.rb +76 -0
- data/lib/sorbet-rails/model_plugins/active_record_assoc.rb +2 -11
- data/lib/sorbet-rails/model_plugins/active_record_attribute.rb +3 -6
- data/lib/sorbet-rails/model_plugins/active_record_querying.rb +10 -2
- data/lib/sorbet-rails/model_plugins/plugins.rb +6 -0
- data/lib/sorbet-rails/rails_mixins/pluck_to_tstruct.rb +33 -0
- data/lib/sorbet-rails/railtie.rb +4 -0
- data/lib/sorbet-rails/sorbet_utils.rb +4 -1
- data/lib/sorbet-rails/tasks/rails_rbi.rake +16 -6
- data/sorbet-rails.gemspec +1 -1
- data/spec/bin/run_all_specs.sh +0 -1
- data/spec/bin/run_spec.sh +4 -6
- data/spec/generators/rails-template.rb +67 -124
- data/spec/generators/sorbet_test_cases.rb +26 -16
- data/spec/pluck_to_tstruct_spec.rb +79 -0
- data/spec/rails_helper.rb +0 -2
- data/spec/rake_rails_rbi_models_spec.rb +2 -0
- data/spec/sorbet_utils_spec.rb +37 -0
- data/spec/spec_helper.rb +1 -2
- data/spec/support/v5.0/Gemfile.lock +6 -6
- data/spec/support/v5.0/config/boot.rb +1 -1
- data/spec/support/v5.0/config/initializers/sorbet_rails.rb +1 -1
- data/spec/support/v5.0/db/migrate/20190620000004_add_more_column_types_to_wands.rb +1 -1
- data/spec/support/v5.0/sorbet_test_cases.rb +26 -16
- data/spec/support/v5.1/Gemfile.lock +6 -6
- data/spec/support/v5.1/config/boot.rb +1 -1
- data/spec/support/v5.1/config/initializers/sorbet_rails.rb +1 -1
- data/spec/support/v5.1/db/migrate/20190620000004_add_more_column_types_to_wands.rb +1 -1
- data/spec/support/v5.1/sorbet_test_cases.rb +26 -16
- data/spec/support/v5.2/Gemfile.lock +6 -6
- data/spec/support/v5.2/config/boot.rb +1 -1
- data/spec/support/v5.2/config/initializers/sorbet_rails.rb +1 -1
- data/spec/support/v5.2/db/migrate/20190620000004_add_more_column_types_to_wands.rb +1 -1
- data/spec/support/v5.2/sorbet_test_cases.rb +26 -16
- data/spec/support/v6.0/Gemfile.lock +6 -6
- data/spec/support/v6.0/config/boot.rb +1 -1
- data/spec/support/v6.0/config/initializers/sorbet_rails.rb +1 -1
- data/spec/support/v6.0/config/initializers/wrap_parameters.rb +1 -1
- data/spec/support/v6.0/db/migrate/20190620000004_add_more_column_types_to_wands.rb +1 -1
- data/spec/support/v6.0/sorbet_test_cases.rb +26 -16
- data/spec/test_data/v5.0/expected_internal_metadata.rbi +209 -197
- data/spec/test_data/v5.0/expected_potion.rbi +208 -196
- data/spec/test_data/v5.0/expected_schema_migration.rbi +209 -197
- data/spec/test_data/v5.0/expected_spell_book.rbi +208 -196
- data/spec/test_data/v5.0/expected_squib.rbi +209 -197
- data/spec/test_data/v5.0/expected_wand.rbi +208 -196
- data/spec/test_data/v5.0/expected_wizard.rbi +209 -197
- data/spec/test_data/v5.0/expected_wizard_wo_spellbook.rbi +209 -197
- data/spec/test_data/v5.1/expected_internal_metadata.rbi +213 -213
- data/spec/test_data/v5.1/expected_potion.rbi +212 -212
- data/spec/test_data/v5.1/expected_schema_migration.rbi +213 -213
- data/spec/test_data/v5.1/expected_spell_book.rbi +212 -212
- data/spec/test_data/v5.1/expected_squib.rbi +213 -213
- data/spec/test_data/v5.1/expected_wand.rbi +212 -212
- data/spec/test_data/v5.1/expected_wizard.rbi +213 -213
- data/spec/test_data/v5.1/expected_wizard_wo_spellbook.rbi +213 -213
- data/spec/test_data/v5.2/expected_attachment.rbi +212 -212
- data/spec/test_data/v5.2/expected_blob.rbi +212 -212
- data/spec/test_data/v5.2/expected_internal_metadata.rbi +213 -213
- data/spec/test_data/v5.2/expected_potion.rbi +212 -212
- data/spec/test_data/v5.2/expected_schema_migration.rbi +213 -213
- data/spec/test_data/v5.2/expected_spell_book.rbi +212 -212
- data/spec/test_data/v5.2/expected_squib.rbi +213 -213
- data/spec/test_data/v5.2/expected_wand.rbi +212 -212
- data/spec/test_data/v5.2/expected_wizard.rbi +213 -213
- data/spec/test_data/v5.2/expected_wizard_wo_spellbook.rbi +213 -213
- data/spec/test_data/v6.0/expected_attachment.rbi +244 -244
- data/spec/test_data/v6.0/expected_blob.rbi +244 -244
- data/spec/test_data/v6.0/expected_internal_metadata.rbi +245 -245
- data/spec/test_data/v6.0/expected_potion.rbi +244 -244
- data/spec/test_data/v6.0/expected_schema_migration.rbi +245 -245
- data/spec/test_data/v6.0/expected_spell_book.rbi +244 -244
- data/spec/test_data/v6.0/expected_squib.rbi +245 -245
- data/spec/test_data/v6.0/expected_wand.rbi +244 -244
- data/spec/test_data/v6.0/expected_wizard.rbi +245 -245
- data/spec/test_data/v6.0/expected_wizard_wo_spellbook.rbi +245 -245
- metadata +8 -185
- data/spec/bin/install.sh +0 -11
- data/spec/support/v4.2/.gitignore +0 -17
- data/spec/support/v4.2/Gemfile +0 -33
- data/spec/support/v4.2/Gemfile.lock +0 -128
- data/spec/support/v4.2/README.rdoc +0 -28
- data/spec/support/v4.2/Rakefile +0 -6
- data/spec/support/v4.2/app/assets/images/.keep +0 -0
- data/spec/support/v4.2/app/assets/stylesheets/application.css +0 -15
- data/spec/support/v4.2/app/controllers/application_controller.rb +0 -6
- data/spec/support/v4.2/app/controllers/concerns/.keep +0 -0
- data/spec/support/v4.2/app/helpers/application_helper.rb +0 -3
- data/spec/support/v4.2/app/helpers/bar_helper.rb +0 -3
- data/spec/support/v4.2/app/helpers/baz_helper.rb +0 -3
- data/spec/support/v4.2/app/helpers/foo_helper.rb +0 -3
- data/spec/support/v4.2/app/mailers/.keep +0 -0
- data/spec/support/v4.2/app/mailers/application_mailer.rb +0 -3
- data/spec/support/v4.2/app/mailers/daily_prophet_mailer.rb +0 -9
- data/spec/support/v4.2/app/mailers/hogwarts_acceptance_mailer.rb +0 -13
- data/spec/support/v4.2/app/models/.keep +0 -0
- data/spec/support/v4.2/app/models/application_record.rb +0 -4
- data/spec/support/v4.2/app/models/concerns/.keep +0 -0
- data/spec/support/v4.2/app/models/concerns/mythical.rb +0 -11
- data/spec/support/v4.2/app/models/potion.rb +0 -5
- data/spec/support/v4.2/app/models/spell_book.rb +0 -11
- data/spec/support/v4.2/app/models/squib.rb +0 -6
- data/spec/support/v4.2/app/models/wand.rb +0 -19
- data/spec/support/v4.2/app/models/wizard.rb +0 -29
- data/spec/support/v4.2/app/views/layouts/application.html.erb +0 -13
- data/spec/support/v4.2/bin/bundle +0 -3
- data/spec/support/v4.2/bin/rails +0 -4
- data/spec/support/v4.2/bin/rake +0 -4
- data/spec/support/v4.2/bin/setup +0 -29
- data/spec/support/v4.2/config.ru +0 -4
- data/spec/support/v4.2/config/application.rb +0 -36
- data/spec/support/v4.2/config/boot.rb +0 -4
- data/spec/support/v4.2/config/database.yml +0 -25
- data/spec/support/v4.2/config/environment.rb +0 -6
- data/spec/support/v4.2/config/environments/development.rb +0 -29
- data/spec/support/v4.2/config/environments/production.rb +0 -68
- data/spec/support/v4.2/config/environments/test.rb +0 -43
- data/spec/support/v4.2/config/initializers/backtrace_silencers.rb +0 -8
- data/spec/support/v4.2/config/initializers/cookies_serializer.rb +0 -4
- data/spec/support/v4.2/config/initializers/filter_parameter_logging.rb +0 -5
- data/spec/support/v4.2/config/initializers/inflections.rb +0 -17
- data/spec/support/v4.2/config/initializers/mime_types.rb +0 -5
- data/spec/support/v4.2/config/initializers/session_store.rb +0 -4
- data/spec/support/v4.2/config/initializers/sorbet_rails.rb +0 -3
- data/spec/support/v4.2/config/initializers/to_time_preserves_timezone.rb +0 -11
- data/spec/support/v4.2/config/initializers/wrap_parameters.rb +0 -15
- data/spec/support/v4.2/config/locales/en.yml +0 -23
- data/spec/support/v4.2/config/routes.rb +0 -58
- data/spec/support/v4.2/db/migrate/20190620000001_create_wizards.rb +0 -13
- data/spec/support/v4.2/db/migrate/20190620000002_create_wands.rb +0 -12
- data/spec/support/v4.2/db/migrate/20190620000003_create_spell_books.rb +0 -10
- data/spec/support/v4.2/db/migrate/20190620000004_add_more_column_types_to_wands.rb +0 -17
- data/spec/support/v4.2/db/migrate/20190620000005_add_broom_to_wizard.rb +0 -6
- data/spec/support/v4.2/db/migrate/20190620000007_add_type_to_wizard.rb +0 -6
- data/spec/support/v4.2/db/schema.rb +0 -49
- data/spec/support/v4.2/db/seeds.rb +0 -8
- data/spec/support/v4.2/lib/assets/.keep +0 -0
- data/spec/support/v4.2/lib/mythical_rbi_plugin.rb +0 -16
- data/spec/support/v4.2/lib/tasks/.keep +0 -0
- data/spec/support/v4.2/log/.keep +0 -0
- data/spec/support/v4.2/sorbet_test_cases.rb +0 -238
- data/spec/support/v4.2/test/controllers/.keep +0 -0
- data/spec/support/v4.2/test/fixtures/.keep +0 -0
- data/spec/support/v4.2/test/helpers/.keep +0 -0
- data/spec/support/v4.2/test/integration/.keep +0 -0
- data/spec/support/v4.2/test/mailers/.keep +0 -0
- data/spec/support/v4.2/test/models/.keep +0 -0
- data/spec/support/v4.2/test/test_helper.rb +0 -11
- data/spec/support/v4.2/typed-override.yaml +0 -2
- data/spec/support/v4.2/vendor/assets/stylesheets/.keep +0 -0
- data/spec/test_data/v4.2/expected_application_mailer.rbi +0 -5
- data/spec/test_data/v4.2/expected_daily_prophet_mailer.rbi +0 -7
- data/spec/test_data/v4.2/expected_helpers.rbi +0 -22
- data/spec/test_data/v4.2/expected_helpers_with_application_and_devise_helpers.rbi +0 -29
- data/spec/test_data/v4.2/expected_hogwarts_acceptance_mailer.rbi +0 -10
- data/spec/test_data/v4.2/expected_no_routes.rbi +0 -4
- data/spec/test_data/v4.2/expected_potion.rbi +0 -657
- data/spec/test_data/v4.2/expected_routes.rbi +0 -19
- data/spec/test_data/v4.2/expected_spell_book.rbi +0 -771
- data/spec/test_data/v4.2/expected_squib.rbi +0 -912
- data/spec/test_data/v4.2/expected_srb_tc_output.txt +0 -65
- data/spec/test_data/v4.2/expected_wand.rbi +0 -873
- data/spec/test_data/v4.2/expected_wizard.rbi +0 -912
- data/spec/test_data/v4.2/expected_wizard_wo_spellbook.rbi +0 -912
@@ -24,7 +24,7 @@ class SorbetRails::ModelPlugins::ActiveRecordQuerying < SorbetRails::ModelPlugin
|
|
24
24
|
# rails/activerecord/lib/active_record/querying.rb
|
25
25
|
model_query_relation_methods = [
|
26
26
|
:select, :reselect, :order, :reorder, :group, :limit, :offset, :joins, :left_joins, :left_outer_joins,
|
27
|
-
:where, :rewhere, :preload, :extract_associated, :eager_load, :includes, :from, :lock, :readonly, :
|
27
|
+
:where, :rewhere, :preload, :extract_associated, :eager_load, :includes, :from, :lock, :readonly, :or,
|
28
28
|
:having, :create_with, :distinct, :references, :none, :unscope, :optimizer_hints, :merge, :except, :only,
|
29
29
|
]
|
30
30
|
model_query_relation_methods.each do |method_name|
|
@@ -33,9 +33,17 @@ class SorbetRails::ModelPlugins::ActiveRecordQuerying < SorbetRails::ModelPlugin
|
|
33
33
|
method_name.to_s,
|
34
34
|
parameters: [
|
35
35
|
Parameter.new("*args", type: "T.untyped"),
|
36
|
-
Parameter.new("&block", type: "T.nilable(T.proc.void)"),
|
37
36
|
],
|
38
37
|
) if exists_class_method?(method_name)
|
39
38
|
end
|
39
|
+
|
40
|
+
add_relation_query_method(
|
41
|
+
root,
|
42
|
+
"extending",
|
43
|
+
parameters: [
|
44
|
+
Parameter.new("*args", type: "T.untyped"),
|
45
|
+
Parameter.new("&block", type: "T.nilable(T.proc.void)"),
|
46
|
+
]
|
47
|
+
)
|
40
48
|
end
|
41
49
|
end
|
@@ -72,6 +72,12 @@ module SorbetRails::ModelPlugins
|
|
72
72
|
when :friendly_id
|
73
73
|
require('sorbet-rails/gem_plugins/friendly_id_plugin')
|
74
74
|
FriendlyIdPlugin
|
75
|
+
when :shrine
|
76
|
+
require('sorbet-rails/gem_plugins/shrine_plugin')
|
77
|
+
ShrinePlugin
|
78
|
+
when :elastic_search
|
79
|
+
require('sorbet-rails/gem_plugins/elastic_search_plugin')
|
80
|
+
ElasticSearchPlugin
|
75
81
|
else
|
76
82
|
raise UnrecognizedPluginName.new(
|
77
83
|
"Unrecognized plugin with name: #{plugin_name}. Please check available plugins in the
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# typed: false
|
2
|
+
require 'sorbet-runtime'
|
3
|
+
|
4
|
+
module SorbetRails::PluckToTStruct
|
5
|
+
extend T::Sig
|
6
|
+
sig {
|
7
|
+
type_parameters(:U).
|
8
|
+
params(
|
9
|
+
ta_struct: ITypeAssert[T.type_parameter(:U)],
|
10
|
+
).
|
11
|
+
returns(T::Array[T.type_parameter(:U)])
|
12
|
+
}
|
13
|
+
def pluck_to_tstruct(ta_struct)
|
14
|
+
tstruct = ta_struct.get_type
|
15
|
+
|
16
|
+
if !(tstruct < T::Struct)
|
17
|
+
raise UnexpectedType.new("pluck_to_tstruct expects a tstruct subclass, given #{tstruct}")
|
18
|
+
end
|
19
|
+
|
20
|
+
keys = tstruct.props.keys
|
21
|
+
|
22
|
+
# loosely based on pluck_to_hash gem
|
23
|
+
# https://github.com/girishso/pluck_to_hash/blob/master/lib/pluck_to_hash.rb
|
24
|
+
keys_one = keys.size == 1
|
25
|
+
pluck(*keys).map do |row|
|
26
|
+
row = [row] if keys_one
|
27
|
+
value = Hash[keys.zip(row)]
|
28
|
+
tstruct.new(value)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class UnexpectedType < StandardError; end
|
33
|
+
end
|
data/lib/sorbet-rails/railtie.rb
CHANGED
@@ -15,10 +15,14 @@ class SorbetRails::Railtie < Rails::Railtie
|
|
15
15
|
ActiveSupport.on_load(:active_record) do
|
16
16
|
require "sorbet-rails/rails_mixins/active_record_overrides"
|
17
17
|
require "sorbet-rails/rails_mixins/custom_finder_methods"
|
18
|
+
require "sorbet-rails/rails_mixins/pluck_to_tstruct"
|
18
19
|
|
19
20
|
ActiveRecord::Base.extend SorbetRails::CustomFinderMethods
|
20
21
|
ActiveRecord::Relation.include SorbetRails::CustomFinderMethods
|
21
22
|
|
23
|
+
ActiveRecord::Base.extend SorbetRails::PluckToTStruct
|
24
|
+
ActiveRecord::Relation.include SorbetRails::PluckToTStruct
|
25
|
+
|
22
26
|
class ::ActiveRecord::Base
|
23
27
|
# open ActiveRecord::Base to override inherited
|
24
28
|
class << self
|
@@ -11,7 +11,10 @@ module SorbetRails::SorbetUtils
|
|
11
11
|
signature = T::Private::Methods.signature_for_method(method_def)
|
12
12
|
|
13
13
|
parameters_with_type = signature.nil? ?
|
14
|
-
method_def.parameters.map { |p|
|
14
|
+
method_def.parameters.map { |p|
|
15
|
+
p1 = p.size == 1 ? p + ['_'] : p # give param without name default name _
|
16
|
+
p1 + ['T.untyped'] # append untyped as type of each param
|
17
|
+
} :
|
15
18
|
get_ordered_parameters_with_type(signature)
|
16
19
|
|
17
20
|
parameters_with_type.map do |param_def|
|
@@ -18,7 +18,7 @@ namespace :rails_rbi do
|
|
18
18
|
Rake::Task['rails_rbi:models'].invoke
|
19
19
|
Rake::Task['rails_rbi:helpers'].invoke
|
20
20
|
Rake::Task['rails_rbi:mailers'].invoke
|
21
|
-
Rake::Task['rails_rbi:
|
21
|
+
Rake::Task['rails_rbi:custom'].invoke
|
22
22
|
end
|
23
23
|
|
24
24
|
desc "Generate rbis for rails routes"
|
@@ -32,9 +32,11 @@ namespace :rails_rbi do
|
|
32
32
|
File.write(file_path, inspector.format(SorbetRails::RoutesRbiFormatter.new))
|
33
33
|
end
|
34
34
|
|
35
|
-
desc "Copy
|
36
|
-
task
|
35
|
+
desc "Copy custom rbis for ActionController::Parameters, pluck_to_struct, etc."
|
36
|
+
task custom: :environment do
|
37
|
+
copy_bundled_rbi('type_assert.rbi')
|
37
38
|
copy_bundled_rbi('parameters.rbi')
|
39
|
+
copy_bundled_rbi('pluck_to_tstruct.rbi')
|
38
40
|
end
|
39
41
|
|
40
42
|
desc "Generate rbis for rails models. Pass models name to regenerate rbi for only the given models."
|
@@ -43,9 +45,17 @@ namespace :rails_rbi do
|
|
43
45
|
|
44
46
|
all_models = Set.new(ActiveRecord::Base.descendants + whitelisted_models - blacklisted_models)
|
45
47
|
|
46
|
-
models_to_generate =
|
47
|
-
|
48
|
-
|
48
|
+
models_to_generate = all_models
|
49
|
+
if args.extras.size > 0
|
50
|
+
models_to_generate = args.extras.map { |m| Object.const_get(m) }
|
51
|
+
# also generate descendants of a model
|
52
|
+
models_to_generate = models_to_generate.
|
53
|
+
map { |m| [m, m.descendants] }.
|
54
|
+
flatten.
|
55
|
+
uniq
|
56
|
+
# be safe about ignoring blacklisted models
|
57
|
+
models_to_generate = models_to_generate - blacklisted_models
|
58
|
+
end
|
49
59
|
|
50
60
|
generated_rbis = generate_rbis_for_models(models_to_generate, all_models)
|
51
61
|
generated_rbis.each do |model_name, contents|
|
data/sorbet-rails.gemspec
CHANGED
data/spec/bin/run_all_specs.sh
CHANGED
data/spec/bin/run_spec.sh
CHANGED
@@ -11,17 +11,15 @@ else
|
|
11
11
|
rails_dir="$wd/../support/v$RAILS_VERSION"
|
12
12
|
fi
|
13
13
|
|
14
|
-
bundle_version=$([ "$RAILS_VERSION" == 4.2 ] && echo "_1.17.3_" || echo "")
|
15
|
-
|
16
14
|
# clean up & install gems
|
17
15
|
rm $wd/../../Gemfile.lock 2>/dev/null # clean up Gemfile.lock first
|
18
|
-
bundle
|
16
|
+
bundle install
|
19
17
|
|
20
18
|
echo $rails_dir
|
21
19
|
|
22
20
|
# update gems in rails_dir
|
23
21
|
cd $rails_dir
|
24
|
-
bundle
|
22
|
+
bundle install
|
25
23
|
|
26
24
|
# move back to current dir to run test
|
27
25
|
cd $current_dir
|
@@ -31,6 +29,6 @@ if [[ -z $RAILS_VERSION ]]; then
|
|
31
29
|
bundle update && DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bundle exec rake
|
32
30
|
else
|
33
31
|
echo "---- Run $RAILS_VERSION ----"
|
34
|
-
RAILS_VERSION=$RAILS_VERSION bundle
|
35
|
-
RAILS_VERSION=$RAILS_VERSION DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bundle
|
32
|
+
RAILS_VERSION=$RAILS_VERSION bundle update
|
33
|
+
RAILS_VERSION=$RAILS_VERSION DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bundle exec rake
|
36
34
|
fi
|
@@ -76,14 +76,6 @@ def create_helpers
|
|
76
76
|
end
|
77
77
|
|
78
78
|
def create_models
|
79
|
-
if ENV['RAILS_VERSION'] == '4.2'
|
80
|
-
file "app/models/application_record.rb", <<~RUBY
|
81
|
-
class ApplicationRecord < ActiveRecord::Base
|
82
|
-
self.abstract_class = true
|
83
|
-
end
|
84
|
-
RUBY
|
85
|
-
end
|
86
|
-
|
87
79
|
file "app/models/spell_book.rb", <<~RUBY
|
88
80
|
class SpellBook < ApplicationRecord
|
89
81
|
validates :name, length: { minimum: 5 }, presence: true
|
@@ -131,89 +123,56 @@ def create_models
|
|
131
123
|
attachments = "has_one_attached :school_photo\n has_many_attached :hats"
|
132
124
|
end
|
133
125
|
|
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
|
-
enum broom: {
|
186
|
-
nimbus: 'nimbus',
|
187
|
-
firebolt: 'firebolt',
|
188
|
-
}, _prefix: true
|
189
|
-
|
190
|
-
enum quidditch_position: {
|
191
|
-
keeper: 0,
|
192
|
-
seeker: 1,
|
193
|
-
beater: 2,
|
194
|
-
chaser: 3,
|
195
|
-
}, _prefix: :quidditch
|
196
|
-
|
197
|
-
enum hair_color: {
|
198
|
-
brown: 0,
|
199
|
-
black: 1,
|
200
|
-
blonde: 2,
|
201
|
-
}, _suffix: :hair
|
202
|
-
|
203
|
-
enum eye_color: {
|
204
|
-
brown: 0,
|
205
|
-
green: 1,
|
206
|
-
blue: 2,
|
207
|
-
}, _prefix: :color, _suffix: :eyes
|
208
|
-
|
209
|
-
has_one :wand
|
210
|
-
has_many :spell_books
|
211
|
-
|
212
|
-
scope :recent, -> { where('created_at > ?', 1.month.ago) }
|
213
|
-
#{attachments}
|
214
|
-
end
|
215
|
-
RUBY
|
216
|
-
end
|
126
|
+
file "app/models/wizard.rb", <<~RUBY
|
127
|
+
class Wizard < ApplicationRecord
|
128
|
+
validates :name, length: { minimum: 5 }, presence: true
|
129
|
+
|
130
|
+
enum house: {
|
131
|
+
Gryffindor: 0,
|
132
|
+
Hufflepuff: 1,
|
133
|
+
Ravenclaw: 2,
|
134
|
+
Slytherin: 3,
|
135
|
+
}
|
136
|
+
|
137
|
+
enum professor: {
|
138
|
+
"Severus Snape": 0,
|
139
|
+
"Minerva McGonagall": 1,
|
140
|
+
"Pomona Sprout": 2,
|
141
|
+
"Filius Flitwick": 3,
|
142
|
+
"Hagrid": 4,
|
143
|
+
}
|
144
|
+
|
145
|
+
enum broom: {
|
146
|
+
nimbus: 'nimbus',
|
147
|
+
firebolt: 'firebolt',
|
148
|
+
}, _prefix: true
|
149
|
+
|
150
|
+
enum quidditch_position: {
|
151
|
+
keeper: 0,
|
152
|
+
seeker: 1,
|
153
|
+
beater: 2,
|
154
|
+
chaser: 3,
|
155
|
+
}, _prefix: :quidditch
|
156
|
+
|
157
|
+
enum hair_color: {
|
158
|
+
brown: 0,
|
159
|
+
black: 1,
|
160
|
+
blonde: 2,
|
161
|
+
}, _suffix: :hair
|
162
|
+
|
163
|
+
enum eye_color: {
|
164
|
+
brown: 0,
|
165
|
+
green: 1,
|
166
|
+
blue: 2,
|
167
|
+
}, _prefix: :color, _suffix: :eyes
|
168
|
+
|
169
|
+
has_one :wand
|
170
|
+
has_many :spell_books
|
171
|
+
|
172
|
+
scope :recent, -> { where('created_at > ?', 1.month.ago) }
|
173
|
+
#{attachments}
|
174
|
+
end
|
175
|
+
RUBY
|
217
176
|
|
218
177
|
file "app/models/concerns/mythical.rb", <<~RUBY
|
219
178
|
require 'active_support/concern'
|
@@ -238,11 +197,7 @@ def create_models
|
|
238
197
|
end
|
239
198
|
|
240
199
|
def create_migrations
|
241
|
-
|
242
|
-
migration_superclass = 'ActiveRecord::Migration'
|
243
|
-
else
|
244
|
-
migration_superclass = "ActiveRecord::Migration[#{ENV['RAILS_VERSION']}]"
|
245
|
-
end
|
200
|
+
migration_superclass = "ActiveRecord::Migration[#{ENV['RAILS_VERSION']}]"
|
246
201
|
|
247
202
|
file "db/migrate/20190620000001_create_wizards.rb", <<~RUBY
|
248
203
|
class CreateWizards < #{migration_superclass}
|
@@ -296,7 +251,7 @@ def create_migrations
|
|
296
251
|
add_column :wands, :chosen_at_date, :date
|
297
252
|
add_column :wands, :chosen_at_time, :time
|
298
253
|
# JSON column type is only supported on 5.2 or higher
|
299
|
-
unless ['
|
254
|
+
unless ['5.0', '5.1'].include?(ENV['RAILS_VERSION'])
|
300
255
|
add_column :wands, :spell_history, :json
|
301
256
|
add_column :wands, :maker_info, :json, null: false, default: '{}'
|
302
257
|
end
|
@@ -312,18 +267,16 @@ def create_migrations
|
|
312
267
|
end
|
313
268
|
RUBY
|
314
269
|
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
add_column :wizards, :hair_length, :integer
|
323
|
-
end
|
270
|
+
file "db/migrate/20190620000006_add_more_enums_to_wizard.rb", <<~RUBY
|
271
|
+
class AddMoreEnumsToWizard < #{migration_superclass}
|
272
|
+
def change
|
273
|
+
add_column :wizards, :quidditch_position, :integer
|
274
|
+
add_column :wizards, :hair_color, :integer
|
275
|
+
add_column :wizards, :eye_color, :integer
|
276
|
+
add_column :wizards, :hair_length, :integer
|
324
277
|
end
|
325
|
-
|
326
|
-
|
278
|
+
end
|
279
|
+
RUBY
|
327
280
|
|
328
281
|
file "db/migrate/20190620000007_add_type_to_wizard.rb", <<~RUBY
|
329
282
|
class AddTypeToWizard < #{migration_superclass}
|
@@ -335,13 +288,6 @@ def create_migrations
|
|
335
288
|
end
|
336
289
|
|
337
290
|
def create_mailers
|
338
|
-
if ENV['RAILS_VERSION'] == '4.2'
|
339
|
-
file "app/mailers/application_mailer.rb", <<~RUBY
|
340
|
-
class ApplicationMailer < ActionMailer::Base
|
341
|
-
end
|
342
|
-
RUBY
|
343
|
-
end
|
344
|
-
|
345
291
|
file "app/mailers/hogwarts_acceptance_mailer.rb", <<~RUBY
|
346
292
|
class HogwartsAcceptanceMailer < ApplicationMailer
|
347
293
|
extend T::Sig
|
@@ -386,7 +332,7 @@ end
|
|
386
332
|
# Main setup
|
387
333
|
source_paths
|
388
334
|
|
389
|
-
if ['
|
335
|
+
if ['5.0'].include?(ENV["RAILS_VERSION"])
|
390
336
|
File.open('Gemfile', 'r+') do |f|
|
391
337
|
out = ""
|
392
338
|
f.each do |line|
|
@@ -414,19 +360,16 @@ after_bundle do
|
|
414
360
|
create_mailers
|
415
361
|
add_sorbet_test_files
|
416
362
|
|
417
|
-
bundle_version = ENV["RAILS_VERSION"] == "4.2" ? "_1.17.3_" : ""
|
418
|
-
|
419
363
|
Bundler.with_clean_env do
|
420
|
-
|
421
|
-
run "bundle #{bundle_version} exec rake db:migrate"
|
364
|
+
run "bundle exec rake db:migrate"
|
422
365
|
end
|
423
366
|
|
424
367
|
if ENV["RUN_WITH_SORBET"] != 'false'
|
425
368
|
Bundler.with_clean_env do
|
426
|
-
run "SRB_YES=true bundle
|
427
|
-
run "bundle
|
428
|
-
run "bundle
|
429
|
-
run "bundle
|
369
|
+
run "SRB_YES=true bundle exec srb init"
|
370
|
+
run "bundle exec rake rails_rbi:all"
|
371
|
+
run "bundle exec srb rbi hidden-definitions"
|
372
|
+
run "bundle exec srb rbi todo"
|
430
373
|
end
|
431
374
|
end
|
432
375
|
say "Done!"
|
@@ -17,12 +17,7 @@ T.assert_type!(wand.chosen_at_date, T.nilable(Date))
|
|
17
17
|
wizard.created_at = DateTime.now
|
18
18
|
T.assert_type!(wizard.created_at, ActiveSupport::TimeWithZone)
|
19
19
|
|
20
|
-
|
21
|
-
# note: this is expected to fail in Rails 4.2, because in that version only
|
22
|
-
# `datetime` fields are made time zone aware (so this will have the type
|
23
|
-
# `T.nilable(Time)` instead)
|
24
|
-
T.assert_type!(wand.chosen_at_time, T.nilable(ActiveSupport::TimeWithZone))
|
25
|
-
end
|
20
|
+
T.assert_type!(wand.chosen_at_time, T.nilable(ActiveSupport::TimeWithZone))
|
26
21
|
|
27
22
|
# -- model associations
|
28
23
|
T.assert_type!(wizard.wand, T.nilable(Wand))
|
@@ -87,9 +82,9 @@ spell_books = wizard.spell_books
|
|
87
82
|
T.assert_type!(spell_books.exists?(name: 'Fantastic Beasts'), T::Boolean)
|
88
83
|
T.assert_type!(spell_books.find(spell_book.id), SpellBook)
|
89
84
|
T.assert_type!(spell_books.first!, SpellBook)
|
90
|
-
# T.assert_type!(spell_books.first, T.nilable(SpellBook)) # TODO fix sig for
|
85
|
+
# T.assert_type!(spell_books.first, T.nilable(SpellBook)) # TODO fix sig for 5.0
|
91
86
|
T.assert_type!(spell_books.last!, SpellBook)
|
92
|
-
# T.assert_type!(spell_books.last, T.nilable(SpellBook)) # TODO fix sig for
|
87
|
+
# T.assert_type!(spell_books.last, T.nilable(SpellBook)) # TODO fix sig for 5.0
|
93
88
|
T.assert_type!(spell_books.first_n(5), T::Array[SpellBook])
|
94
89
|
T.assert_type!(spell_books.last_n(5), T::Array[SpellBook])
|
95
90
|
T.assert_type!(spell_books.find_by(name: 'Fantastic Beasts'), T.nilable(SpellBook))
|
@@ -109,9 +104,9 @@ spell_books_query = spell_books.where(id: 1)
|
|
109
104
|
T.assert_type!(spell_books_query.exists?(name: 'Fantastic Beasts'), T::Boolean)
|
110
105
|
T.assert_type!(spell_books_query.find(spell_book.id), SpellBook)
|
111
106
|
T.assert_type!(spell_books_query.first!, SpellBook)
|
112
|
-
# T.assert_type!(spell_books_query.first, T.nilable(SpellBook)) # TODO fix sig for
|
107
|
+
# T.assert_type!(spell_books_query.first, T.nilable(SpellBook)) # TODO fix sig for 5.0
|
113
108
|
T.assert_type!(spell_books_query.last!, SpellBook)
|
114
|
-
# T.assert_type!(spell_books_query.last, T.nilable(SpellBook)) # TODO fix sig for
|
109
|
+
# T.assert_type!(spell_books_query.last, T.nilable(SpellBook)) # TODO fix sig for 5.0
|
115
110
|
T.assert_type!(spell_books_query.first_n(5), T::Array[SpellBook])
|
116
111
|
T.assert_type!(spell_books_query.last_n(5), T::Array[SpellBook])
|
117
112
|
T.assert_type!(spell_books_query.find_by(name: 'Fantastic Beasts'), T.nilable(SpellBook))
|
@@ -180,12 +175,10 @@ T.assert_type!(Wand.mythicals, T::Array[Wand])
|
|
180
175
|
|
181
176
|
T.assert_type!(HogwartsAcceptanceMailer.notify(wizard), ActionMailer::MessageDelivery)
|
182
177
|
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
T.assert_type!(wizard.brown_hair?, T::Boolean)
|
188
|
-
end
|
178
|
+
T.assert_type!(wizard.broom_nimbus?, T::Boolean)
|
179
|
+
T.assert_type!(wizard.color_brown_eyes?, T::Boolean)
|
180
|
+
T.assert_type!(wizard.quidditch_keeper?, T::Boolean)
|
181
|
+
T.assert_type!(wizard.brown_hair?, T::Boolean)
|
189
182
|
|
190
183
|
# -- Custom ActionController::Parameters Methods
|
191
184
|
params = ActionController::Parameters.new({
|
@@ -236,3 +229,20 @@ T.assert_type!(
|
|
236
229
|
params.fetch_typed(:nonexistence, TA[T::Array[Integer]].new, []),
|
237
230
|
T::Array[Integer],
|
238
231
|
)
|
232
|
+
|
233
|
+
|
234
|
+
# -- pluck to tstruct
|
235
|
+
class WizardStruct < T::Struct
|
236
|
+
const :name, String
|
237
|
+
const :house, T.nilable(String)
|
238
|
+
end
|
239
|
+
|
240
|
+
T.assert_type!(Wizard.pluck_to_tstruct(TA[WizardStruct].new), T::Array[WizardStruct])
|
241
|
+
T.assert_type!(Wizard.all.pluck_to_tstruct(TA[WizardStruct].new), T::Array[WizardStruct])
|
242
|
+
|
243
|
+
Wizard.pluck_to_tstruct(TA[WizardStruct].new).each do |row|
|
244
|
+
T.assert_type!(row, WizardStruct)
|
245
|
+
end
|
246
|
+
Wizard.all.pluck_to_tstruct(TA[WizardStruct].new).each do |row|
|
247
|
+
T.assert_type!(row, WizardStruct)
|
248
|
+
end
|