rubocop-rails 2.7.1 → 2.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +18 -2
  4. data/config/default.yml +144 -6
  5. data/config/obsoletion.yml +7 -0
  6. data/lib/rubocop/cop/mixin/active_record_helper.rb +16 -3
  7. data/lib/rubocop/cop/mixin/enforce_superclass.rb +40 -0
  8. data/lib/rubocop/cop/mixin/index_method.rb +25 -11
  9. data/lib/rubocop/cop/rails/action_filter.rb +10 -14
  10. data/lib/rubocop/cop/rails/active_record_aliases.rb +13 -17
  11. data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +19 -16
  12. data/lib/rubocop/cop/rails/active_record_override.rb +1 -1
  13. data/lib/rubocop/cop/rails/active_support_aliases.rb +12 -21
  14. data/lib/rubocop/cop/rails/after_commit_override.rb +91 -0
  15. data/lib/rubocop/cop/rails/application_controller.rb +3 -7
  16. data/lib/rubocop/cop/rails/application_job.rb +2 -1
  17. data/lib/rubocop/cop/rails/application_mailer.rb +2 -7
  18. data/lib/rubocop/cop/rails/application_record.rb +2 -7
  19. data/lib/rubocop/cop/rails/arel_star.rb +41 -0
  20. data/lib/rubocop/cop/rails/assert_not.rb +8 -10
  21. data/lib/rubocop/cop/rails/attribute_default_block_value.rb +90 -0
  22. data/lib/rubocop/cop/rails/belongs_to.rb +10 -19
  23. data/lib/rubocop/cop/rails/blank.rb +31 -27
  24. data/lib/rubocop/cop/rails/bulk_change_table.rb +1 -1
  25. data/lib/rubocop/cop/rails/content_tag.rb +34 -19
  26. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +2 -1
  27. data/lib/rubocop/cop/rails/date.rb +10 -11
  28. data/lib/rubocop/cop/rails/default_scope.rb +11 -4
  29. data/lib/rubocop/cop/rails/delegate.rb +9 -9
  30. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +7 -8
  31. data/lib/rubocop/cop/rails/dynamic_find_by.rb +15 -12
  32. data/lib/rubocop/cop/rails/enum_hash.rb +11 -10
  33. data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -1
  34. data/lib/rubocop/cop/rails/environment_comparison.rb +18 -14
  35. data/lib/rubocop/cop/rails/environment_variable_access.rb +67 -0
  36. data/lib/rubocop/cop/rails/exit.rb +4 -10
  37. data/lib/rubocop/cop/rails/file_path.rb +7 -8
  38. data/lib/rubocop/cop/rails/find_by.rb +13 -13
  39. data/lib/rubocop/cop/rails/find_by_id.rb +12 -21
  40. data/lib/rubocop/cop/rails/find_each.rb +19 -18
  41. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +3 -2
  42. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +37 -10
  43. data/lib/rubocop/cop/rails/helper_instance_variable.rb +30 -2
  44. data/lib/rubocop/cop/rails/http_positional_arguments.rb +32 -21
  45. data/lib/rubocop/cop/rails/http_status.rb +7 -9
  46. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +8 -6
  47. data/lib/rubocop/cop/rails/index_by.rb +11 -2
  48. data/lib/rubocop/cop/rails/index_with.rb +11 -2
  49. data/lib/rubocop/cop/rails/inquiry.rb +7 -2
  50. data/lib/rubocop/cop/rails/inverse_of.rb +3 -2
  51. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +17 -15
  52. data/lib/rubocop/cop/rails/link_to_blank.rb +25 -23
  53. data/lib/rubocop/cop/rails/mailer_name.rb +19 -13
  54. data/lib/rubocop/cop/rails/match_route.rb +14 -13
  55. data/lib/rubocop/cop/rails/negate_include.rb +10 -8
  56. data/lib/rubocop/cop/rails/not_null_column.rb +2 -1
  57. data/lib/rubocop/cop/rails/order_by_id.rb +52 -0
  58. data/lib/rubocop/cop/rails/output.rb +5 -2
  59. data/lib/rubocop/cop/rails/output_safety.rb +3 -2
  60. data/lib/rubocop/cop/rails/pick.rb +14 -12
  61. data/lib/rubocop/cop/rails/pluck.rb +6 -9
  62. data/lib/rubocop/cop/rails/pluck_id.rb +4 -6
  63. data/lib/rubocop/cop/rails/pluck_in_where.rb +39 -5
  64. data/lib/rubocop/cop/rails/pluralization_grammar.rb +10 -14
  65. data/lib/rubocop/cop/rails/presence.rb +12 -13
  66. data/lib/rubocop/cop/rails/present.rb +30 -24
  67. data/lib/rubocop/cop/rails/rake_environment.rb +8 -10
  68. data/lib/rubocop/cop/rails/read_write_attribute.rb +12 -11
  69. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +29 -31
  70. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +9 -12
  71. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +11 -10
  72. data/lib/rubocop/cop/rails/reflection_class_name.rb +18 -4
  73. data/lib/rubocop/cop/rails/refute_methods.rb +9 -10
  74. data/lib/rubocop/cop/rails/relative_date_constant.rb +34 -22
  75. data/lib/rubocop/cop/rails/render_inline.rb +2 -1
  76. data/lib/rubocop/cop/rails/render_plain_text.rb +9 -14
  77. data/lib/rubocop/cop/rails/request_referer.rb +7 -7
  78. data/lib/rubocop/cop/rails/require_dependency.rb +38 -0
  79. data/lib/rubocop/cop/rails/reversible_migration.rb +83 -8
  80. data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +75 -0
  81. data/lib/rubocop/cop/rails/safe_navigation.rb +30 -11
  82. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +5 -10
  83. data/lib/rubocop/cop/rails/save_bang.rb +19 -22
  84. data/lib/rubocop/cop/rails/scope_args.rb +2 -1
  85. data/lib/rubocop/cop/rails/short_i18n.rb +7 -9
  86. data/lib/rubocop/cop/rails/skips_model_validations.rb +4 -4
  87. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +82 -0
  88. data/lib/rubocop/cop/rails/time_zone.rb +35 -25
  89. data/lib/rubocop/cop/rails/time_zone_assignment.rb +37 -0
  90. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +6 -6
  91. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +18 -8
  92. data/lib/rubocop/cop/rails/unknown_env.rb +3 -3
  93. data/lib/rubocop/cop/rails/validation.rb +15 -14
  94. data/lib/rubocop/cop/rails/where_equals.rb +98 -0
  95. data/lib/rubocop/cop/rails/where_exists.rb +85 -16
  96. data/lib/rubocop/cop/rails/where_not.rb +101 -0
  97. data/lib/rubocop/cop/rails_cops.rb +12 -0
  98. data/lib/rubocop/rails.rb +2 -0
  99. data/lib/rubocop/rails/schema_loader.rb +4 -4
  100. data/lib/rubocop/rails/schema_loader/schema.rb +4 -8
  101. data/lib/rubocop/rails/version.rb +5 -1
  102. metadata +33 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8cffb4c138d270540b5821e23b2c5450a1939d4b01a2720ce400a8bb510ff90c
