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.
Files changed (175) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.travis.yml +0 -2
  4. data/CONTRIBUTING.md +2 -4
  5. data/Gemfile +0 -4
  6. data/README.md +41 -7
  7. data/Rakefile +3 -14
  8. data/lib/bundled_rbi/parameters.rbi +4 -26
  9. data/lib/bundled_rbi/pluck_to_tstruct.rbi +22 -0
  10. data/lib/bundled_rbi/type_assert.rbi +27 -0
  11. data/lib/sorbet-rails/gem_plugins/elastic_search_plugin.rb +40 -0
  12. data/lib/sorbet-rails/gem_plugins/shrine_plugin.rb +76 -0
  13. data/lib/sorbet-rails/model_plugins/active_record_assoc.rb +2 -11
  14. data/lib/sorbet-rails/model_plugins/active_record_attribute.rb +3 -6
  15. data/lib/sorbet-rails/model_plugins/active_record_querying.rb +10 -2
  16. data/lib/sorbet-rails/model_plugins/plugins.rb +6 -0
  17. data/lib/sorbet-rails/rails_mixins/pluck_to_tstruct.rb +33 -0
  18. data/lib/sorbet-rails/railtie.rb +4 -0
  19. data/lib/sorbet-rails/sorbet_utils.rb +4 -1
  20. data/lib/sorbet-rails/tasks/rails_rbi.rake +16 -6
  21. data/sorbet-rails.gemspec +1 -1
  22. data/spec/bin/run_all_specs.sh +0 -1
  23. data/spec/bin/run_spec.sh +4 -6
  24. data/spec/generators/rails-template.rb +67 -124
  25. data/spec/generators/sorbet_test_cases.rb +26 -16
  26. data/spec/pluck_to_tstruct_spec.rb +79 -0
  27. data/spec/rails_helper.rb +0 -2
  28. data/spec/rake_rails_rbi_models_spec.rb +2 -0
  29. data/spec/sorbet_utils_spec.rb +37 -0
  30. data/spec/spec_helper.rb +1 -2
  31. data/spec/support/v5.0/Gemfile.lock +6 -6
  32. data/spec/support/v5.0/config/boot.rb +1 -1
  33. data/spec/support/v5.0/config/initializers/sorbet_rails.rb +1 -1
  34. data/spec/support/v5.0/db/migrate/20190620000004_add_more_column_types_to_wands.rb +1 -1
  35. data/spec/support/v5.0/sorbet_test_cases.rb +26 -16
  36. data/spec/support/v5.1/Gemfile.lock +6 -6
  37. data/spec/support/v5.1/config/boot.rb +1 -1
  38. data/spec/support/v5.1/config/initializers/sorbet_rails.rb +1 -1
  39. data/spec/support/v5.1/db/migrate/20190620000004_add_more_column_types_to_wands.rb +1 -1
  40. data/spec/support/v5.1/sorbet_test_cases.rb +26 -16
  41. data/spec/support/v5.2/Gemfile.lock +6 -6
  42. data/spec/support/v5.2/config/boot.rb +1 -1
  43. data/spec/support/v5.2/config/initializers/sorbet_rails.rb +1 -1
  44. data/spec/support/v5.2/db/migrate/20190620000004_add_more_column_types_to_wands.rb +1 -1
  45. data/spec/support/v5.2/sorbet_test_cases.rb +26 -16
  46. data/spec/support/v6.0/Gemfile.lock +6 -6
  47. data/spec/support/v6.0/config/boot.rb +1 -1
  48. data/spec/support/v6.0/config/initializers/sorbet_rails.rb +1 -1
  49. data/spec/support/v6.0/config/initializers/wrap_parameters.rb +1 -1
  50. data/spec/support/v6.0/db/migrate/20190620000004_add_more_column_types_to_wands.rb +1 -1
  51. data/spec/support/v6.0/sorbet_test_cases.rb +26 -16
  52. data/spec/test_data/v5.0/expected_internal_metadata.rbi +209 -197
  53. data/spec/test_data/v5.0/expected_potion.rbi +208 -196
  54. data/spec/test_data/v5.0/expected_schema_migration.rbi +209 -197
  55. data/spec/test_data/v5.0/expected_spell_book.rbi +208 -196
  56. data/spec/test_data/v5.0/expected_squib.rbi +209 -197
  57. data/spec/test_data/v5.0/expected_wand.rbi +208 -196
  58. data/spec/test_data/v5.0/expected_wizard.rbi +209 -197
  59. data/spec/test_data/v5.0/expected_wizard_wo_spellbook.rbi +209 -197
  60. data/spec/test_data/v5.1/expected_internal_metadata.rbi +213 -213
  61. data/spec/test_data/v5.1/expected_potion.rbi +212 -212
  62. data/spec/test_data/v5.1/expected_schema_migration.rbi +213 -213
  63. data/spec/test_data/v5.1/expected_spell_book.rbi +212 -212
  64. data/spec/test_data/v5.1/expected_squib.rbi +213 -213
  65. data/spec/test_data/v5.1/expected_wand.rbi +212 -212
  66. data/spec/test_data/v5.1/expected_wizard.rbi +213 -213
  67. data/spec/test_data/v5.1/expected_wizard_wo_spellbook.rbi +213 -213
  68. data/spec/test_data/v5.2/expected_attachment.rbi +212 -212
  69. data/spec/test_data/v5.2/expected_blob.rbi +212 -212
  70. data/spec/test_data/v5.2/expected_internal_metadata.rbi +213 -213
  71. data/spec/test_data/v5.2/expected_potion.rbi +212 -212
  72. data/spec/test_data/v5.2/expected_schema_migration.rbi +213 -213
  73. data/spec/test_data/v5.2/expected_spell_book.rbi +212 -212
  74. data/spec/test_data/v5.2/expected_squib.rbi +213 -213
  75. data/spec/test_data/v5.2/expected_wand.rbi +212 -212
  76. data/spec/test_data/v5.2/expected_wizard.rbi +213 -213
  77. data/spec/test_data/v5.2/expected_wizard_wo_spellbook.rbi +213 -213
  78. data/spec/test_data/v6.0/expected_attachment.rbi +244 -244
  79. data/spec/test_data/v6.0/expected_blob.rbi +244 -244
  80. data/spec/test_data/v6.0/expected_internal_metadata.rbi +245 -245
  81. data/spec/test_data/v6.0/expected_potion.rbi +244 -244
  82. data/spec/test_data/v6.0/expected_schema_migration.rbi +245 -245
  83. data/spec/test_data/v6.0/expected_spell_book.rbi +244 -244
  84. data/spec/test_data/v6.0/expected_squib.rbi +245 -245
  85. data/spec/test_data/v6.0/expected_wand.rbi +244 -244
  86. data/spec/test_data/v6.0/expected_wizard.rbi +245 -245
  87. data/spec/test_data/v6.0/expected_wizard_wo_spellbook.rbi +245 -245
  88. metadata +8 -185
  89. data/spec/bin/install.sh +0 -11
  90. data/spec/support/v4.2/.gitignore +0 -17
  91. data/spec/support/v4.2/Gemfile +0 -33
  92. data/spec/support/v4.2/Gemfile.lock +0 -128
  93. data/spec/support/v4.2/README.rdoc +0 -28
  94. data/spec/support/v4.2/Rakefile +0 -6
  95. data/spec/support/v4.2/app/assets/images/.keep +0 -0
  96. data/spec/support/v4.2/app/assets/stylesheets/application.css +0 -15
  97. data/spec/support/v4.2/app/controllers/application_controller.rb +0 -6
  98. data/spec/support/v4.2/app/controllers/concerns/.keep +0 -0
  99. data/spec/support/v4.2/app/helpers/application_helper.rb +0 -3
  100. data/spec/support/v4.2/app/helpers/bar_helper.rb +0 -3
  101. data/spec/support/v4.2/app/helpers/baz_helper.rb +0 -3
  102. data/spec/support/v4.2/app/helpers/foo_helper.rb +0 -3
  103. data/spec/support/v4.2/app/mailers/.keep +0 -0
  104. data/spec/support/v4.2/app/mailers/application_mailer.rb +0 -3
  105. data/spec/support/v4.2/app/mailers/daily_prophet_mailer.rb +0 -9
  106. data/spec/support/v4.2/app/mailers/hogwarts_acceptance_mailer.rb +0 -13
  107. data/spec/support/v4.2/app/models/.keep +0 -0
  108. data/spec/support/v4.2/app/models/application_record.rb +0 -4
  109. data/spec/support/v4.2/app/models/concerns/.keep +0 -0
  110. data/spec/support/v4.2/app/models/concerns/mythical.rb +0 -11
  111. data/spec/support/v4.2/app/models/potion.rb +0 -5
  112. data/spec/support/v4.2/app/models/spell_book.rb +0 -11
  113. data/spec/support/v4.2/app/models/squib.rb +0 -6
  114. data/spec/support/v4.2/app/models/wand.rb +0 -19
  115. data/spec/support/v4.2/app/models/wizard.rb +0 -29
  116. data/spec/support/v4.2/app/views/layouts/application.html.erb +0 -13
  117. data/spec/support/v4.2/bin/bundle +0 -3
  118. data/spec/support/v4.2/bin/rails +0 -4
  119. data/spec/support/v4.2/bin/rake +0 -4
  120. data/spec/support/v4.2/bin/setup +0 -29
  121. data/spec/support/v4.2/config.ru +0 -4
  122. data/spec/support/v4.2/config/application.rb +0 -36
  123. data/spec/support/v4.2/config/boot.rb +0 -4
  124. data/spec/support/v4.2/config/database.yml +0 -25
  125. data/spec/support/v4.2/config/environment.rb +0 -6
  126. data/spec/support/v4.2/config/environments/development.rb +0 -29
  127. data/spec/support/v4.2/config/environments/production.rb +0 -68
  128. data/spec/support/v4.2/config/environments/test.rb +0 -43
  129. data/spec/support/v4.2/config/initializers/backtrace_silencers.rb +0 -8
  130. data/spec/support/v4.2/config/initializers/cookies_serializer.rb +0 -4
  131. data/spec/support/v4.2/config/initializers/filter_parameter_logging.rb +0 -5
  132. data/spec/support/v4.2/config/initializers/inflections.rb +0 -17
  133. data/spec/support/v4.2/config/initializers/mime_types.rb +0 -5
  134. data/spec/support/v4.2/config/initializers/session_store.rb +0 -4
  135. data/spec/support/v4.2/config/initializers/sorbet_rails.rb +0 -3
  136. data/spec/support/v4.2/config/initializers/to_time_preserves_timezone.rb +0 -11
  137. data/spec/support/v4.2/config/initializers/wrap_parameters.rb +0 -15
  138. data/spec/support/v4.2/config/locales/en.yml +0 -23
  139. data/spec/support/v4.2/config/routes.rb +0 -58
  140. data/spec/support/v4.2/db/migrate/20190620000001_create_wizards.rb +0 -13
  141. data/spec/support/v4.2/db/migrate/20190620000002_create_wands.rb +0 -12
  142. data/spec/support/v4.2/db/migrate/20190620000003_create_spell_books.rb +0 -10
  143. data/spec/support/v4.2/db/migrate/20190620000004_add_more_column_types_to_wands.rb +0 -17
  144. data/spec/support/v4.2/db/migrate/20190620000005_add_broom_to_wizard.rb +0 -6
  145. data/spec/support/v4.2/db/migrate/20190620000007_add_type_to_wizard.rb +0 -6
  146. data/spec/support/v4.2/db/schema.rb +0 -49
  147. data/spec/support/v4.2/db/seeds.rb +0 -8
  148. data/spec/support/v4.2/lib/assets/.keep +0 -0
  149. data/spec/support/v4.2/lib/mythical_rbi_plugin.rb +0 -16
  150. data/spec/support/v4.2/lib/tasks/.keep +0 -0
  151. data/spec/support/v4.2/log/.keep +0 -0
  152. data/spec/support/v4.2/sorbet_test_cases.rb +0 -238
  153. data/spec/support/v4.2/test/controllers/.keep +0 -0
  154. data/spec/support/v4.2/test/fixtures/.keep +0 -0
  155. data/spec/support/v4.2/test/helpers/.keep +0 -0
  156. data/spec/support/v4.2/test/integration/.keep +0 -0
  157. data/spec/support/v4.2/test/mailers/.keep +0 -0
  158. data/spec/support/v4.2/test/models/.keep +0 -0
  159. data/spec/support/v4.2/test/test_helper.rb +0 -11
  160. data/spec/support/v4.2/typed-override.yaml +0 -2
  161. data/spec/support/v4.2/vendor/assets/stylesheets/.keep +0 -0
  162. data/spec/test_data/v4.2/expected_application_mailer.rbi +0 -5
  163. data/spec/test_data/v4.2/expected_daily_prophet_mailer.rbi +0 -7
  164. data/spec/test_data/v4.2/expected_helpers.rbi +0 -22
  165. data/spec/test_data/v4.2/expected_helpers_with_application_and_devise_helpers.rbi +0 -29
  166. data/spec/test_data/v4.2/expected_hogwarts_acceptance_mailer.rbi +0 -10
  167. data/spec/test_data/v4.2/expected_no_routes.rbi +0 -4
  168. data/spec/test_data/v4.2/expected_potion.rbi +0 -657
  169. data/spec/test_data/v4.2/expected_routes.rbi +0 -19
  170. data/spec/test_data/v4.2/expected_spell_book.rbi +0 -771
  171. data/spec/test_data/v4.2/expected_squib.rbi +0 -912
  172. data/spec/test_data/v4.2/expected_srb_tc_output.txt +0 -65
  173. data/spec/test_data/v4.2/expected_wand.rbi +0 -873
  174. data/spec/test_data/v4.2/expected_wizard.rbi +0 -912
  175. 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, :extending, :or,
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
@@ -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| p + ['T.untyped'] } : # append untyped to each
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:params'].invoke
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 additional rbis for ActionController::Parameters"
36
- task params: :environment do
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 = args.extras.size > 0 ?
47
- args.extras.map { |m| Object.const_get(m) } :
48
- all_models
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|
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{sorbet-rails}
3
- s.version = "0.5.6"
3
+ s.version = "0.5.7"
4
4
  s.date = %q{2019-04-18}
