rubocop-rails 2.7.0 → 2.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -0
  3. data/config/default.yml +78 -4
  4. data/lib/rubocop/cop/mixin/active_record_helper.rb +5 -3
  5. data/lib/rubocop/cop/mixin/enforce_superclass.rb +40 -0
  6. data/lib/rubocop/cop/mixin/index_method.rb +25 -11
  7. data/lib/rubocop/cop/rails/action_filter.rb +10 -14
  8. data/lib/rubocop/cop/rails/active_record_aliases.rb +13 -17
  9. data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +19 -16
  10. data/lib/rubocop/cop/rails/active_record_override.rb +1 -1
  11. data/lib/rubocop/cop/rails/active_support_aliases.rb +12 -21
  12. data/lib/rubocop/cop/rails/after_commit_override.rb +91 -0
  13. data/lib/rubocop/cop/rails/application_controller.rb +3 -7
  14. data/lib/rubocop/cop/rails/application_job.rb +2 -1
  15. data/lib/rubocop/cop/rails/application_mailer.rb +2 -7
  16. data/lib/rubocop/cop/rails/application_record.rb +2 -7
  17. data/lib/rubocop/cop/rails/arel_star.rb +41 -0
  18. data/lib/rubocop/cop/rails/assert_not.rb +8 -10
  19. data/lib/rubocop/cop/rails/attribute_default_block_value.rb +90 -0
  20. data/lib/rubocop/cop/rails/belongs_to.rb +9 -18
  21. data/lib/rubocop/cop/rails/blank.rb +27 -27
  22. data/lib/rubocop/cop/rails/bulk_change_table.rb +1 -1
  23. data/lib/rubocop/cop/rails/content_tag.rb +17 -17
  24. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +2 -1
  25. data/lib/rubocop/cop/rails/date.rb +10 -11
  26. data/lib/rubocop/cop/rails/default_scope.rb +11 -4
  27. data/lib/rubocop/cop/rails/delegate.rb +9 -9
  28. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +7 -8
  29. data/lib/rubocop/cop/rails/dynamic_find_by.rb +13 -11
  30. data/lib/rubocop/cop/rails/enum_hash.rb +11 -10
  31. data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -1
  32. data/lib/rubocop/cop/rails/environment_comparison.rb +18 -14
  33. data/lib/rubocop/cop/rails/exit.rb +4 -10
  34. data/lib/rubocop/cop/rails/file_path.rb +5 -4
  35. data/lib/rubocop/cop/rails/find_by.rb +13 -13
  36. data/lib/rubocop/cop/rails/find_by_id.rb +12 -21
  37. data/lib/rubocop/cop/rails/find_each.rb +17 -18
  38. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +3 -2
  39. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +4 -7
  40. data/lib/rubocop/cop/rails/helper_instance_variable.rb +30 -2
  41. data/lib/rubocop/cop/rails/http_positional_arguments.rb +25 -21
  42. data/lib/rubocop/cop/rails/http_status.rb +7 -9
  43. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +8 -6
  44. data/lib/rubocop/cop/rails/index_by.rb +11 -2
  45. data/lib/rubocop/cop/rails/index_with.rb +11 -2
  46. data/lib/rubocop/cop/rails/inquiry.rb +7 -2
  47. data/lib/rubocop/cop/rails/inverse_of.rb +3 -2
  48. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +17 -15
  49. data/lib/rubocop/cop/rails/link_to_blank.rb +20 -22
  50. data/lib/rubocop/cop/rails/mailer_name.rb +19 -13
  51. data/lib/rubocop/cop/rails/match_route.rb +16 -13
  52. data/lib/rubocop/cop/rails/negate_include.rb +10 -8
  53. data/lib/rubocop/cop/rails/not_null_column.rb +2 -1
  54. data/lib/rubocop/cop/rails/order_by_id.rb +52 -0
  55. data/lib/rubocop/cop/rails/output.rb +5 -2
  56. data/lib/rubocop/cop/rails/output_safety.rb +3 -2
  57. data/lib/rubocop/cop/rails/pick.rb +14 -12
  58. data/lib/rubocop/cop/rails/pluck.rb +6 -9
  59. data/lib/rubocop/cop/rails/pluck_id.rb +4 -6
  60. data/lib/rubocop/cop/rails/pluck_in_where.rb +39 -5
  61. data/lib/rubocop/cop/rails/pluralization_grammar.rb +10 -14
  62. data/lib/rubocop/cop/rails/presence.rb +12 -13
  63. data/lib/rubocop/cop/rails/present.rb +30 -24
  64. data/lib/rubocop/cop/rails/rake_environment.rb +9 -11
  65. data/lib/rubocop/cop/rails/read_write_attribute.rb +12 -11
  66. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +29 -31
  67. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +9 -12
  68. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +11 -10
  69. data/lib/rubocop/cop/rails/reflection_class_name.rb +4 -3
  70. data/lib/rubocop/cop/rails/refute_methods.rb +9 -10
  71. data/lib/rubocop/cop/rails/relative_date_constant.rb +20 -9
  72. data/lib/rubocop/cop/rails/render_inline.rb +5 -12
  73. data/lib/rubocop/cop/rails/render_plain_text.rb +9 -14
  74. data/lib/rubocop/cop/rails/request_referer.rb +7 -7
  75. data/lib/rubocop/cop/rails/reversible_migration.rb +82 -7
  76. data/lib/rubocop/cop/rails/safe_navigation.rb +11 -10
  77. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +5 -10
  78. data/lib/rubocop/cop/rails/save_bang.rb +19 -22
  79. data/lib/rubocop/cop/rails/scope_args.rb +2 -1
  80. data/lib/rubocop/cop/rails/short_i18n.rb +7 -9
  81. data/lib/rubocop/cop/rails/skips_model_validations.rb +4 -4
  82. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +82 -0
  83. data/lib/rubocop/cop/rails/time_zone.rb +22 -20
  84. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +6 -6
  85. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +18 -8
  86. data/lib/rubocop/cop/rails/unknown_env.rb +15 -4
  87. data/lib/rubocop/cop/rails/validation.rb +15 -14
  88. data/lib/rubocop/cop/rails/where_equals.rb +98 -0
  89. data/lib/rubocop/cop/rails/where_exists.rb +74 -16
  90. data/lib/rubocop/cop/rails/where_not.rb +97 -0
  91. data/lib/rubocop/cop/rails_cops.rb +8 -0
  92. data/lib/rubocop/rails/schema_loader.rb +4 -4
  93. data/lib/rubocop/rails/schema_loader/schema.rb +5 -5
  94. data/lib/rubocop/rails/version.rb +5 -1
  95. metadata +23 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a83fc9ded75b4055903b32234c6c9b878e76b22ee64b7e0d88cf2f54ac690d5
