rubocop-rails 2.8.1 → 2.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) 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 +15 -3
  7. data/lib/rubocop/cop/mixin/enforce_superclass.rb +40 -0
  8. data/lib/rubocop/cop/mixin/index_method.rb +8 -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 +17 -12
  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/add_column_index.rb +64 -0
  15. data/lib/rubocop/cop/rails/after_commit_override.rb +1 -1
  16. data/lib/rubocop/cop/rails/application_controller.rb +3 -7
  17. data/lib/rubocop/cop/rails/application_job.rb +2 -1
  18. data/lib/rubocop/cop/rails/application_mailer.rb +2 -7
  19. data/lib/rubocop/cop/rails/application_record.rb +2 -7
  20. data/lib/rubocop/cop/rails/arel_star.rb +41 -0
  21. data/lib/rubocop/cop/rails/assert_not.rb +8 -10
  22. data/lib/rubocop/cop/rails/attribute_default_block_value.rb +90 -0
  23. data/lib/rubocop/cop/rails/belongs_to.rb +10 -19
  24. data/lib/rubocop/cop/rails/blank.rb +31 -27
  25. data/lib/rubocop/cop/rails/bulk_change_table.rb +1 -1
  26. data/lib/rubocop/cop/rails/content_tag.rb +33 -18
  27. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +2 -1
  28. data/lib/rubocop/cop/rails/date.rb +27 -17
  29. data/lib/rubocop/cop/rails/default_scope.rb +11 -4
  30. data/lib/rubocop/cop/rails/delegate.rb +9 -9
  31. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +7 -8
  32. data/lib/rubocop/cop/rails/dynamic_find_by.rb +16 -13
  33. data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +78 -0
  34. data/lib/rubocop/cop/rails/enum_hash.rb +11 -10
  35. data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -1
  36. data/lib/rubocop/cop/rails/environment_comparison.rb +18 -14
  37. data/lib/rubocop/cop/rails/environment_variable_access.rb +67 -0
  38. data/lib/rubocop/cop/rails/exit.rb +4 -10
  39. data/lib/rubocop/cop/rails/expanded_date_range.rb +86 -0
  40. data/lib/rubocop/cop/rails/file_path.rb +6 -7
  41. data/lib/rubocop/cop/rails/find_by.rb +32 -24
  42. data/lib/rubocop/cop/rails/find_by_id.rb +12 -21
  43. data/lib/rubocop/cop/rails/find_each.rb +19 -18
  44. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +3 -2
  45. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +37 -6
  46. data/lib/rubocop/cop/rails/helper_instance_variable.rb +29 -3
  47. data/lib/rubocop/cop/rails/http_positional_arguments.rb +26 -21
  48. data/lib/rubocop/cop/rails/http_status.rb +18 -11
  49. data/lib/rubocop/cop/rails/i18n_locale_assignment.rb +37 -0
  50. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +8 -6
  51. data/lib/rubocop/cop/rails/index_by.rb +2 -1
  52. data/lib/rubocop/cop/rails/index_with.rb +2 -1
  53. data/lib/rubocop/cop/rails/inquiry.rb +4 -3
  54. data/lib/rubocop/cop/rails/inverse_of.rb +3 -2
  55. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +17 -15
  56. data/lib/rubocop/cop/rails/link_to_blank.rb +25 -23
  57. data/lib/rubocop/cop/rails/mailer_name.rb +19 -13
  58. data/lib/rubocop/cop/rails/match_route.rb +14 -13
  59. data/lib/rubocop/cop/rails/negate_include.rb +10 -8
  60. data/lib/rubocop/cop/rails/not_null_column.rb +2 -1
  61. data/lib/rubocop/cop/rails/order_by_id.rb +1 -2
  62. data/lib/rubocop/cop/rails/output.rb +5 -2
  63. data/lib/rubocop/cop/rails/output_safety.rb +3 -2
  64. data/lib/rubocop/cop/rails/pick.rb +14 -12
  65. data/lib/rubocop/cop/rails/pluck.rb +6 -9
  66. data/lib/rubocop/cop/rails/pluck_id.rb +4 -6
  67. data/lib/rubocop/cop/rails/pluck_in_where.rb +7 -7
  68. data/lib/rubocop/cop/rails/pluralization_grammar.rb +10 -14
  69. data/lib/rubocop/cop/rails/presence.rb +12 -13
  70. data/lib/rubocop/cop/rails/present.rb +30 -24
  71. data/lib/rubocop/cop/rails/rake_environment.rb +8 -10
  72. data/lib/rubocop/cop/rails/read_write_attribute.rb +12 -11
  73. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +29 -31
  74. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +9 -12
  75. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +11 -10
  76. data/lib/rubocop/cop/rails/reflection_class_name.rb +17 -3
  77. data/lib/rubocop/cop/rails/refute_methods.rb +9 -10
  78. data/lib/rubocop/cop/rails/relative_date_constant.rb +34 -27
  79. data/lib/rubocop/cop/rails/render_inline.rb +2 -1
  80. data/lib/rubocop/cop/rails/render_plain_text.rb +9 -14
  81. data/lib/rubocop/cop/rails/request_referer.rb +7 -7
  82. data/lib/rubocop/cop/rails/require_dependency.rb +38 -0
  83. data/lib/rubocop/cop/rails/reversible_migration.rb +3 -7
  84. data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +75 -0
  85. data/lib/rubocop/cop/rails/safe_navigation.rb +30 -11
  86. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +5 -10
  87. data/lib/rubocop/cop/rails/save_bang.rb +17 -20
  88. data/lib/rubocop/cop/rails/scope_args.rb +2 -1
  89. data/lib/rubocop/cop/rails/short_i18n.rb +7 -9
  90. data/lib/rubocop/cop/rails/skips_model_validations.rb +4 -4
  91. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +5 -6
  92. data/lib/rubocop/cop/rails/time_zone.rb +44 -42
  93. data/lib/rubocop/cop/rails/time_zone_assignment.rb +37 -0
  94. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +4 -6
  95. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +2 -2
  96. data/lib/rubocop/cop/rails/unknown_env.rb +3 -3
  97. data/lib/rubocop/cop/rails/unused_ignored_columns.rb +69 -0
  98. data/lib/rubocop/cop/rails/validation.rb +15 -14
  99. data/lib/rubocop/cop/rails/where_equals.rb +98 -0
  100. data/lib/rubocop/cop/rails/where_exists.rb +19 -13
  101. data/lib/rubocop/cop/rails/where_not.rb +10 -17
  102. data/lib/rubocop/cop/rails_cops.rb +13 -0
  103. data/lib/rubocop/rails.rb +2 -0
  104. data/lib/rubocop/rails/schema_loader.rb +4 -4
  105. data/lib/rubocop/rails/schema_loader/schema.rb +3 -5
  106. data/lib/rubocop/rails/version.rb +5 -1
  107. metadata +34 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 49338e1944d78e63ec06fc37e16cbe3010f68f94d99ad24d5353be0c156bf8cb
