sorbet-rails 0.6.5.1 → 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +1 -2
  3. data/.gitignore +2 -1
  4. data/.travis.yml +1 -1
  5. data/README.md +66 -13
  6. data/Rakefile +3 -3
  7. data/lib/bundled_rbi/customizabel_rbi_formatter.rbi +29 -0
  8. data/lib/bundled_rbi/pluck_to_tstruct.rbi +2 -1
  9. data/lib/bundled_rbi/typed_enum.rbi +7 -0
  10. data/lib/sorbet-rails.rb +1 -3
  11. data/lib/sorbet-rails/active_record_rbi_formatter.rb +6 -6
  12. data/lib/sorbet-rails/config.rb +12 -0
  13. data/lib/sorbet-rails/dependent_gem_rbis/activerecord.rbi +3 -0
  14. data/lib/sorbet-rails/dependent_gem_rbis/parlour.rbi +1896 -0
  15. data/lib/sorbet-rails/deprecation.rb +1 -0
  16. data/lib/sorbet-rails/gem_plugins/attr_json_plugin.rb +137 -0
  17. data/lib/sorbet-rails/gem_plugins/flag_shih_tzu_plugin.rb +201 -0
  18. data/lib/sorbet-rails/gem_plugins/kaminari_plugin.rb +28 -0
  19. data/lib/sorbet-rails/gem_plugins/shrine_plugin.rb +1 -1
  20. data/lib/sorbet-rails/helper_rbi_formatter.rb +1 -1
  21. data/lib/sorbet-rails/job_rbi_formatter.rb +75 -62
  22. data/lib/sorbet-rails/mailer_rbi_formatter.rb +40 -27
  23. data/lib/sorbet-rails/model_column_utils.rb +129 -0
  24. data/lib/sorbet-rails/model_plugins/active_record_assoc.rb +40 -1
  25. data/lib/sorbet-rails/model_plugins/active_record_attribute.rb +12 -112
  26. data/lib/sorbet-rails/model_plugins/active_record_serialized_attribute.rb +68 -0
  27. data/lib/sorbet-rails/model_plugins/base.rb +6 -8
  28. data/lib/sorbet-rails/model_plugins/plugins.rb +9 -0
  29. data/lib/sorbet-rails/model_rbi_formatter.rb +3 -3
  30. data/lib/sorbet-rails/model_utils.rb +5 -2
  31. data/lib/sorbet-rails/rails_mixins/generated_url_helpers.rb +2 -3
  32. data/lib/sorbet-rails/rails_mixins/pluck_to_tstruct.rb +54 -7
  33. data/lib/sorbet-rails/railtie.rb +0 -2
  34. data/lib/sorbet-rails/routes_rbi_formatter.rb +6 -2
  35. data/lib/sorbet-rails/sorbet_utils.rb +152 -150
  36. data/lib/sorbet-rails/tasks/rails_rbi.rake +9 -8
  37. data/sorbet-rails.gemspec +2 -2
  38. data/spec/bin/run_spec.sh +1 -1
  39. data/spec/generators/rails-template.rb +16 -0
  40. data/spec/generators/sorbet_test_cases.rb +28 -56
  41. data/spec/job_rbi_formatter_spec.rb +1 -1
  42. data/spec/pluck_to_tstruct_spec.rb +115 -16
  43. data/spec/rails_helper.rb +3 -7
  44. data/spec/rake_rails_rbi_jobs_spec.rb +20 -0
  45. data/spec/rake_rails_rbi_mailers_spec.rb +21 -0
  46. data/spec/sorbet_spec.rb +5 -5
  47. data/spec/support/v5.0/Gemfile +1 -1
  48. data/spec/support/v5.0/Gemfile.lock +23 -19
  49. data/spec/support/v5.0/app/controllers/application_controller.rb +1 -1
  50. data/spec/support/v5.0/app/models/headmaster.rb +1 -1
  51. data/spec/support/v5.0/app/models/potion.rb +1 -1
  52. data/spec/support/v5.0/app/models/robe.rb +1 -1
  53. data/spec/support/v5.0/app/models/school.rb +1 -1
  54. data/spec/support/v5.0/app/models/spell.rb +1 -1
  55. data/spec/support/v5.0/app/models/subject.rb +1 -1
  56. data/spec/support/v5.0/app/models/wizard.rb +5 -0
  57. data/spec/support/v5.0/config/environments/development.rb +1 -1
  58. data/spec/support/v5.0/config/environments/production.rb +1 -1
  59. data/spec/support/v5.0/config/environments/test.rb +1 -1
  60. data/spec/support/v5.0/config/routes.rb +1 -1
  61. data/spec/support/v5.0/db/migrate/20190620000015_add_serialized_to_wizards.rb +9 -0
  62. data/spec/support/v5.0/db/schema.rb +8 -4
  63. data/spec/support/v5.0/sorbet_test_cases.rb +28 -56
  64. data/spec/support/v5.1/Gemfile.lock +21 -17
  65. data/spec/support/v5.1/app/controllers/application_controller.rb +1 -1
  66. data/spec/support/v5.1/app/models/headmaster.rb +1 -1
  67. data/spec/support/v5.1/app/models/school.rb +1 -1
  68. data/spec/support/v5.1/app/models/wizard.rb +5 -0
  69. data/spec/support/v5.1/config/environments/production.rb +1 -1
  70. data/spec/support/v5.1/config/routes.rb +1 -1
  71. data/spec/support/v5.1/db/migrate/20190620000015_add_serialized_to_wizards.rb +9 -0
  72. data/spec/support/v5.1/db/schema.rb +5 -1
  73. data/spec/support/v5.1/sorbet_test_cases.rb +28 -56
  74. data/spec/support/v5.2/Gemfile +1 -1
  75. data/spec/support/v5.2/Gemfile.lock +23 -19
  76. data/spec/support/v5.2/app/models/headmaster.rb +1 -1
  77. data/spec/support/v5.2/app/models/school.rb +1 -1
  78. data/spec/support/v5.2/app/models/wizard.rb +5 -0
  79. data/spec/support/v5.2/config/environments/development.rb +1 -1
  80. data/spec/support/v5.2/config/environments/production.rb +1 -1
  81. data/spec/support/v5.2/config/environments/test.rb +1 -1
  82. data/spec/support/v5.2/config/routes.rb +1 -1
  83. data/spec/support/v5.2/db/migrate/20190620000015_add_serialized_to_wizards.rb +9 -0
  84. data/spec/support/v5.2/db/schema.rb +5 -1
  85. data/spec/support/v5.2/sorbet_test_cases.rb +28 -56
  86. data/spec/support/v6.0/.gitignore +6 -0
  87. data/spec/support/v6.0/Gemfile +3 -3
  88. data/spec/support/v6.0/Gemfile.lock +89 -84
  89. data/spec/support/v6.0/app/models/wizard.rb +5 -0
  90. data/spec/support/v6.0/bin/bundle +22 -13
  91. data/spec/support/v6.0/config/environments/development.rb +1 -1
  92. data/spec/support/v6.0/config/environments/production.rb +1 -1
  93. data/spec/support/v6.0/config/environments/test.rb +2 -2
  94. data/spec/support/v6.0/config/routes.rb +1 -1
  95. data/spec/support/v6.0/db/migrate/20190620000015_add_serialized_to_wizards.rb +9 -0
  96. data/spec/support/v6.0/db/schema.rb +5 -1
  97. data/spec/support/v6.0/sorbet_test_cases.rb +28 -56
  98. data/spec/support/v6.0/tmp/pids/.keep +0 -0
  99. data/spec/test_data/v5.0/expected_active_record_relation.rbi +0 -1
  100. data/spec/test_data/v5.0/expected_application_job.rbi +2 -2
  101. data/spec/test_data/v5.0/expected_award_house_point_hourglasses.rbi +2 -2
  102. data/spec/test_data/v5.0/expected_custom_application_job.rbi +21 -0
  103. data/spec/test_data/v5.0/expected_custom_application_mailer.rbi +6 -0
  104. data/spec/test_data/v5.0/expected_custom_award_house_point_hourglasses.rbi +21 -0
  105. data/spec/test_data/v5.0/expected_custom_daily_prophet_mailer.rbi +8 -0
  106. data/spec/test_data/v5.0/expected_custom_hogwarts_acceptance_mailer.rbi +21 -0
  107. data/spec/test_data/v5.0/expected_headmaster.rbi +24 -0
  108. data/spec/test_data/v5.0/expected_potion.rbi +12 -0
  109. data/spec/test_data/v5.0/expected_robe.rbi +12 -0
  110. data/spec/test_data/v5.0/expected_routes.rbi +4 -0
  111. data/spec/test_data/v5.0/expected_school.rbi +12 -0
  112. data/spec/test_data/v5.0/expected_spell/habtm_spell_books.rbi +24 -0
  113. data/spec/test_data/v5.0/expected_spell_book.rbi +21 -9
  114. data/spec/test_data/v5.0/expected_spell_book/habtm_spells.rbi +24 -0
  115. data/spec/test_data/v5.0/expected_squib.rbi +63 -0
  116. data/spec/test_data/v5.0/expected_subject/habtm_wizards.rbi +24 -0
  117. data/spec/test_data/v5.0/expected_wand.rbi +22 -10
  118. data/spec/test_data/v5.0/expected_wizard.rbi +121 -58
  119. data/spec/test_data/v5.0/expected_wizard/habtm_subjects.rbi +24 -0
  120. data/spec/test_data/v5.0/expected_wizard_wo_spellbook.rbi +121 -58
  121. data/spec/test_data/v5.1/expected_active_record_relation.rbi +0 -1
  122. data/spec/test_data/v5.1/expected_application_job.rbi +2 -2
  123. data/spec/test_data/v5.1/expected_award_house_point_hourglasses.rbi +2 -2
  124. data/spec/test_data/v5.1/expected_custom_application_job.rbi +21 -0
  125. data/spec/test_data/v5.1/expected_custom_application_mailer.rbi +6 -0
  126. data/spec/test_data/v5.1/expected_custom_award_house_point_hourglasses.rbi +21 -0
  127. data/spec/test_data/v5.1/expected_custom_daily_prophet_mailer.rbi +8 -0
  128. data/spec/test_data/v5.1/expected_custom_hogwarts_acceptance_mailer.rbi +21 -0
  129. data/spec/test_data/v5.1/expected_headmaster.rbi +24 -0
  130. data/spec/test_data/v5.1/expected_potion.rbi +12 -0
  131. data/spec/test_data/v5.1/expected_robe.rbi +12 -0
  132. data/spec/test_data/v5.1/expected_routes.rbi +4 -0
  133. data/spec/test_data/v5.1/expected_school.rbi +12 -0
  134. data/spec/test_data/v5.1/expected_spell/habtm_spell_books.rbi +24 -0
  135. data/spec/test_data/v5.1/expected_spell_book.rbi +21 -9
  136. data/spec/test_data/v5.1/expected_spell_book/habtm_spells.rbi +24 -0
  137. data/spec/test_data/v5.1/expected_squib.rbi +63 -0
  138. data/spec/test_data/v5.1/expected_subject/habtm_wizards.rbi +24 -0
  139. data/spec/test_data/v5.1/expected_wand.rbi +22 -10
  140. data/spec/test_data/v5.1/expected_wizard.rbi +121 -58
  141. data/spec/test_data/v5.1/expected_wizard/habtm_subjects.rbi +24 -0
  142. data/spec/test_data/v5.1/expected_wizard_wo_spellbook.rbi +121 -58
  143. data/spec/test_data/v5.2/expected_active_record_relation.rbi +0 -1
  144. data/spec/test_data/v5.2/expected_application_job.rbi +2 -2
  145. data/spec/test_data/v5.2/expected_attachment.rbi +24 -0
  146. data/spec/test_data/v5.2/expected_award_house_point_hourglasses.rbi +2 -2
  147. data/spec/test_data/v5.2/expected_blob.rbi +25 -1
  148. data/spec/test_data/v5.2/expected_custom_application_job.rbi +21 -0
  149. data/spec/test_data/v5.2/expected_custom_application_mailer.rbi +6 -0
  150. data/spec/test_data/v5.2/expected_custom_award_house_point_hourglasses.rbi +21 -0
  151. data/spec/test_data/v5.2/expected_custom_daily_prophet_mailer.rbi +8 -0
  152. data/spec/test_data/v5.2/expected_custom_hogwarts_acceptance_mailer.rbi +21 -0
  153. data/spec/test_data/v5.2/expected_headmaster.rbi +24 -0
  154. data/spec/test_data/v5.2/expected_potion.rbi +12 -0
  155. data/spec/test_data/v5.2/expected_robe.rbi +12 -0
  156. data/spec/test_data/v5.2/expected_routes.rbi +4 -0
  157. data/spec/test_data/v5.2/expected_school.rbi +12 -0
  158. data/spec/test_data/v5.2/expected_spell/habtm_spell_books.rbi +24 -0
  159. data/spec/test_data/v5.2/expected_spell_book.rbi +21 -9
  160. data/spec/test_data/v5.2/expected_spell_book/habtm_spells.rbi +24 -0
  161. data/spec/test_data/v5.2/expected_squib.rbi +89 -2
  162. data/spec/test_data/v5.2/expected_subject/habtm_wizards.rbi +24 -0
  163. data/spec/test_data/v5.2/expected_wand.rbi +22 -10
  164. data/spec/test_data/v5.2/expected_wizard.rbi +147 -60
  165. data/spec/test_data/v5.2/expected_wizard/habtm_subjects.rbi +24 -0
  166. data/spec/test_data/v5.2/expected_wizard_wo_spellbook.rbi +147 -60
  167. data/spec/test_data/v6.0/expected_active_record_relation.rbi +0 -1
  168. data/spec/test_data/v6.0/expected_application_job.rbi +2 -2
  169. data/spec/test_data/v6.0/expected_attachment.rbi +24 -0
  170. data/spec/test_data/v6.0/expected_award_house_point_hourglasses.rbi +2 -2
  171. data/spec/test_data/v6.0/expected_blob.rbi +25 -1
  172. data/spec/test_data/v6.0/expected_custom_application_job.rbi +21 -0
  173. data/spec/test_data/v6.0/expected_custom_application_mailer.rbi +6 -0
  174. data/spec/test_data/v6.0/expected_custom_award_house_point_hourglasses.rbi +21 -0
  175. data/spec/test_data/v6.0/expected_custom_daily_prophet_mailer.rbi +8 -0
  176. data/spec/test_data/v6.0/expected_custom_hogwarts_acceptance_mailer.rbi +21 -0
  177. data/spec/test_data/v6.0/expected_headmaster.rbi +24 -0
  178. data/spec/test_data/v6.0/expected_potion.rbi +12 -0
  179. data/spec/test_data/v6.0/expected_robe.rbi +12 -0
  180. data/spec/test_data/v6.0/expected_routes.rbi +4 -0
  181. data/spec/test_data/v6.0/expected_school.rbi +12 -0
  182. data/spec/test_data/v6.0/expected_spell/habtm_spell_books.rbi +24 -0
  183. data/spec/test_data/v6.0/expected_spell_book.rbi +21 -9
  184. data/spec/test_data/v6.0/expected_spell_book/habtm_spells.rbi +24 -0
  185. data/spec/test_data/v6.0/expected_squib.rbi +89 -2
  186. data/spec/test_data/v6.0/expected_subject/habtm_wizards.rbi +24 -0
  187. data/spec/test_data/v6.0/expected_wand.rbi +22 -10
  188. data/spec/test_data/v6.0/expected_wizard.rbi +147 -60
  189. data/spec/test_data/v6.0/expected_wizard/habtm_subjects.rbi +24 -0
  190. data/spec/test_data/v6.0/expected_wizard_wo_spellbook.rbi +147 -60
  191. data/spec/tstruct_comparable.rb +13 -0
  192. metadata +64 -15
  193. data/lib/bundled_rbi/parameters.rbi +0 -28
  194. data/lib/sorbet-rails/custom_types/boolean_string.rb +0 -42
  195. data/lib/sorbet-rails/custom_types/integer_string.rb +0 -45
  196. data/lib/sorbet-rails/rails_mixins/custom_params_methods.rb +0 -57
  197. data/spec/boolean_string_spec.rb +0 -59
  198. data/spec/custom_params_methods_spec.rb +0 -138
  199. data/spec/integer_string_spec.rb +0 -46
