rubocop-rails 2.15.2 → 2.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +84 -9
  4. data/config/default.yml +417 -82
  5. data/config/obsoletion.yml +10 -0
  6. data/lib/rubocop/cop/mixin/active_record_helper.rb +19 -10
  7. data/lib/rubocop/cop/mixin/active_record_migrations_helper.rb +3 -5
  8. data/lib/rubocop/cop/mixin/database_type_resolvable.rb +66 -0
  9. data/lib/rubocop/cop/mixin/enforce_superclass.rb +7 -2
  10. data/lib/rubocop/cop/mixin/index_method.rb +73 -71
  11. data/lib/rubocop/cop/mixin/migrations_helper.rb +1 -1
  12. data/lib/rubocop/cop/mixin/routes_helper.rb +20 -0
  13. data/lib/rubocop/cop/mixin/target_rails_version.rb +17 -2
  14. data/lib/rubocop/cop/mixin.rb +17 -0
  15. data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +116 -0
  16. data/lib/rubocop/cop/rails/action_controller_test_case.rb +4 -4
  17. data/lib/rubocop/cop/rails/action_filter.rb +4 -1
  18. data/lib/rubocop/cop/rails/action_order.rb +112 -0
  19. data/lib/rubocop/cop/rails/active_record_aliases.rb +5 -6
  20. data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +3 -4
  21. data/lib/rubocop/cop/rails/active_record_override.rb +2 -5
  22. data/lib/rubocop/cop/rails/active_support_aliases.rb +6 -5
  23. data/lib/rubocop/cop/rails/active_support_on_load.rb +90 -0
  24. data/lib/rubocop/cop/rails/add_column_index.rb +3 -5
  25. data/lib/rubocop/cop/rails/after_commit_override.rb +1 -1
  26. data/lib/rubocop/cop/rails/application_controller.rb +2 -3
  27. data/lib/rubocop/cop/rails/application_job.rb +3 -4
  28. data/lib/rubocop/cop/rails/application_mailer.rb +2 -3
  29. data/lib/rubocop/cop/rails/application_record.rb +6 -3
  30. data/lib/rubocop/cop/rails/arel_star.rb +6 -6
  31. data/lib/rubocop/cop/rails/assert_not.rb +1 -2
  32. data/lib/rubocop/cop/rails/attribute_default_block_value.rb +9 -0
  33. data/lib/rubocop/cop/rails/belongs_to.rb +2 -5
  34. data/lib/rubocop/cop/rails/blank.rb +7 -8
  35. data/lib/rubocop/cop/rails/bulk_change_table.rb +26 -69
  36. data/lib/rubocop/cop/rails/compact_blank.rb +34 -9
  37. data/lib/rubocop/cop/rails/content_tag.rb +7 -8
  38. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +16 -3
  39. data/lib/rubocop/cop/rails/dangerous_column_names.rb +447 -0
  40. data/lib/rubocop/cop/rails/date.rb +18 -14
  41. data/lib/rubocop/cop/rails/delegate.rb +71 -14
  42. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +1 -1
  43. data/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb +18 -14
  44. data/lib/rubocop/cop/rails/dot_separated_keys.rb +2 -2
  45. data/lib/rubocop/cop/rails/duplicate_association.rb +71 -10
  46. data/lib/rubocop/cop/rails/duplicate_scope.rb +2 -2
  47. data/lib/rubocop/cop/rails/duration_arithmetic.rb +3 -3
  48. data/lib/rubocop/cop/rails/dynamic_find_by.rb +28 -16
  49. data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +9 -7
  50. data/lib/rubocop/cop/rails/enum_hash.rb +31 -8
  51. data/lib/rubocop/cop/rails/enum_syntax.rb +130 -0
  52. data/lib/rubocop/cop/rails/enum_uniqueness.rb +30 -11
  53. data/lib/rubocop/cop/rails/env.rb +70 -0
  54. data/lib/rubocop/cop/rails/env_local.rb +93 -0
  55. data/lib/rubocop/cop/rails/environment_comparison.rb +56 -49
  56. data/lib/rubocop/cop/rails/exit.rb +7 -4
  57. data/lib/rubocop/cop/rails/expanded_date_range.rb +1 -1
  58. data/lib/rubocop/cop/rails/file_path.rb +214 -32
  59. data/lib/rubocop/cop/rails/find_by.rb +4 -4
  60. data/lib/rubocop/cop/rails/find_by_id.rb +11 -25
  61. data/lib/rubocop/cop/rails/find_by_or_assignment_memoization.rb +124 -0
  62. data/lib/rubocop/cop/rails/find_each.rb +15 -5
  63. data/lib/rubocop/cop/rails/freeze_time.rb +79 -0
  64. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +5 -7
  65. data/lib/rubocop/cop/rails/helper_instance_variable.rb +17 -18
  66. data/lib/rubocop/cop/rails/http_positional_arguments.rb +35 -11
  67. data/lib/rubocop/cop/rails/http_status.rb +22 -16
  68. data/lib/rubocop/cop/rails/http_status_name_consistency.rb +80 -0
  69. data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +65 -13
  70. data/lib/rubocop/cop/rails/i18n_locale_texts.rb +29 -3
  71. data/lib/rubocop/cop/rails/ignored_columns_assignment.rb +50 -0
  72. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +24 -13
  73. data/lib/rubocop/cop/rails/index_by.rb +38 -13
  74. data/lib/rubocop/cop/rails/index_with.rb +43 -13
  75. data/lib/rubocop/cop/rails/inquiry.rb +2 -1
  76. data/lib/rubocop/cop/rails/inverse_of.rb +9 -9
  77. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +38 -23
  78. data/lib/rubocop/cop/rails/link_to_blank.rb +3 -6
  79. data/lib/rubocop/cop/rails/mailer_name.rb +4 -4
  80. data/lib/rubocop/cop/rails/match_route.rb +1 -9
  81. data/lib/rubocop/cop/rails/migration_class_name.rb +1 -1
  82. data/lib/rubocop/cop/rails/multiple_route_paths.rb +50 -0
  83. data/lib/rubocop/cop/rails/negate_include.rb +1 -1
  84. data/lib/rubocop/cop/rails/not_null_column.rb +108 -9
  85. data/lib/rubocop/cop/rails/order_arguments.rb +84 -0
  86. data/lib/rubocop/cop/rails/output.rb +8 -8
  87. data/lib/rubocop/cop/rails/output_safety.rb +8 -2
  88. data/lib/rubocop/cop/rails/pick.rb +10 -5
  89. data/lib/rubocop/cop/rails/pluck.rb +73 -13
  90. data/lib/rubocop/cop/rails/pluck_id.rb +3 -2
  91. data/lib/rubocop/cop/rails/pluck_in_where.rb +35 -13
  92. data/lib/rubocop/cop/rails/pluralization_grammar.rb +30 -17
  93. data/lib/rubocop/cop/rails/presence.rb +94 -29
  94. data/lib/rubocop/cop/rails/present.rb +9 -14
  95. data/lib/rubocop/cop/rails/rake_environment.rb +23 -7
  96. data/lib/rubocop/cop/rails/read_write_attribute.rb +2 -2
  97. data/lib/rubocop/cop/rails/redirect_back_or_to.rb +98 -0
  98. data/lib/rubocop/cop/rails/redundant_active_record_all_method.rb +190 -0
  99. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +5 -7
  100. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +2 -2
  101. data/lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb +21 -5
  102. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +34 -26
  103. data/lib/rubocop/cop/rails/reflection_class_name.rb +34 -1
  104. data/lib/rubocop/cop/rails/refute_methods.rb +1 -6
  105. data/lib/rubocop/cop/rails/relative_date_constant.rb +5 -8
  106. data/lib/rubocop/cop/rails/render_plain_text.rb +6 -3
  107. data/lib/rubocop/cop/rails/request_referer.rb +2 -3
  108. data/lib/rubocop/cop/rails/require_dependency.rb +1 -1
  109. data/lib/rubocop/cop/rails/response_parsed_body.rb +108 -0
  110. data/lib/rubocop/cop/rails/reversible_migration.rb +28 -65
  111. data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +1 -2
  112. data/lib/rubocop/cop/rails/root_join_chain.rb +1 -1
  113. data/lib/rubocop/cop/rails/root_pathname_methods.rb +281 -0
  114. data/lib/rubocop/cop/rails/safe_navigation.rb +34 -6
  115. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +1 -3
  116. data/lib/rubocop/cop/rails/save_bang.rb +42 -39
  117. data/lib/rubocop/cop/rails/schema_comment.rb +18 -11
  118. data/lib/rubocop/cop/rails/select_map.rb +100 -0
  119. data/lib/rubocop/cop/rails/short_i18n.rb +2 -5
  120. data/lib/rubocop/cop/rails/skips_model_validations.rb +11 -7
  121. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +10 -8
  122. data/lib/rubocop/cop/rails/strip_heredoc.rb +1 -1
  123. data/lib/rubocop/cop/rails/strong_parameters_expect.rb +212 -0
  124. data/lib/rubocop/cop/rails/three_state_boolean_column.rb +72 -0
  125. data/lib/rubocop/cop/rails/time_zone.rb +54 -35
  126. data/lib/rubocop/cop/rails/time_zone_assignment.rb +1 -1
  127. data/lib/rubocop/cop/rails/to_s_with_argument.rb +78 -0
  128. data/lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb +49 -0
  129. data/lib/rubocop/cop/rails/transaction_exit_statement.rb +42 -10
  130. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +11 -29
  131. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +25 -22
  132. data/lib/rubocop/cop/rails/unknown_env.rb +40 -11
  133. data/lib/rubocop/cop/rails/unused_ignored_columns.rb +12 -1
  134. data/lib/rubocop/cop/rails/unused_render_content.rb +67 -0
  135. data/lib/rubocop/cop/rails/validation.rb +13 -16
  136. data/lib/rubocop/cop/rails/where_equals.rb +29 -13
  137. data/lib/rubocop/cop/rails/where_exists.rb +14 -14
  138. data/lib/rubocop/cop/rails/where_missing.rb +121 -0
  139. data/lib/rubocop/cop/rails/where_not.rb +18 -12
  140. data/lib/rubocop/cop/rails/where_not_with_multiple_conditions.rb +55 -0
  141. data/lib/rubocop/cop/rails/where_range.rb +202 -0
  142. data/lib/rubocop/cop/rails.rb +152 -0
  143. data/lib/rubocop/cop/rails_cops.rb +4 -120
  144. data/lib/rubocop/rails/migration_file_skippable.rb +50 -0
  145. data/lib/rubocop/rails/plugin.rb +48 -0
  146. data/lib/rubocop/rails/schema_loader/schema.rb +8 -7
  147. data/lib/rubocop/rails/schema_loader.rb +5 -15
  148. data/lib/rubocop/rails/version.rb +1 -1
  149. data/lib/rubocop/rails.rb +1 -8
  150. data/lib/rubocop-rails.rb +30 -3
  151. metadata +74 -13
  152. data/bin/console +0 -11
  153. data/bin/setup +0 -7
  154. data/lib/rubocop/rails/inject.rb +0 -18
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 169ed04b1f5378e9e08fbffdca151afaf4b84fd54b1867a173740aadaede60b3
4
- data.tar.gz: 1a2ac75c313cfcdeec1814d7e3aff4777c5013527d2cf4a2aa4f95aeb45446bf
3
+ metadata.gz: 9258d56d31fc4adeef0526b551dbe652abe70dd4599debc25ee39be54b743330
4
+ data.tar.gz: ab2d60fe9b2a61ac47f8f15f8753fb60dd26e278d5452d23501f59a398fd7b0a
5
5
  SHA512:
6
- metadata.gz: 51d495edd3e825db49ece8f51bfc118c73887681f414b86d31cd47d85dc5a1a22a385a2a17ed4ebf7d1c6332989795e2cde999a364d911c60f452993dc926c4b
7
- data.tar.gz: 4546785b39af93e6c7fe5a6a3b637fa5c21d4b358c38ea8ad3ebd5c272d1d0a504e8206e8201cfe3771af9199c336649d033e32b324f16b06fc7d150054dc488
6
+ metadata.gz: 7ba4c487f87fdbd58130ba06fc0184c9c5b5dabe5ee3934aa03930c27467072a7f89ac77ba423fe89f69ad9739c2dfcbeac1bb947e64804757e6efbdb47b47a5
7
+ data.tar.gz: 223e0693edfd7de08b9d6a770fbdd7ced5c6422cab60d5074657ef65dd7b4ce538c8f61c37cc15156d63824e1d4060a654bca1fef7830d1b587e7ca84d516f62
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012-22 Bozhidar Batsov
1
+ Copyright (c) 2012-26 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,18 +1,19 @@
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/rubocop-rails.svg?style=svg)](https://circleci.com/gh/rubocop/rubocop-rails)
4
+ [![CI](https://github.com/rubocop/rubocop-rails/actions/workflows/test.yml/badge.svg)](https://github.com/rubocop/rubocop-rails/actions/workflows/test.yml)
5
5
 
6
6
  A [RuboCop](https://github.com/rubocop/rubocop) extension focused on enforcing Rails best practices and coding conventions.
7
7
 
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.
8
+ > [!IMPORTANT]
9
+ > 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
10
 
10
11
  ## Installation
11
12
 
12
13
  Just install the `rubocop-rails` gem
13
14
 
14
15
  ```sh
15
- gem install rubocop-rails
16
+ $ gem install rubocop-rails
16
17
  ```
17
18
 
18
19
  or if you use bundler put this in your `Gemfile`
@@ -31,13 +32,13 @@ ways to do this:
31
32
  Put this into your `.rubocop.yml`.
32
33
 
33
34
  ```yaml
34
- require: rubocop-rails
35
+ plugins: rubocop-rails
35
36
  ```
36
37
 
37
38
  Alternatively, use the following array notation when specifying multiple extensions.
38
39
 
39
40
  ```yaml
40
- require:
41
+ plugins:
41
42
  - rubocop-other-extension
42
43
  - rubocop-rails
43
44
  ```
@@ -45,22 +46,96 @@ require:
45
46
  Now you can run `rubocop` and it will automatically load the RuboCop Rails
46
47
  cops together with the standard cops.
47
48
 
49
+ > [!NOTE]
50
+ > The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`.
51
+
48
52
  ### Command line
49
53
 
50
54
  ```sh
51
- rubocop --require rubocop-rails
55
+ $ rubocop --plugin rubocop-rails
52
56
  ```
53
57
 
54
- Note: `--rails` option is required while `rubocop` command supports `--rails` option.
55
-
56
58
  ### Rake task
57
59
 
58
60
  ```ruby
61
+ require 'rubocop/rake_task'
62
+
59
63
  RuboCop::RakeTask.new do |task|
60
- task.requires << 'rubocop-rails'
64
+ task.plugins << 'rubocop-rails'
65
+ end
66
+ ```
67
+
68
+ ## RuboCop Rails configuration
69
+
70
+ The following settings specific to RuboCop Rails can be configured in `.rubocop.yml`.
71
+
72
+ ### `AllCops: TargetRailsVersion`
73
+
74
+ What version of Rails is the inspected code using? If a value is specified
75
+ for `TargetRailsVersion` then it is used. Acceptable values are specified
76
+ as a float (e.g., 7.2); the patch version of Rails should not be included.
77
+
78
+ ```yaml
79
+ AllCops:
80
+ TargetRailsVersion: 7.2
81
+ ```
82
+
83
+ If `TargetRailsVersion` is not set, RuboCop will parse the Gemfile.lock or
84
+ gems.locked file to find the version of Rails that has been bound to the
85
+ application. If neither of those files exist, RuboCop will use Rails 5.0
86
+ as the default.
87
+
88
+ ### `AllCops: MigratedSchemaVersion`
89
+
90
+ By specifying the `MigratedSchemaVersion` option, migration files that have already been run can be ignored.
91
+ When `MigratedSchemaVersion: '20241225000000'` is set, migration files lower than or equal to '20241225000000' will be ignored.
92
+ For example, to ignore db/migrate/20241225000000_create_articles.rb and earlier migrations you would configure it the following way:
93
+
94
+ ```yaml
95
+ AllCops:
96
+ MigratedSchemaVersion: '20241225000000'
97
+ ```
98
+
99
+ This prevents inspecting schema settings for already applied migration files.
100
+ Changing already applied migrations should be avoided because it can lead to the schema getting out of sync
101
+ between your local copy and what it actually is in production, depending on when `bin/rails db:migrate` was executed.
102
+ If you want to modify your schema to comply with the cops, you should instead create new migrations.
103
+
104
+ ## Rails configuration tip
105
+
106
+ In Rails 6.1+, add the following `config.generators.after_generate` setting to
107
+ your `config/environments/development.rb` to apply RuboCop autocorrection to code generated by `bin/rails g`.
108
+
109
+ ```ruby
110
+ # config/environments/development.rb
111
+ Rails.application.configure do
112
+ config.generators.after_generate do |files|
113
+ parsable_files = files.filter { |file| file.end_with?('.rb') }
114
+ unless parsable_files.empty?
115
+ system("bundle exec rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true)
116
+ end
117
+ end
61
118
  end
62
119
  ```
63
120
 
121
+ It uses `rubocop -A` to apply `Style/FrozenStringLiteralComment` and other unsafe autocorrection cops.
122
+ `rubocop -A` is unsafe autocorrection, but code generated by default is simple and less likely to
123
+ be incompatible with `rubocop -A`. If you have problems you can replace it with `rubocop -a` instead.
124
+
125
+ In Rails 7.2+, it is recommended to use `config.generators.apply_rubocop_autocorrect_after_generate!` instead of the above setting:
126
+
127
+ ```diff
128
+ # config/environments/development.rb
129
+ Rails.application.configure do
130
+ (snip)
131
+ # Apply autocorrection by RuboCop to files generated by `bin/rails generate`.
132
+ - # config.generators.apply_rubocop_autocorrect_after_generate!
133
+ + config.generators.apply_rubocop_autocorrect_after_generate!
134
+ end
135
+ ```
136
+
137
+ You only need to uncomment.
138
+
64
139
  ## The Cops
65
140
 
66
141
  All cops are located under