4
- data.tar.gz: cb9cbde85b8117645e16c66d1670cffc61893eba8b5d5e3f530e7e42d81b147f
3
+ metadata.gz: 7a4f37840f94de73bdcc15533ef3e5e95ee265a79fd0486b5e813ee926421d17
4
+ data.tar.gz: b2fee8ee1727d8cf33c1d0568f12378f1115d1ae00aa52295656d232de8c59af
5
5
  SHA512:
6
- metadata.gz: 9df513d510b7a71d81bcf655088528ca642599702eca3f20d83eac18adcc15a3d2ab277760b78b9f86fc90df474409eaee454c6857623a2f459304fb74fdde00
7
- data.tar.gz: 252a9b09c51b33b4ff53454fcc6ccaec41e719c283939475e643074305221a33652a9ab5649b9b2c51f36784bb67eeef5dbceee9b3852de936fed78073241dc0
6
+ metadata.gz: d4ceba3963546eaa994918f85d1558223de162f66de58ac48d1e466c2b126facf7548053c00fcbfcd4d05e2d1225eed32e15e6ff921627cfb2a14014119df757
7
+ data.tar.gz: af84d0f778766239432a0b599e21c0e6671b02739769570b2c8655df5f32770f796978747d02a321a5ad1ea2c009d60b7ec3e93b09f669106ad9305a4cc50c3e
data/README.md CHANGED
@@ -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-hq/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-hq/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).
@@ -62,6 +62,14 @@ Rails/ActiveSupportAliases:
62
62
  Enabled: true