4
- data.tar.gz: e0702c6ae4a7a43a01034b32d64ad67bac55659d6a5ec471685bda3c6d2a43fe
3
+ metadata.gz: 8acb3cfc05c02eae959355280c244243a1dd2f6a979c3468e505015844144c97
4
+ data.tar.gz: 0ba96204c723ae8ec4e7e3ca1b17760b9a8dcc7dfce9b9bc13c008243d35e672
5
5
  SHA512:
6
- metadata.gz: 0ce7f6953d24239b8ce71a2e759da5007eb94a752f34ccb8d738273f5f75c63f57664042964f9766652c16e076996bb294edafc7d687a923c60ae2f56328afd3
7
- data.tar.gz: b810b20d35548b34813946fff138c204fe0ce4f64a68b2f09e63a69c102ad44cf476a543b8bcf87f9caf42d5312ec277e14d23650100df21fb27fb14cc246216
6
+ metadata.gz: 866bd70db471cfbc203226e0007e660c82f2914dcfcf43c3385d9063599935812783525132785b1256abad61b46c960a372dca0d38d64acf22b63a7eb8b76819
7
+ data.tar.gz: aa456d390b46dad8be62e104d293e1fdbb4fc1d13c3a68affd1238c29773509e55ded7dd4b219734c4d51d0099d8d44894df145690013c0712d80089d51c9fe7
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012-20 Bozhidar Batsov
1
+ Copyright (c) 2012-21 Bozhidar Batsov
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # RuboCop Rails
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/rubocop-rails.svg)](https://badge.fury.io/rb/rubocop-rails)
4
- [![CircleCI](https://circleci.com/gh/rubocop-hq/rubocop-rails.svg?style=svg)](https://circleci.com/gh/rubocop-hq/rubocop-rails)
4
+ [![CircleCI](https://circleci.com/gh/rubocop/rubocop-rails.svg?style=svg)](https://circleci.com/gh/rubocop/rubocop-rails)
5
5
 
6
- A [RuboCop](https://github.com/rubocop-hq/rubocop) extension focused on enforcing Rails best practices and coding conventions.
6
+ A [RuboCop](https://github.com/rubocop/rubocop) extension focused on enforcing Rails best practices and coding conventions.
7
7
 
8
8
  Note: This repository manages rubocop-rails gem (>= 2.0.0). rubocop-rails gem (<= 1.5.0) has been renamed to [rubocop-rails_config](https://rubygems.org/gems/rubocop-rails_config) gem.
9
9
 
@@ -86,6 +86,22 @@ Rails cops support the following versions:
86
86
 
87
87
  - Rails 4.2+
88
88
 
89
+ ## Readme Badge
90
+
91
+ If you use RuboCop Rails in your project, you can include one of these badges in your readme to let people know that your code is written following the community Rails Style Guide.
92
+
93
+ [![Rails Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop-rails)
94
+
95
+ [![Rails Style Guide](https://img.shields.io/badge/code_style-community-brightgreen.svg)](https://rails.rubystyle.guide)
96
+
97
+ Here are the Markdown snippets for the two badges:
98
+
99
+ ``` markdown
100
+ [![Rails Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop-rails)
101
+
102
+ [![Rails Style Guide](https://img.shields.io/badge/code_style-community-brightgreen.svg)](https://rails.rubystyle.guide)
103
+ ```
104
+
89
105
  ## Contributing
90
106
 
91
107
  Checkout the [contribution guidelines](CONTRIBUTING.md).
data/config/default.yml CHANGED
@@ -17,6 +17,27 @@ AllCops:
17
17
  # as the default.
18
18
  TargetRailsVersion: ~
19
19
 
20
+ Lint/NumberConversion:
21
+ # Add Rails' duration methods to the ignore list for `Lint/NumberConversion`
22
+ # so that calling `to_i` on one of these does not register an offense.
23
+ # See: https://github.com/rubocop/rubocop/issues/8950
24
+ IgnoredMethods:
25
+ - ago
26
+ - from_now
27
+ - second
28
+ - seconds
29
+ - minute
30
+ - minutes
31
+ - hour
32
+ - hours
33
+ - day
34
+ - days
35
+ - week
36
+ - weeks
37
+ - fortnight
38
+ - fortnights
39
+ - in_milliseconds
40
+
20
41
  Rails/ActionFilter:
21
42
  Description: 'Enforces consistent use of action filter methods.'
22
43
  Enabled: true
@@ -62,6 +83,14 @@ Rails/ActiveSupportAliases:
62
83
  Enabled: true
63
84
  VersionAdded: '0.48'
64
85
 
86
+ Rails/AfterCommitOverride:
87
+ Description: >-
88
+ This cop enforces that there is only one call to `after_commit`
89
+ (and its aliases - `after_create_commit`, `after_update_commit`,
90
+ and `after_destroy_commit`) with the same callback name per model.
91
+ Enabled: 'pending'
92
+ VersionAdded: '2.8'
93
+
65
94
  Rails/ApplicationController:
66
95
  Description: 'Check that controllers subclass ApplicationController.'
67
96
  Enabled: true
@@ -90,6 +119,12 @@ Rails/ApplicationRecord:
90
119
  VersionAdded: '0.49'
91
120
  VersionChanged: '2.5'
92
121
 
122
+ Rails/ArelStar:
123
+ Description: 'Enforces `Arel.star` instead of `"*"` for expanded columns.'
124
+ Enabled: true
125
+ SafeAutoCorrect: false
126
+ VersionAdded: '2.9'
127
+
93
128
  Rails/AssertNot:
94
129
  Description: 'Use `assert_not` instead of `assert !`.'
95
130
  Enabled: true
@@ -97,6 +132,13 @@ Rails/AssertNot:
97
132
  Include:
98
133
  - '**/test/**/*'
99
134
 
135
+ Rails/AttributeDefaultBlockValue:
136
+ Description: 'Pass method call in block for attribute option `default`.'
137
+ Enabled: pending
138
+ VersionAdded: '2.9'
139
+ Include:
140
+ - 'models/**/*'
141
+
100
142
  Rails/BelongsTo:
101
143
  Description: >-
102
144
  Use `optional: true` instead of `required: false` for
@@ -107,8 +149,9 @@ Rails/BelongsTo:
107
149
  Rails/Blank:
108
150
  Description: 'Enforces use of `blank?`.'
109
151
  Enabled: true
152
+ SafeAutoCorrect: false
110
153
  VersionAdded: '0.48'
111
- VersionChanged: '0.67'
154
+ VersionChanged: '2.10'
112
155
  # Convert usages of `nil? || empty?` to `blank?`
113
156
  NilOrEmpty: true
114
157
  # Convert usages of `!present?` to `blank?`
@@ -186,8 +229,9 @@ Rails/DynamicFindBy:
186
229
  Description: 'Use `find_by` instead of dynamic `find_by_*`.'
187
230
  StyleGuide: 'https://rails.rubystyle.guide#find_by'
188
231
  Enabled: true
232
+ Safe: false
189
233
  VersionAdded: '0.44'
190
- VersionChanged: '2.6'
234
+ VersionChanged: '2.10'
191
235
  # The `Whitelist` has been deprecated, Please use `AllowedMethods` instead.
192
236
  Whitelist:
193
237
  - find_by_sql
@@ -216,6 +260,18 @@ Rails/EnvironmentComparison:
216
260
  Enabled: true
217
261
  VersionAdded: '0.52'
218
262
 
263
+ Rails/EnvironmentVariableAccess:
264
+ Description: 'Do not access `ENV` directly after initialization.'
265
+ Enabled: pending
266
+ VersionAdded: '2.10'
267
+ Include:
268
+ - app/**/*.rb
269
+ - lib/**/*.rb
270
+ Exclude:
271
+ - lib/**/*.rake
272
+ AllowReads: false
273
+ AllowWrites: false
274
+
219
275
  Rails/Exit:
220
276
  Description: >-
221
277
  Favor `fail`, `break`, `return`, etc. over `exit` in
@@ -261,8 +317,15 @@ Rails/FindEach:
261
317
  StyleGuide: 'https://rails.rubystyle.guide#find-each'
262
318
  Enabled: true
263
319
  VersionAdded: '0.30'
320
+ VersionChanged: '2.9'
264
321
  Include:
265
322
  - app/models/**/*.rb
323
+ IgnoredMethods:
324
+ # Methods that don't work well with `find_each`.
325
+ - order
326
+ - limit
327
+ - select
328
+ - lock
266
329
 
267
330
  Rails/HasAndBelongsToMany:
268
331
  Description: 'Prefer has_many :through to has_and_belongs_to_many.'
@@ -313,14 +376,16 @@ Rails/IgnoredSkipActionFilterOption:
313
376
  - app/controllers/**/*.rb
314
377
 
315
378
  Rails/IndexBy:
316
- Description: 'Prefer `index_by` over `each_with_object` or `map`.'
379
+ Description: 'Prefer `index_by` over `each_with_object`, `to_h`, or `map`.'
317
380
  Enabled: true
318
381
  VersionAdded: '2.5'
382
+ VersionChanged: '2.8'
319
383
 
320
384
  Rails/IndexWith:
321
- Description: 'Prefer `index_with` over `each_with_object` or `map`.'
385
+ Description: 'Prefer `index_with` over `each_with_object`, `to_h`, or `map`.'
322
386
  Enabled: true
323
387
  VersionAdded: '2.5'
388
+ VersionChanged: '2.8'
324
389
 
325
390
  Rails/Inquiry:
326
391
  Description: "Prefer Ruby's comparison operators over Active Support's `Array#inquiry` and `String#inquiry`."
@@ -357,6 +422,7 @@ Rails/MailerName:
357
422
  Description: 'Mailer should end with `Mailer` suffix.'
358
423
  StyleGuide: 'https://rails.rubystyle.guide/#mailer-name'
359
424
  Enabled: 'pending'
425
+ SafeAutoCorrect: false
360
426
  VersionAdded: '2.7'
361
427
  Include:
362
428
  - app/mailers/**/*.rb
@@ -376,7 +442,9 @@ Rails/NegateInclude:
376
442
  Description: 'Prefer `collection.exclude?(obj)` over `!collection.include?(obj)`.'
377
443
  StyleGuide: 'https://rails.rubystyle.guide#exclude'
378
444
  Enabled: 'pending'
445
+ Safe: false
379
446
  VersionAdded: '2.7'
447
+ VersionChanged: '2.9'
380
448
 
381
449
  Rails/NotNullColumn:
382
450
  Description: 'Do not add a NOT NULL column without a default value.'
@@ -385,6 +453,14 @@ Rails/NotNullColumn:
385
453
  Include:
386
454
  - db/migrate/*.rb
387
455
 
456
+ Rails/OrderById:
457
+ Description: >-
458
+ Do not use the `id` column for ordering.
459
+ Use a timestamp column to order chronologically.
460
+ StyleGuide: 'https://rails.rubystyle.guide/#order-by-id'
461
+ Enabled: false
462
+ VersionAdded: '2.8'
463
+
388
464
  Rails/Output:
389
465
  Description: 'Checks for calls to puts, print, etc.'
390
466
  Enabled: true
@@ -426,6 +502,11 @@ Rails/PluckInWhere:
426
502
  Enabled: 'pending'
427
503
  Safe: false
428
504
  VersionAdded: '2.7'
505
+ VersionChanged: '2.8'
506
+ EnforcedStyle: conservative
507
+ SupportedStyles:
508
+ - conservative
509
+ - aggressive
429
510
 
430
511
  Rails/PluralizationGrammar:
431
512
  Description: 'Checks for incorrect grammar when using methods like `3.day.ago`.'
@@ -494,7 +575,9 @@ Rails/RedundantReceiverInWithOptions:
494
575
  Rails/ReflectionClassName:
495
576
  Description: 'Use a string for `class_name` option value in the definition of a reflection.'
496
577
  Enabled: true
578
+ Safe: false
497
579
  VersionAdded: '0.64'
580
+ VersionChanged: '2.10'
498
581
 
499
582
  Rails/RefuteMethods:
500
583
  Description: 'Use `assert_not` methods instead of `refute` methods.'
@@ -537,6 +620,12 @@ Rails/RequestReferer:
537
620
  - referer
538
621
  - referrer
539
622
 
623
+ Rails/RequireDependency:
624
+ Description: 'Do not use `require_dependency` when running in Zeitwerk mode. `require_dependency` is for autoloading in classic mode.'
625
+ Reference: 'https://guides.rubyonrails.org/autoloading_and_reloading_constants.html'
626
+ Enabled: false
627
+ VersionAdded: '2.10'
628
+
540
629
  Rails/ReversibleMigration:
541
630
  Description: 'Checks whether the change method of the migration file is reversible.'
542
631
  StyleGuide: 'https://rails.rubystyle.guide#reversible-migration'
@@ -546,6 +635,13 @@ Rails/ReversibleMigration:
546
635
  Include:
547
636
  - db/migrate/*.rb
548
637
 
638
+ Rails/ReversibleMigrationMethodDefinition:
639
+ Description: 'Checks whether the migration implements either a `change` method or both an `up` and a `down` method.'
640
+ Enabled: false
641
+ VersionAdded: '2.10'
642
+ include:
643
+ - db/migrate/*.rb
644
+
549
645
  Rails/SafeNavigation:
550
646
  Description: "Use Ruby's safe navigation operator (`&.`) instead of `try!`."
551
647
  Enabled: true
@@ -624,6 +720,16 @@ Rails/SkipsModelValidations:
624
720
  - upsert_all
625
721
  AllowedMethods: []
626
722
 
723
+ Rails/SquishedSQLHeredocs:
724
+ Description: 'Checks SQL heredocs to use `.squish`.'
725
+ StyleGuide: 'https://rails.rubystyle.guide/#squished-heredocs'
726
+ Enabled: 'pending'
727
+ VersionAdded: '2.8'
728
+ VersionChanged: '2.9'
729
+ # Some SQL syntax (e.g. PostgreSQL comments and functions) requires newlines
730
+ # to be preserved in order to work, thus auto-correction is not safe.
731
+ SafeAutoCorrect: false
732
+
627
733
  Rails/TimeZone:
628
734
  Description: 'Checks the correct usage of time zone aware methods.'
629
735
  StyleGuide: 'https://rails.rubystyle.guide#time'
@@ -631,23 +737,35 @@ Rails/TimeZone:
631
737
  Enabled: true
632
738
  Safe: false
633
739
  VersionAdded: '0.30'
634
- VersionChanged: '0.68'
740
+ VersionChanged: '2.10'
635
741
  # The value `strict` means that `Time` should be used with `zone`.
636
742
  # The value `flexible` allows usage of `in_time_zone` instead of `zone`.
637
743
  EnforcedStyle: flexible
638
744
  SupportedStyles:
639
745
  - strict
640
746
  - flexible
747
+ Exclude:
748
+ - '**/*.gemspec'
749
+
750
+ Rails/TimeZoneAssignment:
751
+ Description: 'Prefer the usage of `Time.use_zone` instead of manually updating `Time.zone` value.'
752
+ Reference: 'https://thoughtbot.com/blog/its-about-time-zones'
753
+ Enabled: 'pending'
754
+ VersionAdded: '2.10'
755
+ Include:
756
+ - spec/**/*.rb
757
+ - test/**/*.rb
641
758
 
642
759
  Rails/UniqBeforePluck:
643
760
  Description: 'Prefer the use of uniq or distinct before pluck.'
644
761
  Enabled: true
645
762
  VersionAdded: '0.40'
646
- VersionChanged: '2.6'
763
+ VersionChanged: '2.8'
647
764
  EnforcedStyle: conservative
648
765
  SupportedStyles:
649
766
  - conservative
650
767
  - aggressive
768
+ SafeAutoCorrect: false
651
769
  AutoCorrect: false
652
770
 
653
771
  Rails/UniqueValidationWithoutIndex:
@@ -674,10 +792,30 @@ Rails/Validation:
674
792
  Include:
675
793
  - app/models/**/*.rb
676
794
 
795
+ Rails/WhereEquals:
796
+ Description: 'Pass conditions to `where` as a hash instead of manually constructing SQL.'
797
+ StyleGuide: 'https://rails.rubystyle.guide/#hash-conditions'
798
+ Enabled: 'pending'
799
+ SafeAutoCorrect: false
800
+ VersionAdded: '2.9'
801
+ VersionChanged: '2.10'
802
+
677
803
  Rails/WhereExists:
678
804
  Description: 'Prefer `exists?(...)` over `where(...).exists?`.'
679
805
  Enabled: 'pending'
806
+ SafeAutoCorrect: false
807
+ EnforcedStyle: exists
808
+ SupportedStyles:
809
+ - exists
810
+ - where
680
811
  VersionAdded: '2.7'
812
+ VersionChanged: '2.10'
813
+
814
+ Rails/WhereNot:
815
+ Description: 'Use `where.not(...)` instead of manually constructing negated SQL in `where`.'
816
+ StyleGuide: 'https://rails.rubystyle.guide/#hash-conditions'
817
+ Enabled: 'pending'
818
+ VersionAdded: '2.8'
681
819
 
682
820
  # Accept `redirect_to(...) and return` and similar cases.
683
821
  Style/AndOr:
@@ -0,0 +1,7 @@
1
+ #
2
+ # Configuration for obsoletion.
3
+ #
4
+ # See: https://docs.rubocop.org/rubocop/extensions.html#config-obsoletions
5
+ #
6
+ extracted:
7
+ Rails/*: ~
@@ -8,6 +8,13 @@ module RuboCop
8
8
 
9
9
  WHERE_METHODS = %i[where rewhere].freeze
10
10
 
11
+ def_node_matcher :active_record?, <<~PATTERN
12
+ {
13
+ (const nil? :ApplicationRecord)
14
+ (const (const nil? :ActiveRecord) :Base)
15
+ }
16
+ PATTERN
17
+
11
18
  def_node_search :find_set_table_name, <<~PATTERN
12
19
  (send self :table_name= {str sym})
13
20
  PATTERN
@@ -16,6 +23,10 @@ module RuboCop
16
23
  (send nil? :belongs_to {str sym} ...)
17
24
  PATTERN
18
25
 
26
+ def inherit_active_record_base?(node)
27
+ node.each_ancestor(:class).any? { |class_node| active_record?(class_node.parent_class) }
28
+ end
29
+
19
30
  def external_dependency_checksum
20
31
  return @external_dependency_checksum if defined?(@external_dependency_checksum)
21
32
 
@@ -35,10 +46,11 @@ module RuboCop
35
46
  table_name = find_set_table_name(class_node).to_a.last&.first_argument
36
47
  return table_name.value.to_s if table_name
37
48
 
38
- namespaces = class_node.each_ancestor(:class, :module)
39
- [class_node, *namespaces]
49
+ class_nodes = class_node.defined_module.each_node
50
+ namespaces = class_node.each_ancestor(:class, :module).map(&:identifier)
51
+ [*class_nodes, *namespaces]
40
52
  .reverse
41
- .map { |klass| klass.identifier.children[1] }.join('_')
53
+ .map { |node| node.children[1] }.join('_')
42
54
  .tableize
43
55
  end
44
56
 
@@ -52,6 +64,7 @@ module RuboCop
52
64
  # @param table [RuboCop::Rails::SchemaLoader::Table]
53
65
  # @return [String, nil]
54
66
  def resolve_relation_into_column(name:, class_node:, table:)
67
+ return unless table
55
68
  return name if table.with_column?(name: name)
56
69
 
57
70
  find_belongs_to(class_node) do |belongs_to|
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Common functionality for enforcing a specific superclass.
6
+ module EnforceSuperclass
7
+ def self.included(base)
8
+ base.def_node_matcher :class_definition, <<~PATTERN
9
+ (class (const _ !:#{base::SUPERCLASS}) #{base::BASE_PATTERN} ...)
10
+ PATTERN
11
+
12
+ base.def_node_matcher :class_new_definition, <<~PATTERN
13
+ [!^(casgn {nil? cbase} :#{base::SUPERCLASS} ...)
14
+ !^^(casgn {nil? cbase} :#{base::SUPERCLASS} (block ...))
15
+ (send (const {nil? cbase} :Class) :new #{base::BASE_PATTERN})]
16
+ PATTERN
17
+ end
18
+
19
+ def on_class(node)
20
+ class_definition(node) do
21
+ register_offense(node.children[1])
22
+ end
23
+ end
24
+
25
+ def on_send(node)
26
+ class_new_definition(node) do
27
+ register_offense(node.children.last)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def register_offense(offense_node)
34
+ add_offense(offense_node) do |corrector|
35
+ corrector.replace(offense_node.source_range, self.class::SUPERCLASS)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end