@@ -35,19 +35,19 @@ namespace :rails_rbi do
35
35
  File.write(file_path, inspector.format(SorbetRails::RoutesRbiFormatter.new))
36
36
  end
37
37
 
38
- desc "Copy custom rbis for ActionController::Parameters, pluck_to_struct, etc."
38
+ desc "Copy custom rbis for typed_params, pluck_to_struct, etc."
39
39
  task custom: :environment do
40
40
  copy_bundled_rbi('type_assert.rbi')
41
- copy_bundled_rbi('parameters.rbi')
42
41
  copy_bundled_rbi('pluck_to_tstruct.rbi')
43
42
  copy_bundled_rbi('typed_params.rbi')
43
+ copy_bundled_rbi('typed_enum.rbi')
44
44
 
45
45
  # These files were previously bundled_rbi but are now generated so this
46
46
  # is needed for backwards compatibility with anyone using `rails_rbi:custom`
47
47
  Rake::Task['rails_rbi:active_record'].invoke
48
48
  end
49
49
 
50
- desc "Generate rbis for rails mailers"
50
+ desc "Generate rbis for rails active_record base"
51
51
  task :active_record, [:root_dir] => :environment do |t, args|
52
52
  formatter = SorbetRails::ActiveRecordRbiFormatter.new
