sorbet-rails 0.6.5 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) 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 +60 -11
  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 +2 -2
  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/deprecation.rb +1 -0
  15. data/lib/sorbet-rails/gem_plugins/kaminari_plugin.rb +8 -0
  16. data/lib/sorbet-rails/helper_rbi_formatter.rb +1 -1
  17. data/lib/sorbet-rails/job_rbi_formatter.rb +75 -62
  18. data/lib/sorbet-rails/mailer_rbi_formatter.rb +40 -27
  19. data/lib/sorbet-rails/model_column_utils.rb +129 -0
  20. data/lib/sorbet-rails/model_plugins/active_record_assoc.rb +40 -1
  21. data/lib/sorbet-rails/model_plugins/active_record_attribute.rb +12 -112
  22. data/lib/sorbet-rails/model_plugins/active_record_querying.rb +2 -2
  23. data/lib/sorbet-rails/model_plugins/active_record_serialized_attribute.rb +68 -0
  24. data/lib/sorbet-rails/model_plugins/base.rb +6 -8
  25. data/lib/sorbet-rails/model_plugins/plugins.rb +3 -0
  26. data/lib/sorbet-rails/model_rbi_formatter.rb +3 -3
  27. data/lib/sorbet-rails/model_utils.rb +5 -2
  28. data/lib/sorbet-rails/rails_mixins/generated_url_helpers.rb +2 -3
  29. data/lib/sorbet-rails/rails_mixins/pluck_to_tstruct.rb +54 -7
  30. data/lib/sorbet-rails/railtie.rb +0 -2
  31. data/lib/sorbet-rails/routes_rbi_formatter.rb +6 -2
  32. data/lib/sorbet-rails/sorbet_utils.rb +152 -150
  33. data/lib/sorbet-rails/tasks/rails_rbi.rake +9 -8
  34. data/sorbet-rails.gemspec +2 -2
  35. data/spec/bin/run_spec.sh +1 -1
  36. data/spec/generators/rails-template.rb +16 -0
  37. data/spec/generators/sorbet_test_cases.rb +29 -57
  38. data/spec/job_rbi_formatter_spec.rb +1 -1
  39. data/spec/pluck_to_tstruct_spec.rb +115 -16
  40. data/spec/rails_helper.rb +2 -2
  41. data/spec/rake_rails_rbi_jobs_spec.rb +20 -0
  42. data/spec/rake_rails_rbi_mailers_spec.rb +21 -0
  43. data/spec/sorbet_spec.rb +5 -5
  44. data/spec/support/v5.0/Gemfile +1 -1
  45. data/spec/support/v5.0/Gemfile.lock +15 -14
  46. data/spec/support/v5.0/app/controllers/application_controller.rb +1 -1
  47. data/spec/support/v5.0/app/models/headmaster.rb +1 -1
  48. data/spec/support/v5.0/app/models/potion.rb +1 -1
  49. data/spec/support/v5.0/app/models/robe.rb +1 -1
  50. data/spec/support/v5.0/app/models/school.rb +1 -1
  51. data/spec/support/v5.0/app/models/spell.rb +1 -1
  52. data/spec/support/v5.0/app/models/subject.rb +1 -1
  53. data/spec/support/v5.0/app/models/wizard.rb +5 -0
  54. data/spec/support/v5.0/config/environments/development.rb +1 -1
  55. data/spec/support/v5.0/config/environments/production.rb +1 -1
  56. data/spec/support/v5.0/config/environments/test.rb +1 -1
  57. data/spec/support/v5.0/config/initializers/wrap_parameters.rb +1 -1
  58. data/spec/support/v5.0/config/routes.rb +1 -1
  59. data/spec/support/v5.0/db/migrate/20190620000015_add_serialized_to_wizards.rb +9 -0
  60. data/spec/support/v5.0/db/schema.rb +8 -4
  61. data/spec/support/v5.0/sorbet_test_cases.rb +29 -57
  62. data/spec/support/v5.1/Gemfile.lock +13 -12
  63. data/spec/support/v5.1/app/controllers/application_controller.rb +1 -1
  64. data/spec/support/v5.1/app/models/headmaster.rb +1 -1
  65. data/spec/support/v5.1/app/models/school.rb +1 -1
  66. data/spec/support/v5.1/app/models/wizard.rb +5 -0
  67. data/spec/support/v5.1/config/environments/production.rb +1 -1
  68. data/spec/support/v5.1/config/initializers/wrap_parameters.rb +1 -1
  69. data/spec/support/v5.1/config/routes.rb +1 -1
  70. data/spec/support/v5.1/db/migrate/20190620000015_add_serialized_to_wizards.rb +9 -0
  71. data/spec/support/v5.1/db/schema.rb +5 -1
  72. data/spec/support/v5.1/sorbet_test_cases.rb +29 -57
  73. data/spec/support/v5.2/Gemfile +1 -1
  74. data/spec/support/v5.2/Gemfile.lock +15 -14
  75. data/spec/support/v5.2/app/models/headmaster.rb +1 -1
  76. data/spec/support/v5.2/app/models/school.rb +1 -1
  77. data/spec/support/v5.2/app/models/wizard.rb +5 -0
  78. data/spec/support/v5.2/config/environments/development.rb +1 -1
  79. data/spec/support/v5.2/config/environments/production.rb +1 -1
  80. data/spec/support/v5.2/config/environments/test.rb +1 -1
  81. data/spec/support/v5.2/config/initializers/wrap_parameters.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 +29 -57
  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 +82 -80
  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 +29 -57
  98. data/spec/support/v6.0/tmp/pids/.keep +0 -0
  99. data/spec/test_data/v5.0/expected_application_job.rbi +2 -2
  100. data/spec/test_data/v5.0/expected_award_house_point_hourglasses.rbi +2 -2
  101. data/spec/test_data/v5.0/expected_custom_application_job.rbi +21 -0
  102. data/spec/test_data/v5.0/expected_custom_application_mailer.rbi +6 -0
  103. data/spec/test_data/v5.0/expected_custom_award_house_point_hourglasses.rbi +21 -0
  104. data/spec/test_data/v5.0/expected_custom_daily_prophet_mailer.rbi +8 -0
  105. data/spec/test_data/v5.0/expected_custom_hogwarts_acceptance_mailer.rbi +21 -0
  106. data/spec/test_data/v5.0/expected_headmaster.rbi +26 -2
  107. data/spec/test_data/v5.0/expected_internal_metadata.rbi +2 -2
  108. data/spec/test_data/v5.0/expected_potion.rbi +14 -2
  109. data/spec/test_data/v5.0/expected_robe.rbi +14 -2
  110. data/spec/test_data/v5.0/expected_routes.rbi +4 -0
  111. data/spec/test_data/v5.0/expected_schema_migration.rbi +2 -2
  112. data/spec/test_data/v5.0/expected_school.rbi +14 -2
  113. data/spec/test_data/v5.0/expected_spell.rbi +2 -2
  114. data/spec/test_data/v5.0/expected_spell/habtm_spell_books.rbi +26 -2
  115. data/spec/test_data/v5.0/expected_spell_book.rbi +23 -11
  116. data/spec/test_data/v5.0/expected_spell_book/habtm_spells.rbi +26 -2
  117. data/spec/test_data/v5.0/expected_squib.rbi +65 -2
  118. data/spec/test_data/v5.0/expected_subject.rbi +2 -2
  119. data/spec/test_data/v5.0/expected_subject/habtm_wizards.rbi +26 -2
  120. data/spec/test_data/v5.0/expected_wand.rbi +24 -12
  121. data/spec/test_data/v5.0/expected_wizard.rbi +123 -60
  122. data/spec/test_data/v5.0/expected_wizard/habtm_subjects.rbi +26 -2
  123. data/spec/test_data/v5.0/expected_wizard_wo_spellbook.rbi +123 -60
  124. data/spec/test_data/v5.1/expected_application_job.rbi +2 -2
  125. data/spec/test_data/v5.1/expected_award_house_point_hourglasses.rbi +2 -2
  126. data/spec/test_data/v5.1/expected_custom_application_job.rbi +21 -0
  127. data/spec/test_data/v5.1/expected_custom_application_mailer.rbi +6 -0
  128. data/spec/test_data/v5.1/expected_custom_award_house_point_hourglasses.rbi +21 -0
  129. data/spec/test_data/v5.1/expected_custom_daily_prophet_mailer.rbi +8 -0
  130. data/spec/test_data/v5.1/expected_custom_hogwarts_acceptance_mailer.rbi +21 -0
  131. data/spec/test_data/v5.1/expected_headmaster.rbi +26 -2
  132. data/spec/test_data/v5.1/expected_internal_metadata.rbi +2 -2
  133. data/spec/test_data/v5.1/expected_potion.rbi +14 -2
  134. data/spec/test_data/v5.1/expected_robe.rbi +14 -2
  135. data/spec/test_data/v5.1/expected_routes.rbi +4 -0
  136. data/spec/test_data/v5.1/expected_schema_migration.rbi +2 -2
  137. data/spec/test_data/v5.1/expected_school.rbi +14 -2
  138. data/spec/test_data/v5.1/expected_spell.rbi +2 -2
  139. data/spec/test_data/v5.1/expected_spell/habtm_spell_books.rbi +26 -2
  140. data/spec/test_data/v5.1/expected_spell_book.rbi +23 -11
  141. data/spec/test_data/v5.1/expected_spell_book/habtm_spells.rbi +26 -2
  142. data/spec/test_data/v5.1/expected_squib.rbi +65 -2
  143. data/spec/test_data/v5.1/expected_subject.rbi +2 -2
  144. data/spec/test_data/v5.1/expected_subject/habtm_wizards.rbi +26 -2
  145. data/spec/test_data/v5.1/expected_wand.rbi +24 -12
  146. data/spec/test_data/v5.1/expected_wizard.rbi +123 -60
  147. data/spec/test_data/v5.1/expected_wizard/habtm_subjects.rbi +26 -2
  148. data/spec/test_data/v5.1/expected_wizard_wo_spellbook.rbi +123 -60
  149. data/spec/test_data/v5.2/expected_application_job.rbi +2 -2
  150. data/spec/test_data/v5.2/expected_attachment.rbi +26 -2
  151. data/spec/test_data/v5.2/expected_award_house_point_hourglasses.rbi +2 -2
  152. data/spec/test_data/v5.2/expected_blob.rbi +27 -3
  153. data/spec/test_data/v5.2/expected_custom_application_job.rbi +21 -0
  154. data/spec/test_data/v5.2/expected_custom_application_mailer.rbi +6 -0
  155. data/spec/test_data/v5.2/expected_custom_award_house_point_hourglasses.rbi +21 -0
  156. data/spec/test_data/v5.2/expected_custom_daily_prophet_mailer.rbi +8 -0
  157. data/spec/test_data/v5.2/expected_custom_hogwarts_acceptance_mailer.rbi +21 -0
  158. data/spec/test_data/v5.2/expected_headmaster.rbi +26 -2
  159. data/spec/test_data/v5.2/expected_internal_metadata.rbi +2 -2
  160. data/spec/test_data/v5.2/expected_potion.rbi +14 -2
  161. data/spec/test_data/v5.2/expected_robe.rbi +14 -2
  162. data/spec/test_data/v5.2/expected_routes.rbi +4 -0
  163. data/spec/test_data/v5.2/expected_schema_migration.rbi +2 -2
  164. data/spec/test_data/v5.2/expected_school.rbi +14 -2
  165. data/spec/test_data/v5.2/expected_spell.rbi +2 -2
  166. data/spec/test_data/v5.2/expected_spell/habtm_spell_books.rbi +26 -2
  167. data/spec/test_data/v5.2/expected_spell_book.rbi +23 -11
  168. data/spec/test_data/v5.2/expected_spell_book/habtm_spells.rbi +26 -2
  169. data/spec/test_data/v5.2/expected_squib.rbi +91 -4
  170. data/spec/test_data/v5.2/expected_subject.rbi +2 -2
  171. data/spec/test_data/v5.2/expected_subject/habtm_wizards.rbi +26 -2
  172. data/spec/test_data/v5.2/expected_wand.rbi +24 -12
  173. data/spec/test_data/v5.2/expected_wizard.rbi +149 -62
  174. data/spec/test_data/v5.2/expected_wizard/habtm_subjects.rbi +26 -2
  175. data/spec/test_data/v5.2/expected_wizard_wo_spellbook.rbi +149 -62
  176. data/spec/test_data/v6.0/expected_application_job.rbi +2 -2
  177. data/spec/test_data/v6.0/expected_attachment.rbi +26 -2
  178. data/spec/test_data/v6.0/expected_award_house_point_hourglasses.rbi +2 -2
  179. data/spec/test_data/v6.0/expected_blob.rbi +27 -3
  180. data/spec/test_data/v6.0/expected_custom_application_job.rbi +21 -0
  181. data/spec/test_data/v6.0/expected_custom_application_mailer.rbi +6 -0
  182. data/spec/test_data/v6.0/expected_custom_award_house_point_hourglasses.rbi +21 -0
  183. data/spec/test_data/v6.0/expected_custom_daily_prophet_mailer.rbi +8 -0
  184. data/spec/test_data/v6.0/expected_custom_hogwarts_acceptance_mailer.rbi +21 -0
  185. data/spec/test_data/v6.0/expected_headmaster.rbi +26 -2
  186. data/spec/test_data/v6.0/expected_internal_metadata.rbi +2 -2
  187. data/spec/test_data/v6.0/expected_potion.rbi +14 -2
  188. data/spec/test_data/v6.0/expected_robe.rbi +14 -2
  189. data/spec/test_data/v6.0/expected_routes.rbi +4 -0
  190. data/spec/test_data/v6.0/expected_schema_migration.rbi +2 -2
  191. data/spec/test_data/v6.0/expected_school.rbi +14 -2
  192. data/spec/test_data/v6.0/expected_spell.rbi +2 -2
  193. data/spec/test_data/v6.0/expected_spell/habtm_spell_books.rbi +26 -2
  194. data/spec/test_data/v6.0/expected_spell_book.rbi +23 -11
  195. data/spec/test_data/v6.0/expected_spell_book/habtm_spells.rbi +26 -2
  196. data/spec/test_data/v6.0/expected_squib.rbi +91 -4
  197. data/spec/test_data/v6.0/expected_subject.rbi +2 -2
  198. data/spec/test_data/v6.0/expected_subject/habtm_wizards.rbi +26 -2
  199. data/spec/test_data/v6.0/expected_wand.rbi +24 -12
  200. data/spec/test_data/v6.0/expected_wizard.rbi +149 -62
  201. data/spec/test_data/v6.0/expected_wizard/habtm_subjects.rbi +26 -2
  202. data/spec/test_data/v6.0/expected_wizard_wo_spellbook.rbi +149 -62
  203. data/spec/tstruct_comparable.rb +13 -0
  204. metadata +61 -15
  205. data/lib/bundled_rbi/parameters.rbi +0 -28
  206. data/lib/sorbet-rails/custom_types/boolean_string.rb +0 -42
  207. data/lib/sorbet-rails/custom_types/integer_string.rb +0 -45
  208. data/lib/sorbet-rails/rails_mixins/custom_params_methods.rb +0 -57
  209. data/spec/boolean_string_spec.rb +0 -59
  210. data/spec/custom_params_methods_spec.rb +0 -138
  211. data/spec/integer_string_spec.rb +0 -46
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c81291953020b0718bba6afbbd60cf3e1d5a54ea08a85298357d0b46302437e8
4
- data.tar.gz: 5498a890ee281ddb68b3ca26e1656c5a48b2dddd9eee925383d5b5f90da7bdd5
3
+ metadata.gz: 04e15a0b91862375e9b3f853928e0f46a3c44e6176cb10ecb813a337ac50a45a
4
+ data.tar.gz: e6a5b10b3ac40664e0dce8a51c80795d8fd2e0c7ef4cc665c6ad169d6cd3f0dc
5
5
  SHA512:
6
- metadata.gz: f589fd032bea40225212f4375d93b07cea5ae5a5b1a743c763cd33b767458c552b50f88f11ff6fd3c391dc5ffd2f77ffcb08d34c5a27946d3ee9d5bbcd8fbffa
7
- data.tar.gz: 06a31971aa26f770322cc0ee3700607ad0da700851ae24dd2424605420cfe0552bad8b504509fd887af91ecced25defcdf597bd965c68933fe22eb3daf2bc960
6
+ metadata.gz: 3b3bddd29abcb7d6496ece148526fc204c9a7e2bd51f54a264d526e3c10bcacafe6118a49e9ad9c36f6edb9927fe80cd31de28ce4ddda035811f5d15520e2599
7
+ data.tar.gz: '05249019b6251b14ab95a71599c666710478c8468702546c0619ff9111021447d246ea47bc9f62bb038e04616e6b306647b4c669315ff59672b93a1682e6eb39'
@@ -1,4 +1,3 @@
1
- lib/sorbet/* linguist-generated
2
1
  spec/test_data/* linguist-generated
3
2
  spec/support/* linguist-generated
4
-
3
+ sorbet/* linguist-generated
data/.gitignore CHANGED
@@ -79,6 +79,7 @@ build-iPhoneSimulator/
79
79
  /spec/support/**/config/credentials.yml.enc
