sorbet-rails 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dfa9a71f1119781cfb7095483dbbbab875f51f5562c475d77d38aabe1d97267f
4
- data.tar.gz: 80d6b0e7cc740a65db4678213bd6f1e6d34a81852a516854afedd0ad407e5866
3
+ metadata.gz: 3878883de9a212a28dbbd5f11aa6a1d6a0f58fd4840b26d958942b90b4bf802d
4
+ data.tar.gz: 3367e7a6ad7519c10c3a160372b505a605ccefd2e4437ae86b275b1b01c5df4c
5
5
  SHA512:
6
- metadata.gz: fd11e9d0f44405b205b56a9af096c8bd7814d03b7c1d7123e9f8be8803d6be06452a155c2950386d9de059b4ede03e6d636841e139b01af8fab8c9112b6a3cb8
7
- data.tar.gz: 69e086df0c72d95ffc54c87ee533b0b6833dcf66e70d434a8f0652a84f03c22a737c3cff180f2df06c620e4a3679740bde7774ddd0377575df93f5b6a4ed934c
6
+ metadata.gz: 76b1d238246210fe8d3735dbf216c0cb89529550b9364204571d9d4aed878f42c3a998bb48eaeaa8eb156c2cca758cf4535fc95ae445ef2c6c192b58dbc3bdd0
7
+ data.tar.gz: 97979cd91324b862e3ae0c0f9c53bd443613b1dfa0b8701680c0055d382057a6cdf45110183a43b19714b93b4d2ddedd9f7ba42ee4192f3d9c6597c8b4ddf45f
data/README.md CHANGED
@@ -225,11 +225,38 @@ Check out the [plugins](https://github.com/chanzuckerberg/sorbet-rails/tree/mast
225
225
 
226
226
  ### Registering new plugins
227
227
  You can register your plugins in an initializer:
228
- ```
228
+ ```ruby
229
229
  # -- config/initializers/sorbet_rails.rb
230
230
  SorbetRails::ModelRbiFormatter.register_plugin(MyCustomPlugin)
231
231
  ```
232
- The default plugins and other customizations are defined [here](https://github.com/chanzuckerberg/sorbet-rails/blob/master/lib/sorbet-rails/model_plugins/plugins.rb).
232
+
233
+ ### Enabling built-in plugins
234
+
235
+ 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:
236
+
237
+ ```ruby
238
+ # -- config/initializers/sorbet_rails.rb
239
+ SorbetRails.configure do |config|
240
+ config.enabled_gem_plugins = [
241
+ :kaminari
242
+ ]
243
+ end
244
+ ```
245
+
246
+ These are the currently-supported gems and their symbolized names:
247
+
248
+ | Gem | Symbol |
249
+ |--------------|----------------|
250
+ | [Kaminari] | `:kaminari` |
251
+ | [PgSearch] | `:pg_search` |
252
+ | [FriendlyId] | `:friendly_id` |
253
+
254
+ You can also configure the core model plugins if needed. The default plugins are defined in the [config](https://github.com/chanzuckerberg/sorbet-rails/blob/master/lib/sorbet-rails/lib/sorbet-rails/config.rb). For the full list of plugin symbols, check out [here](https://github.com/chanzuckerberg/sorbet-rails/blob/master/lib/sorbet-rails/model_plugins/plugins.rb).
255
+
256
+
257
+ [Kaminari]: https://github.com/kaminari/kaminari
258
+ [PgSearch]: https://github.com/Casecommons/pg_search
259
+ [FriendlyId]: https://github.com/norman/friendly_id
233
260
 
234
261
  ## Contributing
235
262
 
@@ -0,0 +1,58 @@
1
+ # typed: true
2
+ module SorbetRails
3
+ class << self
4
+ extend T::Sig
5
+
6
+ sig { params(blk: T.proc.params(arg0: Config).void).void }
7
+ def configure(&blk)
8
+ yield config
9
+
10
+ # After user has configured, register any plugins the user has added to
11
+ # their configuration.
12
+ register_configured_plugins
13
+ end
14
+
15
+ sig { returns(Config) }
16
+ def config
17
+ @_config ||= Config.new
18
+ end
19
+
20
+ sig { void }
21
+ def register_configured_plugins
22
+ config.enabled_plugins.each do |plugin_name|
23
+ SorbetRails::ModelRbiFormatter.register_plugin_by_name(plugin_name)
24
+ end
25
+ end
26
+ end
27
+
28
+ class Config
29
+ extend T::Sig
30
+
31
+ sig { returns(T::Array[Symbol]) }
32
+ attr_accessor :enabled_gem_plugins
33
+
34
+ sig { returns(T::Array[Symbol]) }
35
+ attr_accessor :enabled_model_plugins
36
+
37
+ sig { void }
38
+ def initialize
39
+ @enabled_gem_plugins = []
40
+ @enabled_model_plugins = [
41
+ :active_record_enum,
42
+ :active_record_named_scope,
43
+ :active_record_querying,
44
+ :active_relation_where_not,
45
+ :active_record_attribute,
46
+ :active_record_assoc,
47
+ :active_record_finder_methods,
48
+ :custom_finder_methods,
49
+ :enumerable_collections,
50
+ ]
51
+ end
52
+
53
+ sig { returns(T::Array[Symbol]) }
54
+ def enabled_plugins
55
+ @enabled_model_plugins + @enabled_gem_plugins
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,14 @@
1
+ # typed: strict
2
+ class FriendlyIdPlugin < SorbetRails::ModelPlugins::Base
3
+ sig { implementation.params(root: Parlour::RbiGenerator::Namespace).void }
4
+ def generate(root)
5
+ return unless @model_class.singleton_class.included_modules.include?(::FriendlyId)
6
+
7
+ # Friendly method definition
8
+ # https://github.com/norman/friendly_id/blob/53f1ca01c5d9e71b62812028ff009fd138fd41b5/lib/friendly_id/base.rb#L216
9
+ add_relation_query_method(
10
+ root,
11
+ 'friendly'
12
+ )
13
+ end
14
+ end
@@ -2,12 +2,12 @@
2
2
  class KaminariPlugin < SorbetRails::ModelPlugins::Base
3
3
  # Kaminari generates a dynamic `page` method on ActiveRecord relations.
4
4
  # https://github.com/kaminari/kaminari/blob/c5186f5d9b7f23299d115408e62047447fd3189d/kaminari-activerecord/lib/kaminari/activerecord/active_record_model_extension.rb#L15
5
- sig { params(root: Parlour::RbiGenerator::Namespace).void.implementation }
5
+ sig { implementation.params(root: Parlour::RbiGenerator::Namespace).void }
6
6
  def generate(root)
7
- return unless @model_class.include?(Kaminari::ActiveRecordModelExtension)
7
+ return unless @model_class.include?(::Kaminari::ActiveRecordModelExtension)
8
8
 
9
9
  # Get the configured Kaminari page method name, or fall back to 'page' if necessary.
10
- page_method = T.unsafe(Kaminari).config.page_method_name || 'page'
10
+ page_method = T.unsafe(::Kaminari).config.page_method_name || 'page'
11
11
 
12
12
  add_relation_query_method(
13
13
  root,
@@ -0,0 +1,15 @@
1
+ # typed: strict
2
+ class PgSearchPlugin < SorbetRails::ModelPlugins::Base
3
+ # If you include PgSearch::Model, the class implicitly gets methods from
4
+ # PgSearch::Model::ClassMethods.
5
+ sig { implementation.params(root: Parlour::RbiGenerator::Namespace).void }
6
+ def generate(root)
7
+ return unless @model_class.include?(::PgSearch::Model)
8
+
9
+ model_rbi = root.create_class(
10
+ model_class_name
11
+ )
12
+
13
+ model_rbi.create_extend('PgSearch::Model::ClassMethods')
14
+ end
15
+ end
@@ -12,21 +12,9 @@ require('sorbet-rails/model_plugins/enumerable_collections')
12
12
 
13
13
  module SorbetRails::ModelPlugins
14
14
  extend T::Sig
15
+ include Kernel
15
16
 
16
- @@plugins = T.let(
17
- [
18
- ActiveRecordEnum,
19
- ActiveRecordNamedScope,
20
- ActiveRecordQuerying,
21
- ActiveRelationWhereNot,
22
- ActiveRecordAttribute,
23
- ActiveRecordAssoc,
24
- ActiveRecordFinderMethods,
25
- CustomFinderMethods,
26
- EnumerableCollections,
27
- ],
28
- T::Array[T.class_of(Base)]
29
- )
17
+ @@plugins = T.let([], T::Array[T.class_of(Base)])
30
18
 
31
19
  sig { params(plugin: T.class_of(Base)).void }
32
20
  def register_plugin(plugin)
@@ -42,4 +30,49 @@ module SorbetRails::ModelPlugins
42
30
  def get_plugins
43
31
  @@plugins
44
32
  end
33
+
34
+ sig { params(plugin_name: Symbol).void }
35
+ def register_plugin_by_name(plugin_name)
36
+ register_plugin(get_plugin_by_name(plugin_name))
37
+ end
38
+
39
+ sig { params(plugin_name: Symbol).returns(T.class_of(Base)) }
40
+ def get_plugin_by_name(plugin_name)
41
+ case plugin_name
42
+ when :active_record_enum
43
+ ActiveRecordEnum
44
+ when :active_record_named_scope
45
+ ActiveRecordNamedScope
46
+ when :active_record_querying
47
+ ActiveRecordQuerying
48
+ when :active_relation_where_not
49
+ ActiveRelationWhereNot
50
+ when :active_record_attribute
51
+ ActiveRecordAttribute
52
+ when :active_record_assoc
53
+ ActiveRecordAssoc
54
+ when :active_record_finder_methods
55
+ ActiveRecordFinderMethods
56
+ when :custom_finder_methods
57
+ CustomFinderMethods
58
+ when :enumerable_collections
59
+ EnumerableCollections
60
+ when :kaminari
61
+ require('sorbet-rails/gem_plugins/kaminari_plugin')
62
+ KaminariPlugin
63
+ when :pg_search
64
+ require('sorbet-rails/gem_plugins/pg_search_plugin')
65
+ PgSearchPlugin
66
+ when :friendly_id
67
+ require('sorbet-rails/gem_plugins/friendly_id_plugin')
68
+ FriendlyIdPlugin
69
+ else
70
+ raise UnrecognizedPluginName.new(
71
+ "Unrecognized plugin with name: #{plugin_name}. Please check available plugins in the
72
+ documentation".squish!
73
+ )
74
+ end
75
+ end
76
+
77
+ class UnrecognizedPluginName < StandardError; end
45
78
  end
@@ -1,6 +1,8 @@
1
1
  # typed: true
2
2
  require "rails"
3
+ require "sorbet-runtime"
3
4
  require "sorbet-rails/custom_finder_methods"
5
+ require "sorbet-rails/config"
4
6
 
5
7
  class SorbetRails::Railtie < Rails::Railtie
6
8
  railtie_name "sorbet-rails"
@@ -30,5 +32,7 @@ class SorbetRails::Railtie < Rails::Railtie
30
32
  end
31
33
  end
32
34
  end
35
+
36
+ SorbetRails.register_configured_plugins
33
37
  end
34
38
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{sorbet-rails}
3
- s.version = "0.5.2"
3
+ s.version = "0.5.3"
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"]
@@ -267,7 +267,7 @@ after_bundle do
267
267
  Bundler.with_clean_env do
268
268
  run "SRB_YES=true bundle #{bundle_version} exec srb init"
269
269
  run "bundle #{bundle_version} exec rake rails_rbi:all"
270
- run "bundle #{bundle_version} exec srbi rbi hidden-definitions"
270
+ run "bundle #{bundle_version} exec srb rbi hidden-definitions"
271
271
  run "bundle #{bundle_version} exec srb rbi todo"
272
272
  end
273
273
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorbet-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chan Zuckerberg Initiative
@@ -144,8 +144,11 @@ files:
144
144
  - SECURITY.md
145
145
  - lib/sorbet-rails.rb
146
146
  - lib/sorbet-rails/activerecord.rbi
147
+ - lib/sorbet-rails/config.rb
147
148
  - lib/sorbet-rails/custom_finder_methods.rb
149
+ - lib/sorbet-rails/gem_plugins/friendly_id_plugin.rb
148
150
  - lib/sorbet-rails/gem_plugins/kaminari_plugin.rb
151
+ - lib/sorbet-rails/gem_plugins/pg_search_plugin.rb
149
152
  - lib/sorbet-rails/helper_rbi_formatter.rb
150
153
  - lib/sorbet-rails/model_plugins/active_record_assoc.rb
151
154
  - lib/sorbet-rails/model_plugins/active_record_attribute.rb
@@ -453,13 +456,6 @@ files:
453
456
  - spec/support/v5.2/log/.keep
454
457
  - spec/support/v5.2/package.json
455
458
  - spec/support/v5.2/public/robots.txt
456
- - spec/support/v5.2/sorbet/rbi/gems/awesome_print.rbi
457
- - spec/support/v5.2/sorbet/rbi/gems/database_cleaner.rbi
458
- - spec/support/v5.2/sorbet/rbi/gems/nio4r.rbi
459
- - spec/support/v5.2/sorbet/rbi/gems/rspec-core.rbi
460
- - spec/support/v5.2/sorbet/rbi/gems/rspec-rails.rbi
461
- - spec/support/v5.2/sorbet/rbi/gems/rspec-support.rbi
462
- - spec/support/v5.2/sorbet/rbi/gems/rspec.rbi
463
459
  - spec/support/v5.2/sorbet_test_cases.rb
464
460
  - spec/support/v5.2/storage/.keep
465
461
  - spec/support/v5.2/tmp/.keep
@@ -527,13 +523,6 @@ files:
527
523
  - spec/support/v6.0/lib/tasks/.keep
528
524
  - spec/support/v6.0/log/.keep
529
525
  - spec/support/v6.0/public/robots.txt
530
- - spec/support/v6.0/sorbet/rbi/gems/awesome_print.rbi
531
- - spec/support/v6.0/sorbet/rbi/gems/database_cleaner.rbi
532
- - spec/support/v6.0/sorbet/rbi/gems/nio4r.rbi
533
- - spec/support/v6.0/sorbet/rbi/gems/rspec-core.rbi
534
- - spec/support/v6.0/sorbet/rbi/gems/rspec-rails.rbi
535
- - spec/support/v6.0/sorbet/rbi/gems/rspec-support.rbi
536
- - spec/support/v6.0/sorbet/rbi/gems/rspec.rbi
537
526
  - spec/support/v6.0/sorbet_test_cases.rb
538
527
  - spec/support/v6.0/storage/.keep
539
528
  - spec/support/v6.0/tmp/.keep
@@ -923,13 +912,6 @@ test_files:
923
912
  - spec/support/v5.2/log/.keep
924
913
  - spec/support/v5.2/package.json
925
914
  - spec/support/v5.2/public/robots.txt
926
- - spec/support/v5.2/sorbet/rbi/gems/awesome_print.rbi
927
- - spec/support/v5.2/sorbet/rbi/gems/database_cleaner.rbi
928
- - spec/support/v5.2/sorbet/rbi/gems/nio4r.rbi
929
- - spec/support/v5.2/sorbet/rbi/gems/rspec-core.rbi
930
- - spec/support/v5.2/sorbet/rbi/gems/rspec-rails.rbi
931
- - spec/support/v5.2/sorbet/rbi/gems/rspec-support.rbi
932
- - spec/support/v5.2/sorbet/rbi/gems/rspec.rbi
933
915
  - spec/support/v5.2/sorbet_test_cases.rb
934
916
  - spec/support/v5.2/storage/.keep
935
917
  - spec/support/v5.2/tmp/.keep
@@ -997,13 +979,6 @@ test_files:
997
979
  - spec/support/v6.0/lib/tasks/.keep
998
980
  - spec/support/v6.0/log/.keep
999
981
  - spec/support/v6.0/public/robots.txt
1000
- - spec/support/v6.0/sorbet/rbi/gems/awesome_print.rbi
1001
- - spec/support/v6.0/sorbet/rbi/gems/database_cleaner.rbi
1002
- - spec/support/v6.0/sorbet/rbi/gems/nio4r.rbi
1003
- - spec/support/v6.0/sorbet/rbi/gems/rspec-core.rbi
1004
- - spec/support/v6.0/sorbet/rbi/gems/rspec-rails.rbi
1005
- - spec/support/v6.0/sorbet/rbi/gems/rspec-support.rbi
1006
- - spec/support/v6.0/sorbet/rbi/gems/rspec.rbi
1007
982
  - spec/support/v6.0/sorbet_test_cases.rb
1008
983
  - spec/support/v6.0/storage/.keep
1009
984
  - spec/support/v6.0/tmp/.keep