53
53
  FileUtils.mkdir_p(Rails.root.join("sorbet", "rails-rbi"))
@@ -97,17 +97,18 @@ namespace :rails_rbi do
97
97
  task helpers: :environment do |t, args|
98
98
  SorbetRails::Utils.rails_eager_load_all!
99
99
 
100
- if ApplicationController.methods.include?(:modules_for_helpers)
100
+ # API controller does not include ActionController::Helpers
101
+ if ApplicationController < ActionController::Helpers
101
102
  helpers = ApplicationController.modules_for_helpers([:all])
102
103
  end
103
104
 
104
105
  # If ApplicationController doesn't work or doesn't return any helpers,
105
106
  # try using ActionController::Base.
106
- if ActionController::Base.methods.include?(:modules_for_helpers) && (helpers.nil? || helpers.length == 0)
107
+ if ApplicationController < ActionController::Helpers && helpers.blank?
107
108
  helpers = ActionController::Base.modules_for_helpers([:all])
108
109
  end
109
110
 
110
- if helpers.nil? || helpers.length == 0
111
+ if helpers.blank?
111
112
  puts 'No helpers found.'
112
113
  else
113
114
  formatter = SorbetRails::HelperRbiFormatter.new(helpers)
@@ -129,7 +130,7 @@ namespace :rails_rbi do
129
130
  "mailers",