4
- data.tar.gz: 90823cd3bac084df9ac449fb6b0cb07a570721d2449c755432f022fb7c558e54
3
+ metadata.gz: b189297521a834dca247787947efd0f3a7c5fed19b351b37d6c729b0622ff52d
4
+ data.tar.gz: e68accc2d7c9c5726b63967f665414d09f9d3597117de042ee9dbc024cbc6350
5
5
  SHA512:
6
- metadata.gz: c242d3a6cdea9df1e4e7d9000e55db30d68dfd21317a286ad097c34a52e6cbd53a8fc6442a3333a312373adf8d7bfd05b562cf9a7391a752a7dccdc357a1b87e
7
- data.tar.gz: fafe45955b9f0b50e403f37bdb499cdb78c6883f9472faba784e4e6e64a0fa661902d1700d38bd3a1d8c8a1c1cfc7ff2751e01cd37174cdda9025e9ef4195c43
6
+ metadata.gz: 2474d4d0321548d02443df1ec10e20252c5496fa6df4dd4bc5d8c0544c9ff884f23da90273075d808bfa1ef22659ee8d8b81cff4f5dbf8a8134c054adf298187
7
+ data.tar.gz: f7cb276a49bdaffffbc020ac96951f3e5074c4f993f77710b22a48fc7efb357d447853768f6006c7e173640e483601cd6bab61b6ffb3df5a70dffb3a07b49541
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,16 @@ Rails/ActiveSupportAliases:
62
83
  Enabled: true
63
84
  VersionAdded: '0.48'
64
85
 