80
80
 
81
81
  # Ignore sorbet generated files
82
+ /sorbet/rbi/hidden-definitions/
82
83
  /spec/support/**/sorbet/*
83
84
  /spec/support/**/sorbet/**/*
84
85
 
@@ -93,7 +94,7 @@ build-iPhoneSimulator/
93
94
  *.swl
94
95
  *.swk
95
96
 
96
- lib/sorbet/rbi/hidden-definitions/errors.txt
97
+ sorbet/rbi/hidden-definitions/errors.txt
97
98
  .vscode
98
99
 
99
100
  .rake_tasks~
@@ -16,7 +16,7 @@ rvm:
16
16
  matrix:
17
17
  include:
18
18
  - rvm: 2.5
19
- env: RAILS_VERSION=5.2 SORBET_VERSION=0.5.5400
19
+ env: RAILS_VERSION=5.2 SORBET_VERSION=0.5.6210
20
20
  exclude:
21
21
  - rvm: 2.3
22
22
  env: RAILS_VERSION=6.0
data/README.md CHANGED
@@ -9,7 +9,7 @@ This gem adds a few Rake tasks to generate Ruby Interface (RBI) files for dynami
9
9
 
10
10
  `sorbet-rails` supports Rails 5+ or later.
11
11
 
12
- **Notice**: we no longer supports Rails 4.2. [Version 0.5.6](https://github.com/chanzuckerberg/sorbet-rails/releases/tag/v0.5.6) is the last version supporting Rails 4.2.
12
+ **Notice**: we no longer support Rails 4.2. [Version 0.5.6](https://github.com/chanzuckerberg/sorbet-rails/releases/tag/v0.5.6) is the last version supporting Rails 4.2.
13
13
 
14
14
  ## Initial Setup
15
15
 
@@ -94,6 +94,28 @@ Wizard.pluck_to_tstruct(TA[WizardStruct].new) # T::Array[WizardStruct]
94
94
  Wizard.all.pluck_to_tstruct(TA[WizardStruct].new) # T::Array[WizardStruct]
95
95
  ```
96
96
 
97
+ You can also pass a keyword argument called `associations` that represents a mapping of a `T::Struct`'s keys to an associated table's columns.
98
+ ```ruby
99
+ # -- API
100
+ Arel.pluck_to_tstruct(TA[ <TStructSubclass> ].new, associations: < Hash<Symbol, String> >)
101
+
102
+ # -- example
103
+ class WizardWithWandStruct < T::Struct
104
+ const :name, String
105
+ const :house, T.nilable(String)
106
+ const :wand_wood_type, String
107
+ end
108
+
109
+ Wizard.joins(:wand).pluck_to_tstruct(
110
+ TA[WizardWithWandStruct].new,
111
+ associations: { wand_wood_type: "wands.wood_type" }
112
+ )
113
+ Wizard.all.joins(:wand).pluck_to_tstruct(
114
+ TA[WizardWithWandStruct].new,
115
+ associations: { wand_wood_type: "wands.wood_type" }
116
+ )
117
+ ````
118
+
97
119
  This method is based on [pluck_to_hash](https://github.com/girishso/pluck_to_hash) gem.
98
120
 
99
121
  #### `typed_enum` instead of `enum`
@@ -114,12 +136,14 @@ ie.
114
136
  Will generate this enum:
115
137
 
116
138
  ```ruby
117
- class Wizard::House < T::Enum
118
- enums do
119
- Gryffindor = new('Gryffindor')
120
- Hufflepuff = new('Hufflepuff')
121
- Ravenclaw = new('Ravenclaw')
122
- Slytherin = new('Slytherin')
139
+ class Wizard
140
+ class House < T::Enum
141
+ enums do
142
+ Gryffindor = new('Gryffindor')
143
+ Hufflepuff = new('Hufflepuff')
144
+ Ravenclaw = new('Ravenclaw')
145
+ Slytherin = new('Slytherin')
146
+ end
123
147
  end
124
148
  end
125
149
  ```
@@ -412,11 +436,13 @@ If you run into the following issue when running rspec, it's likely because you'
412
436
  if you're really stuck.
413
437
  ```
414
438
 
415
- ## Extending Model Generation Task with Custom Plugins
439
+ ## Extending RBI Generation logic
440
+
441
+ ### Extending Model Generation Task with Custom Plugins
416
442
 
417
443
  `sorbet-rails` support a customizable plugin system that you can use to generate additional RBI for each model. This will be useful to generate RBI for methods dynamically added by gems or private concerns. If you write plugins for public gems, please feel free to contribute it to this repo.
418
444
 
419
- ### Defining a Custom `ModelPlugin`
445
+ #### Defining a Custom `ModelPlugin`
420
446
 
421
447
  A custom plugin should be a subclass of `SorbetRails::ModelPlugins::Base`. Each plugin would implement a `generate(root)` method that generate additional rbi for the model.
422
448
 
@@ -465,14 +491,14 @@ It is also recommended to check if the generated methods are detected by `sorbet
465
491
 
466
492
  Check out the [plugins](https://github.com/chanzuckerberg/sorbet-rails/tree/master/lib/sorbet-rails/model_plugins) written for `sorbet-rails`'s own model RBI generation logic for examples.
467
493
 
468
- ### Registering new plugins
494
+ #### Registering new plugins
469
495
  You can register your plugins in an initializer:
470
496
  ```ruby
471
497
  # -- config/initializers/sorbet_rails.rb
472
498
  SorbetRails::ModelRbiFormatter.register_plugin(MyCustomPlugin)
473
499
  ```
474
500
 
475
- ### Enabling built-in plugins
501
+ #### Enabling built-in plugins
476
502
 
477
503
  sorbet-rails comes with a handful of gem plugins that can be enabled in an initializer. You can pass enabled gem plugins to `config.enabled_gem_plugins`, like so:
478
504
 
@@ -508,6 +534,29 @@ You can also configure the core model plugins if needed. The default plugins are
508
534
  [active_flag]: https://github.com/kenn/active_flag
509
535
  [Paperclip]: https://github.com/thoughtbot/paperclip
510
536
 
537
+ ### Customize Generation Class
538
+
539
+ For mailer and job rbi generation, you can customize the logic by
540
+ setting the generation class in the config:
541
+
542
+ ```ruby
543
+ SorbetRails.configure do |config|
544
+ config.job_generator_class = CustomJobRbiGenerator
545
+ config.mailer_generator_class = CustomMailerRbiGenerator
546
+ end
547
+ ```
548
+
549
+ The custom generator can subclass the [provided generators](lib/bundled_rbi/customizabel_rbi_formatter.rbi) and override the populate_rbi method. For example:
550
+
551
+ ```ruby
552
+ class CustomJobRbiGenerator < SorbetRails::JobRbiFormatter
553
+ def populate_rbi
554
+ rbi_generator.root.add_comment("== Custom Generator ==")
555
+ super
556
+ end
557
+ end
558
+ ```
559
+
511
560
  ## Contributing
512
561
 
513
562
  Contributions and ideas are welcome! Please see [our contributing guide](CONTRIBUTING.md) and don't hesitate to open an issue or send a pull request to improve the functionality of this gem.
data/Rakefile CHANGED
@@ -20,9 +20,9 @@ namespace :update_spec do
20
20
  task :v6_0 do |t, args|
21
21
  Bundler.with_clean_env do
22
22
  FileUtils.rm_rf 'spec/support/v6.0' if File.directory?('spec/support/v6.0')
23
- system("gem install rails -v 6.0.0")
24
- system("rails _6.0.0_ -v")
25
- system("RAILS_VERSION='6.0' rails _6.0.0_ new --template spec/generators/rails-template.rb spec/support/v6.0 --skip-javascript --skip-action-cable --skip-test --skip-sprockets --skip-spring --skip-bootsnap --skip-listen")
23
+ system("gem install rails -v 6.0.3")
24
+ system("rails _6.0.3_ -v")
25
+ system("RAILS_VERSION='6.0' rails _6.0.3_ new --template spec/generators/rails-template.rb spec/support/v6.0 --skip-javascript --skip-action-cable --skip-test --skip-sprockets --skip-spring --skip-bootsnap --skip-listen")
26
26
  end
27
27
  end
28
28
 
@@ -0,0 +1,29 @@
1
+ # typed: strong
2
+ module SorbetRails
3
+ class MailerRbiFormatter
4
+ extend T::Sig
5
+ Parameter = ::Parlour::RbiGenerator::Parameter
6
+
7
+ sig { returns(T.class_of(ActionMailer::Base)) }
8
+ attr_reader :mailer_class
9
+
10
+ sig { returns(Parlour::RbiGenerator) }
11
+ def rbi_generator; end
12
+
13
+ sig { void }
14
+ def populate_rbi; end
15
+ end
16
+
17
+ class JobRbiFormatter
18
+ Parameter = ::Parlour::RbiGenerator::Parameter
19
+
20
+ sig { returns(Parlour::RbiGenerator) }
21
+ attr_reader :rbi_generator
22
+
23
+ sig { returns(T.class_of(ActiveJob::Base)) }
24
+ attr_reader :job_class
25
+
26
+ sig { void }
27
+ def populate_rbi; end
28
+ end
29
+ end
@@ -6,10 +6,11 @@ module SorbetRails::PluckToTStruct
6
6
  type_parameters(:U).
7
7
  params(
8
8
  ta_struct: ITypeAssert[T.type_parameter(:U)],
9
+ associations: T::Hash[Symbol, String],
9
10
  ).
10
11
  returns(T::Array[T.type_parameter(:U)])
11
12
  }
12
- def pluck_to_tstruct(ta_struct, &blk); end
13
+ def pluck_to_tstruct(ta_struct, associations: {}); end
13
14
  end
14
15
 
15
16
  class ActiveRecord::Base
@@ -0,0 +1,7 @@
1
+ # typed: strong
2
+ module ActiveRecord::Enum
3
+
4
+ sig { params(definitions: T::Hash[Symbol, T.untyped]).void }
5
+ def typed_enum(definitions); end
6
+
7
+ end
@@ -1,11 +1,9 @@
1
- # typed: strong
1
+ # typed: strict
2
2
  module SorbetRails
3
3
  if defined?(Rails)
4
4
  require 'sorbet-rails/railtie'
5
5
  require 'sorbet-rails/model_rbi_formatter'
6
6
  require 'sorbet-rails/type_assert/type_assert'
7
- require 'sorbet-rails/custom_types/integer_string'
8
- require 'sorbet-rails/custom_types/boolean_string'
9
7
  require 'sorbet-rails/typed_params'
10
8
  end
11
9
  end
@@ -12,7 +12,7 @@ class SorbetRails::ActiveRecordRbiFormatter
12
12
 
13
13
  parlour = T.let(Parlour::RbiGenerator.new, Parlour::RbiGenerator)
14
14
 
15
- parlour.root.add_comments([
15
+ parlour.root.add_comment([
16
16
  'This is an autogenerated file for Rails\' ActiveRecord.',
17
17
  'Please rerun bundle exec rake rails_rbi:active_record to regenerate.'
18
18
  ])
@@ -31,7 +31,7 @@ class SorbetRails::ActiveRecordRbiFormatter
31
31
 
32
32
  parlour = T.let(Parlour::RbiGenerator.new, Parlour::RbiGenerator)
33
33
 
34
- parlour.root.add_comments([
34
+ parlour.root.add_comment([
35
35
  'This is an autogenerated file for Rails\' ActiveRecord.',
36
36
  'Please rerun bundle exec rake rails_rbi:active_record to regenerate.'
37
37
  ])
@@ -1,4 +1,7 @@
1
1
  # typed: true
2
+ require("sorbet-rails/mailer_rbi_formatter")
3
+ require("sorbet-rails/job_rbi_formatter")
4
+
2
5
  module SorbetRails
3
6
  class << self
4
7
  extend T::Sig
@@ -37,6 +40,12 @@ module SorbetRails
37
40
  sig { returns(T::Array[String]) }
38
41
  attr_accessor :extra_helper_includes
39
42
 
43
+ sig { returns(T.class_of(SorbetRails::JobRbiFormatter))}
44
+ attr_accessor :job_generator_class
45
+
46
+ sig { returns(T.class_of(SorbetRails::MailerRbiFormatter))}
47
+ attr_accessor :mailer_generator_class
48
+
40
49
  sig { void }
41
50
  def initialize
42
51
  @enabled_gem_plugins = []
@@ -45,6 +54,7 @@ module SorbetRails
45
54
  :active_record_named_scope,
46
55
  :active_record_querying,
47
56
  :active_relation_where_not,
57
+ :active_record_serialized_attribute,
48
58
  :active_record_attribute,
49
59
  :active_record_assoc,
50
60
  :custom_finder_methods,
@@ -52,6 +62,8 @@ module SorbetRails
52
62
  ]
53
63
  @enabled_model_plugins << :active_storage_methods if defined?(T.unsafe(ActiveStorage))
54
64
  @extra_helper_includes = []
65
+ @mailer_generator_class = SorbetRails::MailerRbiFormatter
66
+ @job_generator_class = SorbetRails::JobRbiFormatter
55
67
  end
56
68
 
57
69
  sig { returns(T::Array[Symbol]) }
@@ -12,6 +12,9 @@ class ActiveRecord::Base < Object
12
12
  sig { returns(T::Hash[String, T.untyped]) }
13
13
  def self.columns_hash; end
14
14
 
15
+ sig { params(column_name: String).returns(T.nilable(T.any(ActiveModel::Type::Value, ActiveRecord::Type::Serialized))) }
16
+ def self.type_for_attribute(column_name); end
17
+
15
18
  sig { returns(T::Hash[String, T.untyped]) }
16
19
  def self.reflections; end
17
20
 
@@ -1,3 +1,4 @@
1
+ # typed: false
1
2
  require 'active_support/deprecation'
2
3
 
3
4
  module SorbetRails
@@ -35,5 +35,13 @@ class KaminariPlugin < SorbetRails::ModelPlugins::Base
35
35
  Parameter.new('num', type: 'Integer')
36
36
  ],
37
37
  )
38
+
39
+ # https://github.com/kaminari/kaminari/blob/c5186f5d9b7f23299d115408e62047447fd3189d/kaminari-core/lib/kaminari/models/configuration_methods.rb#L19
40
+ model_class_rbi = root.create_class(model_class_name)
41
+ model_class_rbi.create_method(
42
+ "default_per_page",
43
+ return_type: "Integer",
44
+ class_method: true,
45
+ )
38
46
  end
39
47
  end
@@ -16,7 +16,7 @@ class SorbetRails::HelperRbiFormatter
16
16
  def generate_rbi
17
17
  puts "-- Generate sigs for helpers --"
18
18
 
19
- @parlour.root.add_comments([
19
+ @parlour.root.add_comment([
20
20
  'This is an autogenerated file for Rails helpers.',
21
21
  'Please rerun bundle exec rake rails_rbi:helpers to regenerate.'
22
22
  ])
@@ -2,74 +2,87 @@
2
2
  require('parlour')
3
3
  require('sorbet-rails/sorbet_utils.rb')
4
4
 
5
- class SorbetRails::JobRbiFormatter
6
- extend T::Sig
5
+ module SorbetRails
6
+ class JobRbiFormatter
7
+ extend T::Sig
7
8
 
8
- Parameter = ::Parlour::RbiGenerator::Parameter
9
+ Parameter = ::Parlour::RbiGenerator::Parameter
9
10
 
10
- sig { params(job_class: T.class_of(ActiveJob::Base)).void }
11
- def initialize(job_class)
12
- @job_class = T.let(job_class, T.class_of(ActiveJob::Base))
13
- @parlour = T.let(Parlour::RbiGenerator.new, Parlour::RbiGenerator)
14
- end
11
+ sig { returns(Parlour::RbiGenerator) }
12
+ attr_reader :rbi_generator
13
+
14
+ sig { returns(T.class_of(ActiveJob::Base)) }
15
+ attr_reader :job_class
15
16
 
16
- sig {returns(String)}
17
- def generate_rbi
18
- puts "-- Generate sigs for mailer #{@job_class.name} --"
17
+ sig { params(job_class: T.class_of(ActiveJob::Base)).void }
18
+ def initialize(job_class)
19
+ @job_class = T.let(job_class, T.class_of(ActiveJob::Base))
20
+ @rbi_generator = T.let(Parlour::RbiGenerator.new, Parlour::RbiGenerator)
21
+ end
19
22
 
20
- @parlour.root.add_comments([
21
- 'This is an autogenerated file for Rails\' jobs.',
22
- 'Please rerun bundle exec rake rails_rbi:jobs to regenerate.'
23
- ])
23
+ sig { void }
24
+ def populate_rbi
25
+ @rbi_generator.root.add_comment([
26
+ 'This is an autogenerated file for Rails\' jobs.',
27
+ 'Please rerun bundle exec rake rails_rbi:jobs to regenerate.'
28
+ ])
24
29
 
25
- @parlour.root.create_class(@job_class.name) do |job_rbi|
26
- method_def = @job_class.instance_method(:perform)
27
- parameters = SorbetRails::SorbetUtils.parameters_from_method_def(method_def)
28
- job_rbi.create_method(
29
- "perform_later",
30
- parameters: parameters,
31
- class_method: true,
32
- )
33
- job_rbi.create_method(
34
- "perform_now",
35
- parameters: parameters,
36
- class_method: true,
37
- )
38
- # Override the signature for "set" so that we can support it
39
- # At run-time, this method returns a `ActiveJob::ConfiguredJob` but
40
- # we fake the signature to return "T.self_class" so that
41
- # sorbet can type-check when `perform_later` is called on it
42
- # See: https://guides.rubyonrails.org/active_job_basics.html#enqueue-the-job
43
- job_rbi.create_method(
44
- "set",
45
- # Documentation: https://api.rubyonrails.org/classes/ActiveJob/Core/ClassMethods.html
46
- parameters: [
47
- Parameter.new(
48
- "wait:",
49
- type: "T.nilable(ActiveSupport::Duration)",
50
- default: "nil",
51
- ),
52
- Parameter.new(
53
- "wait_until:",
54
- type: "T.nilable(T.any(ActiveSupport::TimeWithZone, Date, Time))",
55
- default: "nil",
56
- ),
57
- Parameter.new(
58
- "queue:",
59
- type: "T.nilable(T.any(String, Symbol))",
60
- default: "nil",
61
- ),
62
- Parameter.new(
63
- "priority:",
64
- type: "T.nilable(Integer)",
65
- default: "nil",
66
- ),
67
- ],
68
- return_type: "T.self_type",
69
- class_method: true,
70
- )
30
+ @rbi_generator.root.create_class(T.must(@job_class.name)) do |job_rbi|
31
+ method_def = @job_class.instance_method(:perform)
32
+ parameters = SorbetRails::SorbetUtils.parameters_from_method_def(method_def)
33
+ job_rbi.create_method(
34
+ "perform_later",
35
+ parameters: parameters,
36
+ class_method: true,
37
+ return_type: @job_class.name,
38
+ )
39
+ job_rbi.create_method(
40
+ "perform_now",
41
+ parameters: parameters,
42
+ class_method: true,
43
+ return_type: @job_class.name,
44
+ )
45
+ # Override the signature for "set" so that we can support it
46
+ # At run-time, this method returns a `ActiveJob::ConfiguredJob` but
47
+ # we fake the signature to return "T.self_class" so that
48
+ # sorbet can type-check when `perform_later` is called on it
49
+ # See: https://guides.rubyonrails.org/active_job_basics.html#enqueue-the-job
50
+ job_rbi.create_method(
51
+ "set",
52
+ # Documentation: https://api.rubyonrails.org/classes/ActiveJob/Core/ClassMethods.html
53
+ parameters: [
54
+ Parameter.new(
55
+ "wait:",
56
+ type: "T.nilable(ActiveSupport::Duration)",
57
+ default: "nil",
58
+ ),
59
+ Parameter.new(
60
+ "wait_until:",
61
+ type: "T.nilable(T.any(ActiveSupport::TimeWithZone, Date, Time))",
62
+ default: "nil",
63
+ ),
64
+ Parameter.new(
65
+ "queue:",
66
+ type: "T.nilable(T.any(String, Symbol))",
67
+ default: "nil",
68
+ ),
69
+ Parameter.new(
70
+ "priority:",
71
+ type: "T.nilable(Integer)",
72
+ default: "nil",
73
+ ),
74
+ ],
75
+ return_type: "T.self_type",
76
+ class_method: true,
77
+ )
78
+ end
71
79
  end
72
80
 
73
- @parlour.rbi
81
+ sig { returns(String) }
82
+ def generate_rbi
83
+ puts "-- Generate sigs for mailer #{@job_class.name} --"
84
+ populate_rbi
85
+ @rbi_generator.rbi
86
+ end
74
87
  end
75
88
  end