rubocop-rails 2.12.1 → 2.13.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 (43) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +43 -8
  3. data/lib/rubocop/cop/mixin/active_record_migrations_helper.rb +34 -0
  4. data/lib/rubocop/cop/rails/active_record_aliases.rb +6 -2
  5. data/lib/rubocop/cop/rails/application_controller.rb +5 -1
  6. data/lib/rubocop/cop/rails/application_job.rb +5 -1
  7. data/lib/rubocop/cop/rails/application_mailer.rb +5 -1
  8. data/lib/rubocop/cop/rails/application_record.rb +6 -1
  9. data/lib/rubocop/cop/rails/arel_star.rb +6 -0
  10. data/lib/rubocop/cop/rails/blank.rb +5 -4
  11. data/lib/rubocop/cop/rails/compact_blank.rb +98 -0
  12. data/lib/rubocop/cop/rails/content_tag.rb +15 -8
  13. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +2 -7
  14. data/lib/rubocop/cop/rails/duration_arithmetic.rb +97 -0
  15. data/lib/rubocop/cop/rails/dynamic_find_by.rb +4 -0
  16. data/lib/rubocop/cop/rails/find_each.rb +13 -0
  17. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +1 -1
  18. data/lib/rubocop/cop/rails/http_positional_arguments.rb +1 -1
  19. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +8 -7
  20. data/lib/rubocop/cop/rails/mailer_name.rb +4 -0
  21. data/lib/rubocop/cop/rails/negate_include.rb +3 -2
  22. data/lib/rubocop/cop/rails/output.rb +4 -0
  23. data/lib/rubocop/cop/rails/pick.rb +7 -0
  24. data/lib/rubocop/cop/rails/pluck_id.rb +3 -0
  25. data/lib/rubocop/cop/rails/pluck_in_where.rb +7 -6
  26. data/lib/rubocop/cop/rails/rake_environment.rb +5 -0
  27. data/lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb +192 -0
  28. data/lib/rubocop/cop/rails/reflection_class_name.rb +4 -2
  29. data/lib/rubocop/cop/rails/relative_date_constant.rb +4 -1
  30. data/lib/rubocop/cop/rails/reversible_migration.rb +11 -3
  31. data/lib/rubocop/cop/rails/root_join_chain.rb +72 -0
  32. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +12 -3
  33. data/lib/rubocop/cop/rails/save_bang.rb +19 -0
  34. data/lib/rubocop/cop/rails/schema_comment.rb +104 -0
  35. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +4 -2
  36. data/lib/rubocop/cop/rails/time_zone.rb +3 -0
  37. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +29 -35
  38. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +1 -1
  39. data/lib/rubocop/cop/rails/where_equals.rb +4 -0
  40. data/lib/rubocop/cop/rails/where_exists.rb +9 -8
  41. data/lib/rubocop/cop/rails_cops.rb +6 -0
  42. data/lib/rubocop/rails/version.rb +1 -1
  43. metadata +11 -4
@@ -3,47 +3,46 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # Prefer the use of distinct, before pluck instead of after.
6
+ # Prefer using `distinct` before `pluck` instead of `uniq` after `pluck`.
7
7
  #
8
- # The use of distinct before pluck is preferred because it executes within
8
+ # The use of distinct before pluck is preferred because it executes by
9
9
  # the database.
10
10
  #
11
11
  # This cop has two different enforcement modes. When the EnforcedStyle
12
- # is conservative (the default) then only calls to pluck on a constant
13
- # (i.e. a model class) before distinct are added as offenses.
12
+ # is `conservative` (the default), then only calls to `pluck` on a constant
13
+ # (i.e. a model class) before `uniq` are added as offenses.
14
14
  #
15
- # When the EnforcedStyle is aggressive then all calls to pluck before
15
+ # When the EnforcedStyle is `aggressive` then all calls to `pluck` before
16
16
  # distinct are added as offenses. This may lead to false positives
17
- # as the cop cannot distinguish between calls to pluck on an
17
+ # as the cop cannot distinguish between calls to `pluck` on an
18
18
  # ActiveRecord::Relation vs a call to pluck on an
19
19
  # ActiveRecord::Associations::CollectionProxy.
20
20
  #