63
63
  VersionAdded: '0.48'
64
64
 
65
+ Rails/AfterCommitOverride:
66
+ Description: >-
67
+ This cop enforces that there is only one call to `after_commit`
68
+ (and its aliases - `after_create_commit`, `after_update_commit`,
69
+ and `after_destroy_commit`) with the same callback name per model.
70
+ Enabled: 'pending'
71
+ VersionAdded: '2.8'
72
+
65
73
  Rails/ApplicationController:
66
74
  Description: 'Check that controllers subclass ApplicationController.'
67
75
  Enabled: true
@@ -90,6 +98,12 @@ Rails/ApplicationRecord:
90
98
  VersionAdded: '0.49'
91
99
  VersionChanged: '2.5'
92
100
 
101
+ Rails/ArelStar:
102
+ Description: 'Enforces `Arel.star` instead of `"*"` for expanded columns.'
103
+ Enabled: true
104
+ SafeAutoCorrect: false
105
+ VersionAdded: '2.9'
106
+
93
107
  Rails/AssertNot:
94
108
  Description: 'Use `assert_not` instead of `assert !`.'
95
109
  Enabled: true
@@ -97,6 +111,13 @@ Rails/AssertNot:
97
111
  Include:
98
112
  - '**/test/**/*'
99
113
 
114
+ Rails/AttributeDefaultBlockValue:
115
+ Description: 'Pass method call in block for attribute option `default`.'
116
+ Enabled: pending
117
+ VersionAdded: '2.9'
118
+ Include:
119
+ - 'models/**/*'
120
+
100
121
  Rails/BelongsTo:
101
122
  Description: >-
102
123
  Use `optional: true` instead of `required: false` for
@@ -261,8 +282,15 @@ Rails/FindEach:
261
282
  StyleGuide: 'https://rails.rubystyle.guide#find-each'
262
283
  Enabled: true
263
284
  VersionAdded: '0.30'
285
+ VersionChanged: '2.9'
264
286
  Include:
265
287
  - app/models/**/*.rb
288
+ IgnoredMethods:
289
+ # Methods that don't work well with `find_each`.
290
+ - order
291
+ - limit
292
+ - select
293
+ - lock
266
294
 
267
295
  Rails/HasAndBelongsToMany:
268
296
  Description: 'Prefer has_many :through to has_and_belongs_to_many.'
@@ -313,14 +341,16 @@ Rails/IgnoredSkipActionFilterOption:
313
341
  - app/controllers/**/*.rb
314
342
 
315
343
  Rails/IndexBy:
316
- Description: 'Prefer `index_by` over `each_with_object` or `map`.'
344
+ Description: 'Prefer `index_by` over `each_with_object`, `to_h`, or `map`.'
317
345
  Enabled: true
318
346
  VersionAdded: '2.5'
347
+ VersionChanged: '2.8'
319
348
 
320
349
  Rails/IndexWith:
321
- Description: 'Prefer `index_with` over `each_with_object` or `map`.'
350
+ Description: 'Prefer `index_with` over `each_with_object`, `to_h`, or `map`.'
322
351
  Enabled: true
323
352
  VersionAdded: '2.5'
353
+ VersionChanged: '2.8'
324
354
 
325
355
  Rails/Inquiry:
326
356
  Description: "Prefer Ruby's comparison operators over Active Support's `Array#inquiry` and `String#inquiry`."
@@ -357,6 +387,7 @@ Rails/MailerName:
357
387
  Description: 'Mailer should end with `Mailer` suffix.'
358
388
  StyleGuide: 'https://rails.rubystyle.guide/#mailer-name'
359
389
  Enabled: 'pending'
390
+ SafeAutoCorrect: false
360
391
  VersionAdded: '2.7'
361
392
  Include:
362
393
  - app/mailers/**/*.rb
@@ -376,7 +407,9 @@ Rails/NegateInclude:
376
407
  Description: 'Prefer `collection.exclude?(obj)` over `!collection.include?(obj)`.'
377
408
  StyleGuide: 'https://rails.rubystyle.guide#exclude'
378
409
  Enabled: 'pending'
410
+ Safe: false
379
411
  VersionAdded: '2.7'