130
131
  "#{mailer_class.name.underscore}.rbi",
131
132
  )
132
- formatter = SorbetRails::MailerRbiFormatter.new(mailer_class)
133
+ formatter = ::SorbetRails.config.mailer_generator_class.new(mailer_class)
133
134
  FileUtils.mkdir_p(File.dirname(file_path))
134
135
  File.write(file_path, formatter.generate_rbi)
135
136
  end
@@ -147,7 +148,7 @@ namespace :rails_rbi do
147
148
  "jobs",
148
149
  "#{job_class.name.underscore}.rbi",
149
150
  )
150
- formatter = SorbetRails::JobRbiFormatter.new(job_class)
151
+ formatter = ::SorbetRails.config.job_generator_class.new(job_class)
151
152
  FileUtils.mkdir_p(File.dirname(file_path))
152
153
  File.write(file_path, formatter.generate_rbi)
153
154
  end
data/sorbet-rails.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{sorbet-rails}
3
- s.version = "0.6.5.1"
3
+ s.version = "0.7.4"
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"]
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
15
15
  `git ls-files -z`.split("\x0").select { |f| f.match(%r{^(test|spec|features)/}) }
16
16
  end
17
17
 
18
- s.add_dependency 'parlour', '~> 2.0'
18
+ s.add_dependency 'parlour', '>= 4.0.1'
19
19
  s.add_dependency 'sorbet-runtime', '>= 0.5'