21
- # This cop is unsafe because the behavior may change depending on the
22
- # database collation.
23
- # Autocorrect is disabled by default for this cop since it may generate
24
- # false positives.
21
+ # @safety
22
+ # This cop is unsafe for autocorrection because the behavior may change
23
+ # depending on the database collation.
25
24
  #
26
25
  # @example EnforcedStyle: conservative (default)
27
- # # bad
28
- # Model.pluck(:id).uniq
26
+ # # bad - redundantly fetches duplicate values
27
+ # Album.pluck(:band_name).uniq
29
28
  #
30
29
  # # good
31
- # Model.distinct.pluck(:id)
30
+ # Album.distinct.pluck(:band_name)
32
31
  #
33
32
  # @example EnforcedStyle: aggressive
34
- # # bad
35
- # # this will return a Relation that pluck is called on
36
- # Model.where(cond: true).pluck(:id).uniq
33
+ # # bad - redundantly fetches duplicate values
34
+ # Album.pluck(:band_name).uniq
37
35
  #
38
- # # bad
39
- # # an association on an instance will return a CollectionProxy
40
- # instance.assoc.pluck(:id).uniq
36
+ # # bad - redundantly fetches duplicate values
37
+ # Album.where(year: 1985).pluck(:band_name).uniq
41
38
  #
42
- # # bad
43
- # Model.pluck(:id).uniq
39
+ # # bad - redundantly fetches duplicate values
40
+ # customer.favourites.pluck(:color).uniq
44
41
  #
45
42
  # # good
46
- # Model.distinct.pluck(:id)
43
+ # Album.distinct.pluck(:band_name)
44
+ # Album.distinct.where(year: 1985).pluck(:band_name)
45
+ # customer.favourites.distinct.pluck(:color)
47
46
  #
48
47
  class UniqBeforePluck < Base
49
48
  include ConfigurableEnforcedStyle
@@ -51,10 +50,9 @@ module RuboCop
51
50
  extend AutoCorrector
52
51
 
53
52
  MSG = 'Use `distinct` before `pluck`.'
54
- RESTRICT_ON_SEND = %i[uniq distinct pluck].freeze
53
+ RESTRICT_ON_SEND = %i[uniq].freeze
55
54
  NEWLINE = "\n"
56
- PATTERN = '[!^block (send (send %<type>s :pluck ...) ' \
57
- '${:uniq :distinct} ...)]'
55
+ PATTERN = '[!^block (send (send %<type>s :pluck ...) :uniq ...)]'
58
56
 
59
57
  def_node_matcher :conservative_node_match,
60
58
  format(PATTERN, type: 'const')
@@ -63,13 +61,13 @@ module RuboCop
63
61
  format(PATTERN, type: '_')
64
62
 
65
63
  def on_send(node)
66
- method = if style == :conservative
67
- conservative_node_match(node)
68
- else
69
- aggressive_node_match(node)
70
- end
64
+ uniq = if style == :conservative
65
+ conservative_node_match(node)
66
+ else
67
+ aggressive_node_match(node)
68
+ end
71
69
 
72
- return unless method
70
+ return unless uniq
73
71
 
74
72
  add_offense(node.loc.selector) do |corrector|
75
73
  method = node.method_name
@@ -81,10 +79,6 @@ module RuboCop
81
79
 
82
80
  private
83
81
 
84
- def style_parameter_name
85
- 'EnforcedStyle'
86
- end
87
-
88
82
  def dot_method_with_whitespace(method, node)
89
83
  range_between(dot_method_begin_pos(method, node),
90
84
  node.loc.selector.end_pos)
@@ -27,7 +27,7 @@ module RuboCop
27
27
  class UniqueValidationWithoutIndex < Base
28
28
  include ActiveRecordHelper
29
29
 
30
- MSG = 'Uniqueness validation should be with a unique index.'
30
+ MSG = 'Uniqueness validation should have a unique index on the database column.'
31
31
  RESTRICT_ON_SEND = %i[validates].freeze
32
32
 
33
33
  def on_send(node)
@@ -6,6 +6,10 @@ module RuboCop
6
6
  # This cop identifies places where manually constructed SQL
7
7
  # in `where` can be replaced with `where(attribute: value)`.