412
+ VersionChanged: '2.9'
380
413
 
381
414
  Rails/NotNullColumn:
382
415
  Description: 'Do not add a NOT NULL column without a default value.'
@@ -385,6 +418,14 @@ Rails/NotNullColumn:
385
418
  Include:
386
419
  - db/migrate/*.rb
387
420
 
421
+ Rails/OrderById:
422
+ Description: >-
423
+ Do not use the `id` column for ordering.
424
+ Use a timestamp column to order chronologically.
425
+ StyleGuide: 'https://rails.rubystyle.guide/#order-by-id'
426
+ Enabled: false
427
+ VersionAdded: '2.8'
428
+
388
429
  Rails/Output:
389
430
  Description: 'Checks for calls to puts, print, etc.'
390
431
  Enabled: true
@@ -417,7 +458,7 @@ Rails/Pluck:
417
458
  Rails/PluckId:
418
459
  Description: 'Use `ids` instead of `pluck(:id)` or `pluck(primary_key)`.'
419
460
  StyleGuide: 'https://rails.rubystyle.guide/#ids'
420
- Enabled: 'pending'
461
+ Enabled: false
421
462
  Safe: false
422
463
  VersionAdded: '2.7'
423
464
 
@@ -426,6 +467,11 @@ Rails/PluckInWhere:
426
467
  Enabled: 'pending'
427
468
  Safe: false
428
469
  VersionAdded: '2.7'
470
+ VersionChanged: '2.8'
471
+ EnforcedStyle: conservative
472
+ SupportedStyles:
473
+ - conservative
474
+ - aggressive
429
475
 
430
476
  Rails/PluralizationGrammar:
431
477
  Description: 'Checks for incorrect grammar when using methods like `3.day.ago`.'
@@ -624,6 +670,16 @@ Rails/SkipsModelValidations:
624
670
  - upsert_all
625
671
  AllowedMethods: []
626
672
 
673
+ Rails/SquishedSQLHeredocs:
674
+ Description: 'Checks SQL heredocs to use `.squish`.'
675
+ StyleGuide: 'https://rails.rubystyle.guide/#squished-heredocs'
676
+ Enabled: 'pending'
677
+ VersionAdded: '2.8'
678
+ VersionChanged: '2.9'
679
+ # Some SQL syntax (e.g. PostgreSQL comments and functions) requires newlines
680
+ # to be preserved in order to work, thus auto-correction is not safe.
681
+ SafeAutoCorrect: false
682
+
627
683
  Rails/TimeZone:
628
684
  Description: 'Checks the correct usage of time zone aware methods.'
629
685
  StyleGuide: 'https://rails.rubystyle.guide#time'
@@ -643,11 +699,12 @@ Rails/UniqBeforePluck:
643
699
  Description: 'Prefer the use of uniq or distinct before pluck.'
644
700
  Enabled: true
645
701
  VersionAdded: '0.40'
646
- VersionChanged: '2.6'
702
+ VersionChanged: '2.8'
647
703
  EnforcedStyle: conservative
648
704
  SupportedStyles:
649
705
  - conservative
650
706
  - aggressive
707
+ SafeAutoCorrect: false
651
708
  AutoCorrect: false
652
709
 
653
710
  Rails/UniqueValidationWithoutIndex:
@@ -674,10 +731,27 @@ Rails/Validation:
674
731
  Include:
675
732
  - app/models/**/*.rb
676
733
 
734
+ Rails/WhereEquals:
735
+ Description: 'Pass conditions to `where` as a hash instead of manually constructing SQL.'
736
+ StyleGuide: 'https://rails.rubystyle.guide/#hash-conditions'
737
+ Enabled: 'pending'
738
+ VersionAdded: '2.9'
739
+
677
740
  Rails/WhereExists:
678
741
  Description: 'Prefer `exists?(...)` over `where(...).exists?`.'
679
742
  Enabled: 'pending'
743
+ EnforcedStyle: exists
744
+ SupportedStyles:
745
+ - exists
746
+ - where
680
747
  VersionAdded: '2.7'