20
20
  s.add_dependency 'sorbet-coerce', '>= 0.2.6'
21
21
  s.add_dependency 'method_source', '>= 0.9.2'
data/spec/bin/run_spec.sh CHANGED
@@ -30,5 +30,5 @@ if [[ -z $RAILS_VERSION ]]; then
30
30
  else
31
31
  echo "---- Run $RAILS_VERSION ----"
32
32
  RAILS_VERSION=$RAILS_VERSION bundle update
33
- RAILS_VERSION=$RAILS_VERSION DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bundle exec rake
33
+ RAILS_VERSION=$RAILS_VERSION DISABLE_DATABASE_ENVIRONMENT_CHECK=1 SBR_DEBUG_MODE=true bundle exec rake
34
34
  fi
@@ -180,6 +180,11 @@ def create_models
180
180
  blue: 2,
181
181
  }, _prefix: :color, _suffix: :eyes
182
182
 
183
+ serialize :owl_results, Hash
184
+ serialize :newt_subjects # no specific data type, uses the default YAML Object coder
185
+ serialize :pets, Array
186
+ serialize :patronus_characteristics, JSON
187
+
183
188
  has_one :wand
184
189
  has_many :spell_books
185
190
  # habtm which is optional at the db level
@@ -417,6 +422,17 @@ def create_migrations
417
422
  end
418
423
  RUBY
419
424
 
425
+ file "db/migrate/20190620000015_add_serialized_to_wizards.rb", <<~RUBY
426
+ class AddSerializedToWizards < #{migration_superclass}
427
+ def change
428
+ add_column :wizards, :owl_results, :text # Hash
429
+ add_column :wizards, :newt_subjects, :text # generic
430
+ add_column :wizards, :pets, :text # Array
431
+ add_column :wizards, :patronus_characteristics, :text # serialized as JSON, but not a JSON column type
432
+ end
433
+ end
434
+ RUBY
435
+
420
436
  end
421
437
 
422
438
  def create_mailers
@@ -305,6 +305,21 @@ T.assert_type!(Wizard::House::Hufflepuff, Wizard::House)
305
305
  T.assert_type!(Wizard::House::Ravenclaw, Wizard::House)
306
306
  T.assert_type!(Wizard::House::Slytherin, Wizard::House)
307
307
 