8
8
  #
9
+ # @safety
10
+ # This cop's autocorrection is unsafe because is may change SQL.
11
+ # See: https://github.com/rubocop/rubocop-rails/issues/403
12
+ #
9
13
  # @example
10
14
  # # bad
11
15
  # User.where('name = ?', 'Gabe')
@@ -11,16 +11,17 @@ module RuboCop
11
11
  # When EnforcedStyle is 'where' then the cop enforces
12
12
  # `where(...).exists?` over `exists?(...)`.
13
13
  #
14
- # This cop is unsafe for auto-correction because the behavior may change on the following case:
14
+ # @safety
15
+ # This cop is unsafe for auto-correction because the behavior may change on the following case:
15
16
  #
16
- # [source,ruby]
17
- # ----
18
- # Author.includes(:articles).where(articles: {id: id}).exists?
19
- # #=> Perform `eager_load` behavior (`LEFT JOIN` query) and get result.
17
+ # [source,ruby]
18
+ # ----
19
+ # Author.includes(:articles).where(articles: {id: id}).exists?
20
+ # #=> Perform `eager_load` behavior (`LEFT JOIN` query) and get result.
20
21
  #
21
- # Author.includes(:articles).exists?(articles: {id: id})
22
- # #=> Perform `preload` behavior and `ActiveRecord::StatementInvalid` error occurs.
23
- # ----
22
+ # Author.includes(:articles).exists?(articles: {id: id})
23
+ # #=> Perform `preload` behavior and `ActiveRecord::StatementInvalid` error occurs.
24
+ # ----
24
25
  #
25
26
  # @example EnforcedStyle: exists (default)
26
27
  # # bad
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'mixin/active_record_helper'
4
+ require_relative 'mixin/active_record_migrations_helper'
4
5
  require_relative 'mixin/enforce_superclass'
5
6
  require_relative 'mixin/index_method'
6
7
  require_relative 'mixin/target_rails_version'
@@ -22,12 +23,14 @@ require_relative 'rails/attribute_default_block_value'
22
23
  require_relative 'rails/belongs_to'
23
24
  require_relative 'rails/blank'
24
25
  require_relative 'rails/bulk_change_table'
26
+ require_relative 'rails/compact_blank'
25
27
  require_relative 'rails/content_tag'
26
28
  require_relative 'rails/create_table_with_timestamps'
27
29
  require_relative 'rails/date'
28
30
  require_relative 'rails/default_scope'
29
31
  require_relative 'rails/delegate'
30
32
  require_relative 'rails/delegate_allow_blank'
33
+ require_relative 'rails/duration_arithmetic'
31
34
  require_relative 'rails/dynamic_find_by'
32
35
  require_relative 'rails/eager_evaluation_log_message'
33
36
  require_relative 'rails/enum_hash'
@@ -71,6 +74,7 @@ require_relative 'rails/rake_environment'
71
74
  require_relative 'rails/read_write_attribute'
72
75
  require_relative 'rails/redundant_allow_nil'
73
76
  require_relative 'rails/redundant_foreign_key'
77
+ require_relative 'rails/redundant_presence_validation_on_belongs_to'
74
78
  require_relative 'rails/redundant_receiver_in_with_options'
75
79
  require_relative 'rails/redundant_travel_back'
76
80
  require_relative 'rails/reflection_class_name'
@@ -82,9 +86,11 @@ require_relative 'rails/request_referer'
82
86
  require_relative 'rails/require_dependency'
83
87
  require_relative 'rails/reversible_migration'
84
88
  require_relative 'rails/reversible_migration_method_definition'
89
+ require_relative 'rails/root_join_chain'
85
90
  require_relative 'rails/safe_navigation'
86
91
  require_relative 'rails/safe_navigation_with_blank'
87
92
  require_relative 'rails/save_bang'
93
+ require_relative 'rails/schema_comment'
88
94
  require_relative 'rails/scope_args'
89
95
  require_relative 'rails/short_i18n'
90
96
  require_relative 'rails/skips_model_validations'
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Rails
5
5
  # This module holds the RuboCop Rails version information.
6
6
  module Version