5
5
  s.summary = %q{Set of tools to make Sorbet work with Rails seamlessly.}
6
6
  s.authors = ["Chan Zuckerberg Initiative"]
@@ -3,7 +3,6 @@
3
3
 
4
4
  wd="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
5
5
 
6
- RAILS_VERSION=4.2 bash $wd/run_spec.sh
7
6
  RAILS_VERSION=5.0 bash $wd/run_spec.sh
8
7
  RAILS_VERSION=5.1 bash $wd/run_spec.sh
9
8
  RAILS_VERSION=6.0 bash $wd/run_spec.sh
@@ -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 $bundle_version install
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 $bundle_version install
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 $bundle_version update
35
- RAILS_VERSION=$RAILS_VERSION DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bundle $bundle_version exec rake
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
- if ENV["RAILS_VERSION"] == "4.2"
135
- file "app/models/wizard.rb", <<~RUBY
136
- class Wizard < ApplicationRecord
137
- validates :name, length: { minimum: 5 }, presence: true
138
-
139
- enum house: {
140
- Gryffindor: 0,
141
- Hufflepuff: 1,
142
- Ravenclaw: 2,
143
- Slytherin: 3,
144
- }
145
-
146
- enum professor: {
147
- "Severus Snape": 0,
148
- "Minerva McGonagall": 1,
149
- "Pomona Sprout": 2,
150
- "Filius Flitwick": 3,
151
- "Hagrid": 4,
152
- }
153
-
154
- enum broom: {
155
- nimbus: 'nimbus',
156
- firebolt: 'firebolt',
157
- }
158
-
159
- has_one :wand
160
- has_many :spell_books
161
-
162
- scope :recent, -> { where('created_at > ?', 1.month.ago) }
163
- end
164
- RUBY
165
- else
166
- file "app/models/wizard.rb", <<~RUBY
167
- class Wizard < ApplicationRecord
168
- validates :name, length: { minimum: 5 }, presence: true
169
-
170
- enum house: {
171
- Gryffindor: 0,
172
- Hufflepuff: 1,
173
- Ravenclaw: 2,
174
- Slytherin: 3,
175
- }
176
-
177
- enum professor: {
178
- "Severus Snape": 0,
179
- "Minerva McGonagall": 1,
180
- "Pomona Sprout": 2,
181
- "Filius Flitwick": 3,
182
- "Hagrid": 4,
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
- if ENV["RAILS_VERSION"] == "4.2"
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 ['4.2', '5.0', '5.1'].include?(ENV['RAILS_VERSION'])
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
- if ENV["RAILS_VERSION"] != "4.2"
316
- file "db/migrate/20190620000006_add_more_enums_to_wizard.rb", <<~RUBY
317
- class AddMoreEnumsToWizard < #{migration_superclass}
318
- def change
319
- add_column :wizards, :quidditch_position, :integer
320
- add_column :wizards, :hair_color, :integer
321
- add_column :wizards, :eye_color, :integer
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
- RUBY
326
- end
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 ['4.2', '5.0'].include?(ENV["RAILS_VERSION"])
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
- # Rails 4.2 doesn't have the rails_command method, so just use run.
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 #{bundle_version} exec srb init"
427
- run "bundle #{bundle_version} exec rake rails_rbi:all"
428
- run "bundle #{bundle_version} exec srb rbi hidden-definitions"
429
- run "bundle #{bundle_version} exec srb rbi todo"
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
- if ENV["RAILS_VERSION"] != "4.2"
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 4.2 and 5.0
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 4.2 and 5.0
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 4.2 and 5.0
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 4.2 and 5.0
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
- if ENV["RAILS_VERSION"] != "4.2"
184
- T.assert_type!(wizard.broom_nimbus?, T::Boolean)
185
- T.assert_type!(wizard.color_brown_eyes?, T::Boolean)
186
- T.assert_type!(wizard.quidditch_keeper?, T::Boolean)
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