308
+ # Serialization
309
+ T.assert_type!(
310
+ wizard.owl_results,
311
+ T.nilable(T::Hash[T.untyped, T.untyped])
312
+ )
313
+ T.assert_type!(
314
+ wizard.newt_subjects,
315
+ T.nilable(T.any(T::Array[T.untyped], T::Boolean, Float, T::Hash[T.untyped, T.untyped], Integer, String))
316
+ )
317
+ T.assert_type!(
318
+ wizard.patronus_characteristics,
319
+ T.nilable(T.any(T::Array[T.untyped], T::Boolean, Float, T::Hash[String, T.untyped], Integer, String))
320
+ )
321
+ T.assert_type!(wizard.pets, T.nilable(T::Array[T.untyped]))
322
+
308
323
  # Mythical plugin
309
324
  T.assert_type!(Wand.mythicals, T::Array[Wand])
310
325
 
@@ -312,7 +327,10 @@ T.assert_type!(Wand.mythicals, T::Array[Wand])
312
327
  T.assert_type!(HogwartsAcceptanceMailer.notify(wizard), ActionMailer::MessageDelivery)
313
328
 
314
329
  # Jobs
315
- AwardHousePointHourglasses.perform_later(student: wizard, point: 10)
330
+ T.assert_type!(
331
+ AwardHousePointHourglasses.perform_later(student: wizard, point: 10),
332
+ AwardHousePointHourglasses
333
+ )
316
334
  AwardHousePointHourglasses.perform_now(student: wizard, point: 10)
317
335
  AwardHousePointHourglasses.
318
336
  set(queue: :daily, wait: 5.minutes).
@@ -321,7 +339,6 @@ AwardHousePointHourglasses.
321
339
  set(priority: 10, wait_until: Time.now.tomorrow).
322
340
  perform_later(student: wizard, point: 10)
323
341
 
324
-
325
342
  T.assert_type!(wizard.broom_nimbus?, T::Boolean)
326
343
  T.assert_type!(wizard.color_brown_eyes?, T::Boolean)
327
344
  T.assert_type!(wizard.quidditch_keeper?, T::Boolean)