7
- STRING = '2.12.1'
7
+ STRING = '2.13.0'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.12.1
4
+ version: 2.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-09-10 00:00:00.000000000 Z
13
+ date: 2021-12-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -78,6 +78,7 @@ files:
78
78
  - config/obsoletion.yml
79
79
  - lib/rubocop-rails.rb
80
80
  - lib/rubocop/cop/mixin/active_record_helper.rb
81
+ - lib/rubocop/cop/mixin/active_record_migrations_helper.rb
81
82
  - lib/rubocop/cop/mixin/enforce_superclass.rb
82
83
  - lib/rubocop/cop/mixin/index_method.rb
83
84
  - lib/rubocop/cop/mixin/target_rails_version.rb
@@ -98,12 +99,14 @@ files:
98
99
  - lib/rubocop/cop/rails/belongs_to.rb
99
100
  - lib/rubocop/cop/rails/blank.rb
100
101
  - lib/rubocop/cop/rails/bulk_change_table.rb
102
+ - lib/rubocop/cop/rails/compact_blank.rb
101
103
  - lib/rubocop/cop/rails/content_tag.rb
102
104
  - lib/rubocop/cop/rails/create_table_with_timestamps.rb
103
105
  - lib/rubocop/cop/rails/date.rb
104
106
  - lib/rubocop/cop/rails/default_scope.rb
105
107
  - lib/rubocop/cop/rails/delegate.rb
106
108
  - lib/rubocop/cop/rails/delegate_allow_blank.rb
109
+ - lib/rubocop/cop/rails/duration_arithmetic.rb
107
110
  - lib/rubocop/cop/rails/dynamic_find_by.rb
108
111
  - lib/rubocop/cop/rails/eager_evaluation_log_message.rb
109
112
  - lib/rubocop/cop/rails/enum_hash.rb
@@ -147,6 +150,7 @@ files:
147
150
  - lib/rubocop/cop/rails/read_write_attribute.rb
148
151
  - lib/rubocop/cop/rails/redundant_allow_nil.rb
149
152
  - lib/rubocop/cop/rails/redundant_foreign_key.rb
153
+ - lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb
150
154
  - lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb
151
155
  - lib/rubocop/cop/rails/redundant_travel_back.rb
152
156
  - lib/rubocop/cop/rails/reflection_class_name.rb
@@ -158,9 +162,11 @@ files:
158
162
  - lib/rubocop/cop/rails/require_dependency.rb
159
163
  - lib/rubocop/cop/rails/reversible_migration.rb
160
164
  - lib/rubocop/cop/rails/reversible_migration_method_definition.rb
165
+ - lib/rubocop/cop/rails/root_join_chain.rb
161
166
  - lib/rubocop/cop/rails/safe_navigation.rb
162
167
  - lib/rubocop/cop/rails/safe_navigation_with_blank.rb
163
168
  - lib/rubocop/cop/rails/save_bang.rb
169
+ - lib/rubocop/cop/rails/schema_comment.rb
164
170
  - lib/rubocop/cop/rails/scope_args.rb
165
171
  - lib/rubocop/cop/rails/short_i18n.rb
166
172
  - lib/rubocop/cop/rails/skips_model_validations.rb
@@ -188,8 +194,9 @@ metadata:
188
194
  homepage_uri: https://docs.rubocop.org/rubocop-rails/
189
195
  changelog_uri: https://github.com/rubocop/rubocop-rails/blob/master/CHANGELOG.md
190
196
  source_code_uri: https://github.com/rubocop/rubocop-rails/
191
- documentation_uri: https://docs.rubocop.org/rubocop-rails/2.12/
197
+ documentation_uri: https://docs.rubocop.org/rubocop-rails/2.13/
192
198
  bug_tracker_uri: https://github.com/rubocop/rubocop-rails/issues
199
+ rubygems_mfa_required: 'true'
193
200
  post_install_message:
194
201
  rdoc_options: []
195
202
  require_paths:
@@ -205,7 +212,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
205
212
  - !ruby/object:Gem::Version
206
213
  version: '0'
207
214
  requirements: []
208
- rubygems_version: 3.2.18
215
+ rubygems_version: 3.2.32
209
216
  signing_key:
210
217
  specification_version: 4
211
218
  summary: Automatic Rails code style checking tool.