748
+ VersionChanged: '2.8'
749
+
750
+ Rails/WhereNot:
751
+ Description: 'Use `where.not(...)` instead of manually constructing negated SQL in `where`.'
752
+ StyleGuide: 'https://rails.rubystyle.guide/#hash-conditions'
753
+ Enabled: 'pending'
754
+ VersionAdded: '2.8'
681
755
 
682
756
  # Accept `redirect_to(...) and return` and similar cases.
683
757
  Style/AndOr:
@@ -35,10 +35,11 @@ module RuboCop
35
35
  table_name = find_set_table_name(class_node).to_a.last&.first_argument
36
36
  return table_name.value.to_s if table_name
37
37
 
38
- namespaces = class_node.each_ancestor(:class, :module)
39
- [class_node, *namespaces]
38
+ class_nodes = class_node.defined_module.each_node
39
+ namespaces = class_node.each_ancestor(:class, :module).map(&:identifier)
40
+ [*class_nodes, *namespaces]
40
41
  .reverse
41
- .map { |klass| klass.identifier.children[1] }.join('_')
42
+ .map { |node| node.children[1] }.join('_')
42
43
  .tableize
43
44
  end
44
45
 
@@ -52,6 +53,7 @@ module RuboCop
52
53
  # @param table [RuboCop::Rails::SchemaLoader::Table]
53
54
  # @return [String, nil]
54
55
  def resolve_relation_into_column(name:, class_node:, table:)
56
+ return unless table
55
57
  return name if table.with_column?(name: name)
56
58
 
57
59
  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
@@ -4,10 +4,18 @@ module RuboCop
4
4
  module Cop
5
5
  # Common functionality for Rails/IndexBy and Rails/IndexWith
6
6
  module IndexMethod # rubocop:disable Metrics/ModuleLength
7
+ RESTRICT_ON_SEND = %i[each_with_object to_h map collect []].freeze
8
+
7
9
  def on_block(node)
8
10
  on_bad_each_with_object(node) do |*match|
9
11
  handle_possible_offense(node, match, 'each_with_object')
10
12
  end
13
+
14
+ return if target_ruby_version < 2.6
15
+
16
+ on_bad_to_h(node) do |*match|
17
+ handle_possible_offense(node, match, 'to_h { ... }')
18
+ end
11
19
  end
12
20
 
13
21
  def on_send(node)
@@ -26,13 +34,6 @@ module RuboCop
26
34
  end
27
35
  end
28
36
 
29
- def autocorrect(node)
30
- lambda do |corrector|
31
- correction = prepare_correction(node)
32
- execute_correction(corrector, node, correction)
33
- end
34
- end
35
-
36
37
  private
37
38
 
38
39
  # @abstract Implemented with `def_node_matcher`
@@ -40,6 +41,11 @@ module RuboCop
40
41
  raise NotImplementedError
41
42
  end
42
43
 
44
+ # @abstract Implemented with `def_node_matcher`
45
+ def on_bad_to_h(_node)
46
+ raise NotImplementedError
47
+ end
48
+
43
49
  # @abstract Implemented with `def_node_matcher`
44
50
  def on_bad_map_to_h(_node)
45
51
  raise NotImplementedError
@@ -56,9 +62,11 @@ module RuboCop
56
62
  return if captures.noop_transformation?
57
63
 
58
64
  add_offense(
59
- node,
60
- message: "Prefer `#{new_method_name}` over `#{match_desc}`."
61
- )
65
+ node, message: "Prefer `#{new_method_name}` over `#{match_desc}`."
66
+ ) do |corrector|
67
+ correction = prepare_correction(node)
68
+ execute_correction(corrector, node, correction)
69
+ end
62
70
  end
63
71
 
64
72
  def extract_captures(match)
@@ -73,6 +81,8 @@ module RuboCop
73
81
  def prepare_correction(node)
74
82
  if (match = on_bad_each_with_object(node))
75
83
  Autocorrection.from_each_with_object(node, match)
84
+ elsif (match = on_bad_to_h(node))
85
+ Autocorrection.from_to_h(node, match)
76
86
  elsif (match = on_bad_map_to_h(node))
77
87
  Autocorrection.from_map_to_h(node, match)
78
88
  elsif (match = on_bad_hash_brackets_map(node))