@@ -361,44 +378,6 @@ params = ActionController::Parameters.new({
361
378
  typed_params = TypedParams[MyActionParams].new.extract!(params)
362
379
  T.assert_type!(typed_params, MyActionParams)
363
380
 
364
- # -- require_typed
365
- T.assert_type!(
366
- params.require_typed(:age, TA[Integer].new),
367
- Integer,
368
- )
369
- T.assert_type!(
370
- params.require_typed(:name, TA[String].new),
371
- String,
372
- )
373
- info = params.require_typed(:info, TA[ActionController::Parameters].new)
374
- T.assert_type!(info, ActionController::Parameters)
375
- T.assert_type!(
376
- info.require_typed(:friends, TA[T::Array[String]].new),
377
- T::Array[String],
378
- )
379
- # -- fetch_typed
380
- T.assert_type!(
381
- params.fetch_typed(:age, TA[Integer].new),
382
- Integer,
383
- )
384
- T.assert_type!(
385
- params.fetch_typed(:name, TA[String].new),
386
- String,
387
- )
388
- T.assert_type!(
389
- params.fetch_typed(:nonexistence, TA[String].new, ''),
390
- String,
391
- )
392
- T.assert_type!(
393
- params.fetch_typed(:nonexistence, TA[T.nilable(String)].new, nil),
394
- T.nilable(String),
395
- )
396
- T.assert_type!(
397
- params.fetch_typed(:nonexistence, TA[T::Array[Integer]].new, []),
398
- T::Array[Integer],
399
- )
400
-
401
-
402
381
  # -- pluck to tstruct
403
382
  class WizardStruct < T::Struct
404
383
  const :name, String
@@ -415,23 +394,16 @@ Wizard.all.pluck_to_tstruct(TA[WizardStruct].new).each do |row|
415
394
  T.assert_type!(row, WizardStruct)
416
395
  end
417
396
 
418
-
419
397
  # -- GeneratedUrlHelpers
420
398
  class TestHelper
421
399
  include GeneratedUrlHelpers
422
-
423
- # need to implement this for the url
424
- def default_url_options
425
- {
426
- protocol: 'http',
427
- host: 'localhost',
428
- port: 3000,
429
- }
430
- end
431
-
432
- def test_url_helper
433
- T.assert_type!(test_index_path, String)
434
- T.assert_type!(test_index_url, String)
435
- end
436
400
  end
437
- TestHelper.new.test_url_helper
401
+
402
+ T.assert_type!(TestHelper.new.test_index_path, String)
403
+ # need to set this config for _url methods
404
+ Rails.application.routes.default_url_options = {
405
+ protocol: 'http',
406
+ host: 'localhost',
407
+ port: 3000,
408
+ }
409
+ T.assert_type!(TestHelper.new.test_index_url, String)
@@ -1,5 +1,5 @@
1
1
  require 'rails_helper'
2
- require 'sorbet-rails/mailer_rbi_formatter'
2
+ require 'sorbet-rails/job_rbi_formatter'
3
3
 
4
4
  RSpec.describe SorbetRails::MailerRbiFormatter do
5
5
 
@@ -1,5 +1,6 @@
1
1
  require 'rails_helper'
2
2
  require 'sorbet-rails/model_rbi_formatter'
3
+ require 'tstruct_comparable'
3
4
 
4
5
  RSpec.describe SorbetRails::PluckToTStruct do
5
6
  let!(:harry) do
@@ -9,6 +10,18 @@ RSpec.describe SorbetRails::PluckToTStruct do
9
10
  )
10
11
  end
11
12
 
13
+ let!(:harrys_wand) do
14
+ Wand.create!(
15
+ wizard: harry,
16
+ core_type: :phoenix_feather,
17
+ wood_type: "Holly",
18
+ chosen_at_date: Date.parse('2019-09-01'),
19
+ chosen_at_time: Time.parse('2019-09-01T09:00:00Z'),
20
+ broken: true,
21
+ broken_at: Time.parse('2019-09-05T15:30:00Z'),
22
+ )
23
+ end
24
+
12
25
  let!(:hermione) do
13
26
  Wizard.create!(
14
27
  name: 'Hermione Granger',
@@ -16,31 +29,53 @@ RSpec.describe SorbetRails::PluckToTStruct do
16
29
  )
17
30
  end
18
31
 
32
+ let!(:hermiones_wand) do
33
+ Wand.create!(
34
+ wizard: hermione,
35
+ core_type: :phoenix_feather,
36
+ wood_type: "Vine",
37
+ chosen_at_date: Date.parse('2019-09-01'),
38
+ chosen_at_time: Time.parse('2019-09-01T09:00:00Z'),
39
+ broken: true,
40
+ broken_at: Time.parse('2019-09-05T15:30:00Z'),
41
+ )
42
+ end
43
+
19
44
  class WizardName < T::Struct
45
+ include TStructComparable
46
+
20
47
  const :name, String
48
+ end
21
49
 
22
- def ==(other)
23
- return false unless other.is_a?(self.class)
24
- name == other.name
25
- end
50
+ class WizardT < T::Struct
51
+ include TStructComparable
26
52
 
27
- def eql?(other)
28
- self == other
29
- end
53
+ const :name, String
54
+ const :house, String
30
55
  end
31
56
 
32
- class WizardT < T::Struct
57
+ class WizardWithWandT < T::Struct
58
+ include TStructComparable
59
+
33
60
  const :name, String
34
61
  const :house, String
62
+ const :wand_wood_type, String
63
+ end
35
64
 
36
- def ==(other)
37
- return false unless other.is_a?(self.class)
38
- name == other.name && house == other.house
39
- end
65
+ class WizardWithDefaultParentEmailT < T::Struct
66
+ include TStructComparable
40
67
 
41
- def eql?(other)
42
- self == other
43
- end
68
+ const :name, String
69
+ const :house, String
70
+ const :parent_email, String, default: "hagrid@hogwarts.com"
71
+ end
72
+
73
+ class WizardWithDefaultNilableParentEmailT < T::Struct
74
+ include TStructComparable
75
+
76
+ const :name, String
77
+ const :house, String
78
+ const :parent_email, T.nilable(String), default: "hagrid@hogwarts.com"
44
79
  end
45
80
 
46
81
  shared_examples 'pluck_to_tstruct' do |struct_type, expected_values|
@@ -55,6 +90,18 @@ RSpec.describe SorbetRails::PluckToTStruct do
55
90
  end
56
91
  end
57
92
 
93
+ shared_examples 'pluck_to_tstruct with associations' do |struct_type, associations, expected_values|
94
+ it 'plucks correctly from ActiveRecord model with associations' do
95
+ plucked = Wizard.joins(:wand).pluck_to_tstruct(TA[struct_type].new, associations: associations)
96
+ expect(plucked).to match_array(expected_values)
97
+ end
98
+
99
+ it 'plucks correctly from ActiveRecord relation with associations' do
100
+ plucked = Wizard.all.joins(:wand).pluck_to_tstruct(TA[struct_type].new, associations: associations)
101
+ expect(plucked).to match_array(expected_values)
102
+ end
103
+ end
104
+
58
105
  context 'pluck 1 attribute' do
59
106
  it_should_behave_like 'pluck_to_tstruct', WizardName, [
60
107
  WizardName.new(name: "Harry Potter"),
@@ -72,8 +119,60 @@ RSpec.describe SorbetRails::PluckToTStruct do
72
119
  context 'given a wrong type' do
73
120
  it 'should raise error' do
74
121
  expect {
75
- plucked = Wizard.pluck_to_tstruct(TA[String].new)
122
+ Wizard.pluck_to_tstruct(TA[String].new)
76
123
  }.to raise_error(SorbetRails::PluckToTStruct::UnexpectedType)
77
124
  end
78
125
  end
126
+
127
+ context "given associations mappings that don't exist in T::Struct" do
128
+ it 'should raise error' do
129
+ expect {
130
+ Wizard.pluck_to_tstruct(
131
+ TA[WizardWithWandT].new,
132
+ associations: { wood_type: "wands.wood_type" }
133
+ )
134
+ }.to raise_error(SorbetRails::PluckToTStruct::UnexpectedAssociations)
135
+ end
136
+ end
137
+
138
+ context 'pluck with associations' do
139
+ associations = { wand_wood_type: "wands.wood_type" }
140
+
141
+ expected = [
142
+ WizardWithWandT.new(name: "Harry Potter", house: "Gryffindor", wand_wood_type: "Holly"),
143
+ WizardWithWandT.new(name: "Hermione Granger", house: "Gryffindor", wand_wood_type: "Vine"),
144
+ ]
145
+
146
+ it_should_behave_like 'pluck_to_tstruct with associations', WizardWithWandT, associations, expected
147
+ end
148
+
149
+ context 'uses default value if prop type is not nilable and has default value' do
150
+ it_should_behave_like 'pluck_to_tstruct', WizardWithDefaultParentEmailT, [
151
+ WizardWithDefaultParentEmailT.new(
152
+ name: "Harry Potter",
153
+ house: "Gryffindor",
154
+ parent_email: "hagrid@hogwarts.com"
155
+ ),
156
+ WizardWithDefaultParentEmailT.new(
157
+ name: "Hermione Granger",
158
+ house: "Gryffindor",
159
+ parent_email: "hagrid@hogwarts.com"
160
+ ),
161
+ ]
162
+ end
163
+
164
+ context 'doesnt use default value if prop type is nilable even with a default value' do
165
+ it_should_behave_like 'pluck_to_tstruct', WizardWithDefaultNilableParentEmailT, [
166
+ WizardWithDefaultNilableParentEmailT.new(
167
+ name: "Harry Potter",
168
+ house: "Gryffindor",
169
+ parent_email: nil
170
+ ),
171
+ WizardWithDefaultNilableParentEmailT.new(
172
+ name: "Hermione Granger",
173
+ house: "Gryffindor",
174
+ parent_email: nil
175
+ ),
176
+ ]
177
+ end
79
178
  end
data/spec/rails_helper.rb CHANGED
@@ -36,7 +36,6 @@ RSpec.configure do |config|
36
36
  next if ENV["DISABLE_DATABASE_CLEANER"] == 'true'
37
37
  DatabaseCleaner.clean_with(:truncation, {
38
38
  pre_count: true,
39
- reset_ids: false,
40
39
  except: %w(ar_internal_metadata),
41
40
  })
42
41
  end
@@ -46,10 +45,7 @@ RSpec.configure do |config|
46
45
  example.run
47
46
  next
48
47
  end
49
- DatabaseCleaner.strategy = :truncation, {
50
- pre_count: true,
51
- reset_ids: false,
52
- }
48
+ DatabaseCleaner.strategy = :truncation, { pre_count: true }
53
49
  DatabaseCleaner.start
54
50
  example.run
55
51
  DatabaseCleaner.clean
@@ -70,7 +66,7 @@ end
70
66
  # File uses File::SEPARATOR by default but will also use File::ALT_SEPARATOR if it's not nil
71
67
  FILE_SEPARATOR_RE = Regexp.union(*[File::SEPARATOR, File::ALT_SEPARATOR].compact).freeze
72
68
 
73
- def expect_files(base_dir:, files:)
69
+ def expect_files(base_dir:, files:, expected_file_prefix: "expected")
74
70
  dirs = Set.new([base_dir])
75
71
  # if any files contain file separators then include those paths in our globbing as well
76
72
  files.each do |f|
@@ -92,7 +88,7 @@ def expect_files(base_dir:, files:)
92
88
  generated = File.read(file_path)
93
89
  # expect there is the same file in test_data folder
94
90
  path_from_dir = pathname.relative_path_from(base_dir).to_path
95
- expect_match_file(generated, "expected_#{path_from_dir}")
91
+ expect_match_file(generated, "#{expected_file_prefix}_#{path_from_dir}")
96
92
  path_from_dir
97
93
  end
98
94