86
+ Rails/AddColumnIndex:
87
+ Description: >-
88
+ Rails migrations don't make use of a given `index` key, but also
89
+ doesn't given an error when it's used, so it makes it seem like an
90
+ index might be used.
91
+ Enabled: pending
92
+ VersionAdded: '2.11'
93
+ Include:
94
+ - db/migrate/*.rb
95
+
65
96
  Rails/AfterCommitOverride:
66
97
  Description: >-
67
98
  This cop enforces that there is only one call to `after_commit`
@@ -98,6 +129,12 @@ Rails/ApplicationRecord:
98
129
  VersionAdded: '0.49'
99
130
  VersionChanged: '2.5'
100
131
 
132
+ Rails/ArelStar:
133
+ Description: 'Enforces `Arel.star` instead of `"*"` for expanded columns.'
134
+ Enabled: true
135
+ SafeAutoCorrect: false
136
+ VersionAdded: '2.9'
137
+
101
138
  Rails/AssertNot:
102
139
  Description: 'Use `assert_not` instead of `assert !`.'
103
140
  Enabled: true
@@ -105,6 +142,13 @@ Rails/AssertNot:
105
142
  Include:
106
143
  - '**/test/**/*'
107
144
 
145
+ Rails/AttributeDefaultBlockValue:
146
+ Description: 'Pass method call in block for attribute option `default`.'
147
+ Enabled: pending
148
+ VersionAdded: '2.9'
149
+ Include:
150
+ - 'models/**/*'
151
+
108
152
  Rails/BelongsTo:
109
153
  Description: >-
110
154
  Use `optional: true` instead of `required: false` for
@@ -115,8 +159,9 @@ Rails/BelongsTo:
115
159
  Rails/Blank:
116
160
  Description: 'Enforces use of `blank?`.'
117
161
  Enabled: true
162
+ SafeAutoCorrect: false
118
163
  VersionAdded: '0.48'
119
- VersionChanged: '0.67'
164
+ VersionChanged: '2.10'
120
165
  # Convert usages of `nil? || empty?` to `blank?`
121
166
  NilOrEmpty: true
122
167
  # Convert usages of `!present?` to `blank?`
@@ -158,7 +203,7 @@ Rails/Date:
158
203
  such as Date.today, Date.current etc.
159
204
  Enabled: true
160
205
  VersionAdded: '0.30'
161
- VersionChanged: '0.33'
206
+ VersionChanged: '2.11'
162
207
  # The value `strict` disallows usage of `Date.today`, `Date.current`,
163
208
  # `Date#to_time` etc.
164
209
  # The value `flexible` allows usage of `Date.current`, `Date.yesterday`, etc
@@ -168,6 +213,7 @@ Rails/Date:
168
213
  SupportedStyles:
169
214
  - strict
170
215
  - flexible
216
+ AllowToTime: true
171
217
 
172
218
  Rails/DefaultScope:
173
219
  Description: 'Avoid use of `default_scope`.'
@@ -194,8 +240,9 @@ Rails/DynamicFindBy:
194
240
  Description: 'Use `find_by` instead of dynamic `find_by_*`.'
195
241
  StyleGuide: 'https://rails.rubystyle.guide#find_by'
196
242
  Enabled: true
243
+ Safe: false
197
244
  VersionAdded: '0.44'
198
- VersionChanged: '2.6'
245
+ VersionChanged: '2.10'
199
246
  # The `Whitelist` has been deprecated, Please use `AllowedMethods` instead.
200
247
  Whitelist:
201
248
  - find_by_sql
@@ -204,6 +251,12 @@ Rails/DynamicFindBy:
204
251
  AllowedReceivers:
205
252
  - Gem::Specification
206
253
 
254
+ Rails/EagerEvaluationLogMessage:
255
+ Description: 'Checks that blocks are used for interpolated strings passed to `Rails.logger.debug`.'
256
+ Reference: 'https://guides.rubyonrails.org/debugging_rails_applications.html#impact-of-logs-on-performance'
257
+ Enabled: pending
258
+ VersionAdded: '2.11'
259
+
207
260
  Rails/EnumHash:
208
261
  Description: 'Prefer hash syntax over array syntax when defining enums.'
209
262
  StyleGuide: 'https://rails.rubystyle.guide#enums'
@@ -224,6 +277,20 @@ Rails/EnvironmentComparison:
224
277
  Enabled: true
225
278
  VersionAdded: '0.52'
226
279
 
280
+ Rails/EnvironmentVariableAccess:
281
+ Description: 'Do not access `ENV` directly after initialization.'
282
+ # TODO: Set to `pending` status in RuboCop Rails 2 series when migration doc will be written.
283
+ Enabled: false
284
+ VersionAdded: '2.10'
285
+ VersionChanged: '2.11'
286
+ Include:
287
+ - app/**/*.rb
288
+ - lib/**/*.rb
289
+ Exclude:
290
+ - lib/**/*.rake
291
+ AllowReads: false
292
+ AllowWrites: false
293
+
227
294
  Rails/Exit:
228
295
  Description: >-
229
296
  Favor `fail`, `break`, `return`, etc. over `exit` in
@@ -238,6 +305,11 @@ Rails/Exit:
238
305
  Exclude:
239
306
  - lib/**/*.rake
240
307
 
308
+ Rails/ExpandedDateRange:
309
+ Description: 'Checks for expanded date range.'
310
+ Enabled: pending
311
+ VersionAdded: '2.11'
312
+
241
313
  Rails/FilePath:
242
314
  Description: 'Use `Rails.root.join` for file path joining.'
243
315
  Enabled: true
@@ -253,6 +325,8 @@ Rails/FindBy:
253
325
  StyleGuide: 'https://rails.rubystyle.guide#find_by'
254
326
  Enabled: true
255
327
  VersionAdded: '0.30'
328
+ VersionChanged: '2.11'
329
+ IgnoreWhereFirst: true
256
330
  Include:
257
331
  - app/models/**/*.rb
258
332
 
@@ -269,8 +343,15 @@ Rails/FindEach:
269
343
  StyleGuide: 'https://rails.rubystyle.guide#find-each'
270
344
  Enabled: true
271
345
  VersionAdded: '0.30'
346
+ VersionChanged: '2.9'
272
347
  Include:
273
348
  - app/models/**/*.rb
349
+ IgnoredMethods:
350
+ # Methods that don't work well with `find_each`.
351
+ - order
352
+ - limit
353
+ - select
354
+ - lock
274
355
 
275
356
  Rails/HasAndBelongsToMany:
276
357
  Description: 'Prefer has_many :through to has_and_belongs_to_many.'
@@ -307,11 +388,20 @@ Rails/HttpStatus:
307
388
  Description: 'Enforces use of symbolic or numeric value to define HTTP status.'
308
389
  Enabled: true
309
390
  VersionAdded: '0.54'
391
+ VersionChanged: '2.11'
310
392
  EnforcedStyle: symbolic
311
393
  SupportedStyles:
312
394
  - numeric
313
395
  - symbolic
314
396
 
397
+ Rails/I18nLocaleAssignment:
398
+ Description: 'Prefer the usage of `I18n.with_locale` instead of manually updating `I18n.locale` value.'
399
+ Enabled: 'pending'
400
+ VersionAdded: '2.11'
401
+ Include:
402
+ - spec/**/*.rb
403
+ - test/**/*.rb
404
+
315
405
  Rails/IgnoredSkipActionFilterOption:
316
406
  Description: 'Checks that `if` and `only` (or `except`) are not used together as options of `skip_*` action filter.'
317
407
  Reference: 'https://api.rubyonrails.org/classes/AbstractController/Callbacks/ClassMethods.html#method-i-_normalize_callback_options'
@@ -387,7 +477,9 @@ Rails/NegateInclude:
387
477
  Description: 'Prefer `collection.exclude?(obj)` over `!collection.include?(obj)`.'
388
478
  StyleGuide: 'https://rails.rubystyle.guide#exclude'
389
479
  Enabled: 'pending'
480
+ Safe: false
390
481
  VersionAdded: '2.7'
482
+ VersionChanged: '2.9'
391
483
 
392
484
  Rails/NotNullColumn:
393
485
  Description: 'Do not add a NOT NULL column without a default value.'
@@ -518,7 +610,9 @@ Rails/RedundantReceiverInWithOptions:
518
610
  Rails/ReflectionClassName:
519
611
  Description: 'Use a string for `class_name` option value in the definition of a reflection.'
520
612
  Enabled: true
613
+ Safe: false
521
614
  VersionAdded: '0.64'
615
+ VersionChanged: '2.10'
522
616
 
523
617
  Rails/RefuteMethods:
524
618
  Description: 'Use `assert_not` methods instead of `refute` methods.'
@@ -561,6 +655,12 @@ Rails/RequestReferer:
561
655
  - referer
562
656
  - referrer
563
657
 
658
+ Rails/RequireDependency:
659
+ Description: 'Do not use `require_dependency` when running in Zeitwerk mode. `require_dependency` is for autoloading in classic mode.'
660
+ Reference: 'https://guides.rubyonrails.org/autoloading_and_reloading_constants.html'
661
+ Enabled: false
662
+ VersionAdded: '2.10'
663
+
564
664
  Rails/ReversibleMigration:
565
665
  Description: 'Checks whether the change method of the migration file is reversible.'