@@ -106,11 +116,15 @@ module RuboCop
106
116
  end
107
117
 
108
118
  # Internal helper class to hold autocorrect data
109
- Autocorrection = Struct.new(:match, :block_node, :leading, :trailing) do # rubocop:disable Metrics/BlockLength
119
+ Autocorrection = Struct.new(:match, :block_node, :leading, :trailing) do
110
120
  def self.from_each_with_object(node, match)
111
121
  new(match, node, 0, 0)
112
122
  end
113
123
 
124
+ def self.from_to_h(node, match)
125
+ new(match, node, 0, 0)
126
+ end
127
+
114
128
  def self.from_map_to_h(node, match)
115
129
  strip_trailing_chars = 0
116
130
 
@@ -29,8 +29,9 @@ module RuboCop
29
29
  # after_filter :do_stuff
30
30
  # append_around_filter :do_stuff
31
31
  # skip_after_filter :do_stuff
32
- class ActionFilter < Cop
32
+ class ActionFilter < Base
33
33
  include ConfigurableEnforcedStyle
34
+ extend AutoCorrector
34
35
 
35
36
  MSG = 'Prefer `%<prefer>s` over `%<current>s`.'
36
37
 
@@ -66,6 +67,8 @@ module RuboCop
66
67
  skip_action_callback
67
68
  ].freeze
68
69
 
70
+ RESTRICT_ON_SEND = FILTER_METHODS + ACTION_METHODS
71
+
69
72
  def on_block(node)
70
73
  check_method_node(node.send_node)
71
74
  end
@@ -74,24 +77,17 @@ module RuboCop
74
77
  check_method_node(node) unless node.receiver
75
78
  end
76
79
 
77
- def autocorrect(node)
78
- lambda do |corrector|
79
- corrector.replace(node.loc.selector,
80
- preferred_method(node.loc.selector.source).to_s)
81
- end
82
- end
83
-
84
80
  private
85
81
 
86
82
  def check_method_node(node)
87
- return unless bad_methods.include?(node.method_name)
83
+ method_name = node.method_name
84
+ return unless bad_methods.include?(method_name)
88
85
 
89
- add_offense(node, location: :selector)
90
- end
86
+ message = format(MSG, prefer: preferred_method(method_name), current: method_name)
91
87
 
92
- def message(node)
93
- format(MSG, prefer: preferred_method(node.method_name),
94
- current: node.method_name)
88
+ add_offense(node.loc.selector, message: message) do |corrector|
89
+ corrector.replace(node.loc.selector, preferred_method(node.loc.selector.source))
90
+ end
95
91
  end
96
92
 
97
93
  def bad_methods
@@ -12,7 +12,9 @@ module RuboCop
12
12
  #
13
13
  # #good
14
14
  # Book.update!(author: 'Alice')
15
- class ActiveRecordAliases < Cop
15
+ class ActiveRecordAliases < Base
16
+ extend AutoCorrector
17
+
16
18
  MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
17
19
 
18
20
  ALIASES = {
@@ -20,28 +22,22 @@ module RuboCop
20
22
  update_attributes!: :update!
21
23
  }.freeze
22
24
 
25
+ RESTRICT_ON_SEND = ALIASES.keys.freeze
26
+
23
27
  def on_send(node)
24
- ALIASES.each do |bad, good|
25
- next unless node.method?(bad)
28
+ method_name = node.method_name
29
+ alias_method = ALIASES[method_name]
26
30
 
27
- add_offense(node,
28
- message: format(MSG, prefer: good, current: bad),
29
- location: :selector,
30
- severity: :warning)
31
- break
31
+ add_offense(
32
+ node.loc.selector,
33
+ message: format(MSG, prefer: alias_method, current: method_name),
34
+ severity: :warning
35
+ ) do |corrector|
36
+ corrector.replace(node.loc.selector, alias_method)
32
37
  end
33
38
  end
34
39
 
35
40
  alias on_csend on_send
36
-
37
- def autocorrect(node)
38
- lambda do |corrector|
39
- corrector.replace(
40
- node.loc.selector,
41
- ALIASES[node.method_name].to_s
42
- )
43
- end
44
- end
45
41
  end
46
42
  end
47
43
  end