566
666
  StyleGuide: 'https://rails.rubystyle.guide#reversible-migration'
@@ -570,6 +670,13 @@ Rails/ReversibleMigration:
570
670
  Include:
571
671
  - db/migrate/*.rb
572
672
 
673
+ Rails/ReversibleMigrationMethodDefinition:
674
+ Description: 'Checks whether the migration implements either a `change` method or both an `up` and a `down` method.'
675
+ Enabled: false
676
+ VersionAdded: '2.10'
677
+ Include:
678
+ - db/migrate/*.rb
679
+
573
680
  Rails/SafeNavigation:
574
681
  Description: "Use Ruby's safe navigation operator (`&.`) instead of `try!`."
575
682
  Enabled: true
@@ -653,6 +760,10 @@ Rails/SquishedSQLHeredocs:
653
760
  StyleGuide: 'https://rails.rubystyle.guide/#squished-heredocs'
654
761
  Enabled: 'pending'
655
762
  VersionAdded: '2.8'
763
+ VersionChanged: '2.9'
764
+ # Some SQL syntax (e.g. PostgreSQL comments and functions) requires newlines
765
+ # to be preserved in order to work, thus auto-correction is not safe.
766
+ SafeAutoCorrect: false
656
767
 
657
768
  Rails/TimeZone:
658
769
  Description: 'Checks the correct usage of time zone aware methods.'
@@ -661,13 +772,24 @@ Rails/TimeZone:
661
772
  Enabled: true
662
773
  Safe: false
663
774
  VersionAdded: '0.30'
664
- VersionChanged: '0.68'
775
+ VersionChanged: '2.10'
665
776
  # The value `strict` means that `Time` should be used with `zone`.
666
777
  # The value `flexible` allows usage of `in_time_zone` instead of `zone`.
667
778
  EnforcedStyle: flexible
668
779
  SupportedStyles:
669
780
  - strict
670
781
  - flexible
782
+ Exclude:
783
+ - '**/*.gemspec'
784
+
785
+ Rails/TimeZoneAssignment:
786
+ Description: 'Prefer the usage of `Time.use_zone` instead of manually updating `Time.zone` value.'
787
+ Reference: 'https://thoughtbot.com/blog/its-about-time-zones'
788
+ Enabled: 'pending'
789
+ VersionAdded: '2.10'
790
+ Include:
791
+ - spec/**/*.rb
792
+ - test/**/*.rb
671
793
 
672
794
  Rails/UniqBeforePluck:
673
795
  Description: 'Prefer the use of uniq or distinct before pluck.'
@@ -697,6 +819,13 @@ Rails/UnknownEnv:
697
819
  - test
698
820
  - production
699
821
 
822
+ Rails/UnusedIgnoredColumns:
823
+ Description: 'Remove a column that does not exist from `ignored_columns`.'
824
+ Enabled: pending
825
+ VersionAdded: '2.11'
826
+ Include:
827
+ - app/models/**/*.rb
828
+
700
829
  Rails/Validation:
701
830
  Description: 'Use validates :attribute, hash of validations.'
702
831
  Enabled: true
@@ -705,19 +834,28 @@ Rails/Validation:
705
834
  Include:
706
835
  - app/models/**/*.rb
707
836
 
837
+ Rails/WhereEquals:
838
+ Description: 'Pass conditions to `where` as a hash instead of manually constructing SQL.'
839
+ StyleGuide: 'https://rails.rubystyle.guide/#hash-conditions'
840
+ Enabled: 'pending'
841
+ SafeAutoCorrect: false
842
+ VersionAdded: '2.9'
843
+ VersionChanged: '2.10'
844
+
708
845
  Rails/WhereExists:
709
846
  Description: 'Prefer `exists?(...)` over `where(...).exists?`.'
710
847
  Enabled: 'pending'
848
+ SafeAutoCorrect: false
711
849
  EnforcedStyle: exists
712
850
  SupportedStyles:
713
851
  - exists
714
852
  - where
715
853
  VersionAdded: '2.7'
716
- VersionChanged: '2.8'
854
+ VersionChanged: '2.10'
717
855
 
718
856
  Rails/WhereNot:
719
857
  Description: 'Use `where.not(...)` instead of manually constructing negated SQL in `where`.'
720
- StyleGuide: 'https://rails.rubystyle.guide/#where-not'
858
+ StyleGuide: 'https://rails.rubystyle.guide/#hash-conditions'
721
859
  Enabled: 'pending'
722
860
  VersionAdded: '2.8'
723
861
 
@@ -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
 
@@ -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