rubocop-rails 2.9.1 → 2.11.2

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 (44) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +4 -4
  4. data/config/default.yml +111 -5
  5. data/config/obsoletion.yml +7 -0
  6. data/lib/rubocop/cop/mixin/active_record_helper.rb +11 -0
  7. data/lib/rubocop/cop/rails/add_column_index.rb +64 -0
  8. data/lib/rubocop/cop/rails/attribute_default_block_value.rb +1 -1
  9. data/lib/rubocop/cop/rails/belongs_to.rb +1 -1
  10. data/lib/rubocop/cop/rails/blank.rb +4 -0
  11. data/lib/rubocop/cop/rails/bulk_change_table.rb +5 -1
  12. data/lib/rubocop/cop/rails/content_tag.rb +18 -3
  13. data/lib/rubocop/cop/rails/date.rb +17 -6
  14. data/lib/rubocop/cop/rails/dynamic_find_by.rb +4 -2
  15. data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +78 -0
  16. data/lib/rubocop/cop/rails/environment_comparison.rb +1 -2
  17. data/lib/rubocop/cop/rails/environment_variable_access.rb +67 -0
  18. data/lib/rubocop/cop/rails/expanded_date_range.rb +86 -0
  19. data/lib/rubocop/cop/rails/file_path.rb +2 -4
  20. data/lib/rubocop/cop/rails/find_by.rb +31 -12
  21. data/lib/rubocop/cop/rails/find_each.rb +2 -0
  22. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +34 -4
  23. data/lib/rubocop/cop/rails/http_positional_arguments.rb +8 -1
  24. data/lib/rubocop/cop/rails/http_status.rb +12 -3
  25. data/lib/rubocop/cop/rails/i18n_locale_assignment.rb +37 -0
  26. data/lib/rubocop/cop/rails/inverse_of.rb +1 -2
  27. data/lib/rubocop/cop/rails/link_to_blank.rb +5 -1
  28. data/lib/rubocop/cop/rails/reflection_class_name.rb +14 -1
  29. data/lib/rubocop/cop/rails/relative_date_constant.rb +18 -19
  30. data/lib/rubocop/cop/rails/require_dependency.rb +38 -0
  31. data/lib/rubocop/cop/rails/reversible_migration.rb +1 -1
  32. data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +75 -0
  33. data/lib/rubocop/cop/rails/safe_navigation.rb +20 -2
  34. data/lib/rubocop/cop/rails/time_zone.rb +22 -22
  35. data/lib/rubocop/cop/rails/time_zone_assignment.rb +37 -0
  36. data/lib/rubocop/cop/rails/unknown_env.rb +1 -1
  37. data/lib/rubocop/cop/rails/unused_ignored_columns.rb +69 -0
  38. data/lib/rubocop/cop/rails/where_exists.rb +11 -0
  39. data/lib/rubocop/cop/rails/where_not.rb +5 -1
  40. data/lib/rubocop/cop/rails_cops.rb +9 -0
  41. data/lib/rubocop/rails.rb +2 -0
  42. data/lib/rubocop/rails/schema_loader/schema.rb +2 -4
  43. data/lib/rubocop/rails/version.rb +1 -1
  44. metadata +21 -11
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # This cop checks for the use of `Time.zone=` method.
7
+ #
8
+ # The `zone` attribute persists for the rest of the Ruby runtime, potentially causing
9
+ # unexpected behavior at a later time.
10
+ # Using `Time.use_zone` ensures the code passed in the block is the only place Time.zone is affected.
11
+ # It eliminates the possibility of a `zone` sticking around longer than intended.
12
+ #
13
+ # @example
14
+ # # bad
15
+ # Time.zone = 'EST'
16
+ #
17
+ # # good
18
+ # Time.use_zone('EST') do
19
+ # end
20
+ #
21
+ class TimeZoneAssignment < Base
22
+ MSG = 'Use `Time.use_zone` with block instead of `Time.zone=`.'
23
+ RESTRICT_ON_SEND = %i[zone=].freeze
24
+
25
+ def_node_matcher :time_zone_assignement?, <<~PATTERN
26
+ (send (const nil? :Time) :zone= ...)
27
+ PATTERN
28
+
29
+ def on_send(node)
30
+ return unless time_zone_assignement?(node)
31
+
32
+ add_offense(node)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -62,7 +62,7 @@ module RuboCop
62
62
  # DidYouMean::SpellChecker is not available in all versions of Ruby,
63
63
  # and even on versions where it *is* available (>= 2.3), it is not
64
64
  # always required correctly. So we do a feature check first. See:
65
- # https://github.com/rubocop-hq/rubocop/issues/7979
65
+ # https://github.com/rubocop/rubocop/issues/7979
66
66
  similar_names = if defined?(DidYouMean::SpellChecker)
67
67
  spell_checker = DidYouMean::SpellChecker.new(dictionary: environments)
68
68
  spell_checker.correct(name)
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # This cop suggests you remove a column that does not exist in the schema from `ignored_columns`.
7
+ # `ignored_columns` is necessary to drop a column from RDBMS, but you don't need it after the migration
8
+ # to drop the column. You avoid forgetting to remove `ignored_columns` by this cop.
9
+ #
10
+ # @example
11
+ # # bad
12
+ # class User < ApplicationRecord
13
+ # self.ignored_columns = [:already_removed_column]
14
+ # end
15
+ #
16
+ # # good
17
+ # class User < ApplicationRecord
18
+ # self.ignored_columns = [:still_existing_column]
19
+ # end
20
+ #
21
+ class UnusedIgnoredColumns < Base
22
+ include ActiveRecordHelper
23
+
24
+ MSG = 'Remove `%<column_name>s` from `ignored_columns` because the column does not exist.'
25
+ RESTRICT_ON_SEND = %i[ignored_columns=].freeze
26
+
27
+ def_node_matcher :ignored_columns, <<~PATTERN
28
+ (send self :ignored_columns= $array)
29
+ PATTERN
30
+
31
+ def_node_matcher :column_name, <<~PATTERN
32
+ ({str sym} $_)
33
+ PATTERN
34
+
35
+ def on_send(node)
36
+ return unless (columns = ignored_columns(node))
37
+ return unless schema
38
+
39
+ table = table(node)
40
+ return unless table
41
+
42
+ columns.children.each do |column_node|
43
+ check_column_existence(column_node, table)
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def check_column_existence(column_node, table)
50
+ column_name = column_name(column_node)
51
+ return unless column_name
52
+ return if table.with_column?(name: column_name.to_s)
53
+
54
+ message = format(MSG, column_name: column_name)
55
+ add_offense(column_node, message: message)
56
+ end
57
+
58
+ def class_node(node)
59
+ node.each_ancestor.find(&:class_type?)
60
+ end
61
+
62
+ def table(node)
63
+ klass = class_node(node)
64
+ schema.table_by(name: table_name(klass))
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -11,6 +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:
15
+ #
16
+ # [source,ruby]
17
+ # ----
18
+ # Author.includes(:articles).where(articles: {id: id}).exists?
19
+ # #=> Perform `eager_load` behavior (`LEFT JOIN` query) and get result.
20
+ #
21
+ # Author.includes(:articles).exists?(articles: {id: id})
22
+ # #=> Perform `preload` behavior and `ActiveRecord::StatementInvalid` error occurs.
23
+ # ----
24
+ #
14
25
  # @example EnforcedStyle: exists (default)
15
26
  # # bad
16
27
  # User.where(name: 'john').exists?
@@ -15,11 +15,13 @@ module RuboCop
15
15
  # User.where('name IS NOT NULL')
16
16
  # User.where('name NOT IN (?)', ['john', 'jane'])
17
17
  # User.where('name NOT IN (:names)', names: ['john', 'jane'])
18
+ # User.where('users.name != :name', name: 'Gabe')
18
19
  #
19
20
  # # good
20
21
  # User.where.not(name: 'Gabe')
21
22
  # User.where.not(name: nil)
22
23
  # User.where.not(name: ['john', 'jane'])
24
+ # User.where.not(users: { name: 'Gabe' })
23
25
  #
24
26
  class WhereNot < Base
25
27
  include RangeHelp
@@ -86,7 +88,9 @@ module RuboCop
86
88
 
87
89
  def build_good_method(column, value)
88
90
  if column.include?('.')
89
- "where.not('#{column}' => #{value})"
91
+ table, column = column.split('.')
92
+
93
+ "where.not(#{table}: { #{column}: #{value} })"
90
94
  else
91
95
  "where.not(#{column}: #{value})"
92
96
  end
@@ -10,6 +10,7 @@ require_relative 'rails/active_record_aliases'
10
10
  require_relative 'rails/active_record_callbacks_order'
11
11
  require_relative 'rails/active_record_override'
12
12
  require_relative 'rails/active_support_aliases'
13
+ require_relative 'rails/add_column_index'
13
14
  require_relative 'rails/after_commit_override'
14
15
  require_relative 'rails/application_controller'
15
16
  require_relative 'rails/application_job'
@@ -28,10 +29,13 @@ require_relative 'rails/default_scope'
28
29
  require_relative 'rails/delegate'
29
30
  require_relative 'rails/delegate_allow_blank'
30
31
  require_relative 'rails/dynamic_find_by'
32
+ require_relative 'rails/eager_evaluation_log_message'
31
33
  require_relative 'rails/enum_hash'
32
34
  require_relative 'rails/enum_uniqueness'
33
35
  require_relative 'rails/environment_comparison'
36
+ require_relative 'rails/environment_variable_access'
34
37
  require_relative 'rails/exit'
38
+ require_relative 'rails/expanded_date_range'
35
39
  require_relative 'rails/file_path'
36
40
  require_relative 'rails/find_by'
37
41
  require_relative 'rails/find_by_id'
@@ -41,6 +45,7 @@ require_relative 'rails/has_many_or_has_one_dependent'
41
45
  require_relative 'rails/helper_instance_variable'
42
46
  require_relative 'rails/http_positional_arguments'
43
47
  require_relative 'rails/http_status'
48
+ require_relative 'rails/i18n_locale_assignment'
44
49
  require_relative 'rails/ignored_skip_action_filter_option'
45
50
  require_relative 'rails/index_by'
46
51
  require_relative 'rails/index_with'
@@ -73,7 +78,9 @@ require_relative 'rails/relative_date_constant'
73
78
  require_relative 'rails/render_inline'
74
79
  require_relative 'rails/render_plain_text'
75
80
  require_relative 'rails/request_referer'
81
+ require_relative 'rails/require_dependency'
76
82
  require_relative 'rails/reversible_migration'
83
+ require_relative 'rails/reversible_migration_method_definition'
77
84
  require_relative 'rails/safe_navigation'
78
85
  require_relative 'rails/safe_navigation_with_blank'
79
86
  require_relative 'rails/save_bang'
@@ -82,9 +89,11 @@ require_relative 'rails/short_i18n'
82
89
  require_relative 'rails/skips_model_validations'
83
90
  require_relative 'rails/squished_sql_heredocs'
84
91
  require_relative 'rails/time_zone'
92
+ require_relative 'rails/time_zone_assignment'
85
93
  require_relative 'rails/uniq_before_pluck'
86
94
  require_relative 'rails/unique_validation_without_index'
87
95
  require_relative 'rails/unknown_env'
96
+ require_relative 'rails/unused_ignored_columns'
88
97
  require_relative 'rails/validation'
89
98
  require_relative 'rails/where_equals'
90
99
  require_relative 'rails/where_exists'
data/lib/rubocop/rails.rb CHANGED
@@ -8,5 +8,7 @@ module RuboCop
8
8
  CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
9
9
 
10
10
  private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
11
+
12
+ ::RuboCop::ConfigObsoletion.files << PROJECT_ROOT.join('config', 'obsoletion.yml')
11
13
  end
12
14
  end
@@ -114,7 +114,7 @@ module RuboCop
114
114
  attr_reader :name, :type, :not_null
115
115
 
116
116
  def initialize(node)
117
- @name = node.first_argument.value
117
+ @name = node.first_argument.str_content
118
118
  @type = node.method_name
119
119
  @not_null = nil
120
120
 
@@ -128,9 +128,7 @@ module RuboCop
128
128
  return unless pairs.hash_type?
129
129
 
130
130
  pairs.each_pair do |k, v|
131
- if k.value == :null
132
- @not_null = v.true_type? ? false : true
133
- end
131
+ @not_null = !v.true_type? if k.value == :null
134
132
  end
135
133
  end
136
134
  end
@@ -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.9.1'
7
+ STRING = '2.11.2'
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.9.1
4
+ version: 2.11.2
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: 2020-12-16 00:00:00.000000000 Z
13
+ date: 2021-07-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -46,7 +46,7 @@ dependencies:
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: 0.90.0
49
+ version: 1.7.0
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
52
  version: '2.0'
@@ -56,7 +56,7 @@ dependencies:
56
56
  requirements:
57
57
  - - ">="
58
58
  - !ruby/object:Gem::Version
59
- version: 0.90.0
59
+ version: 1.7.0
60
60
  - - "<"
61
61
  - !ruby/object:Gem::Version
62
62
  version: '2.0'
@@ -74,6 +74,7 @@ files:
74
74
  - README.md
75
75
  - bin/setup
76
76
  - config/default.yml
77
+ - config/obsoletion.yml
77
78
  - lib/rubocop-rails.rb
78
79
  - lib/rubocop/cop/mixin/active_record_helper.rb
79
80
  - lib/rubocop/cop/mixin/enforce_superclass.rb
@@ -84,6 +85,7 @@ files:
84
85
  - lib/rubocop/cop/rails/active_record_callbacks_order.rb
85
86
  - lib/rubocop/cop/rails/active_record_override.rb
86
87
  - lib/rubocop/cop/rails/active_support_aliases.rb
88
+ - lib/rubocop/cop/rails/add_column_index.rb
87
89
  - lib/rubocop/cop/rails/after_commit_override.rb
88
90
  - lib/rubocop/cop/rails/application_controller.rb
89
91
  - lib/rubocop/cop/rails/application_job.rb
@@ -102,10 +104,13 @@ files:
102
104
  - lib/rubocop/cop/rails/delegate.rb
103
105
  - lib/rubocop/cop/rails/delegate_allow_blank.rb
104
106
  - lib/rubocop/cop/rails/dynamic_find_by.rb
107
+ - lib/rubocop/cop/rails/eager_evaluation_log_message.rb
105
108
  - lib/rubocop/cop/rails/enum_hash.rb
106
109
  - lib/rubocop/cop/rails/enum_uniqueness.rb
107
110
  - lib/rubocop/cop/rails/environment_comparison.rb
111
+ - lib/rubocop/cop/rails/environment_variable_access.rb
108
112
  - lib/rubocop/cop/rails/exit.rb
113
+ - lib/rubocop/cop/rails/expanded_date_range.rb
109
114
  - lib/rubocop/cop/rails/file_path.rb
110
115
  - lib/rubocop/cop/rails/find_by.rb
111
116
  - lib/rubocop/cop/rails/find_by_id.rb
@@ -115,6 +120,7 @@ files:
115
120
  - lib/rubocop/cop/rails/helper_instance_variable.rb
116
121
  - lib/rubocop/cop/rails/http_positional_arguments.rb
117
122
  - lib/rubocop/cop/rails/http_status.rb
123
+ - lib/rubocop/cop/rails/i18n_locale_assignment.rb
118
124
  - lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb
119
125
  - lib/rubocop/cop/rails/index_by.rb
120
126
  - lib/rubocop/cop/rails/index_with.rb
@@ -147,7 +153,9 @@ files:
147
153
  - lib/rubocop/cop/rails/render_inline.rb
148
154
  - lib/rubocop/cop/rails/render_plain_text.rb
149
155
  - lib/rubocop/cop/rails/request_referer.rb
156
+ - lib/rubocop/cop/rails/require_dependency.rb
150
157
  - lib/rubocop/cop/rails/reversible_migration.rb
158
+ - lib/rubocop/cop/rails/reversible_migration_method_definition.rb
151
159
  - lib/rubocop/cop/rails/safe_navigation.rb
152
160
  - lib/rubocop/cop/rails/safe_navigation_with_blank.rb
153
161
  - lib/rubocop/cop/rails/save_bang.rb
@@ -156,9 +164,11 @@ files:
156
164
  - lib/rubocop/cop/rails/skips_model_validations.rb
157
165
  - lib/rubocop/cop/rails/squished_sql_heredocs.rb
158
166
  - lib/rubocop/cop/rails/time_zone.rb
167
+ - lib/rubocop/cop/rails/time_zone_assignment.rb
159
168
  - lib/rubocop/cop/rails/uniq_before_pluck.rb
160
169
  - lib/rubocop/cop/rails/unique_validation_without_index.rb
161
170
  - lib/rubocop/cop/rails/unknown_env.rb
171
+ - lib/rubocop/cop/rails/unused_ignored_columns.rb
162
172
  - lib/rubocop/cop/rails/validation.rb
163
173
  - lib/rubocop/cop/rails/where_equals.rb
164
174
  - lib/rubocop/cop/rails/where_exists.rb
@@ -169,15 +179,15 @@ files:
169
179
  - lib/rubocop/rails/schema_loader.rb
170
180
  - lib/rubocop/rails/schema_loader/schema.rb
171
181
  - lib/rubocop/rails/version.rb
172
- homepage: https://github.com/rubocop-hq/rubocop-rails
182
+ homepage: https://github.com/rubocop/rubocop-rails
173
183
  licenses:
174
184
  - MIT
175
185
  metadata:
176
186
  homepage_uri: https://docs.rubocop.org/rubocop-rails/
177
- changelog_uri: https://github.com/rubocop-hq/rubocop-rails/blob/master/CHANGELOG.md
178
- source_code_uri: https://github.com/rubocop-hq/rubocop-rails/
179
- documentation_uri: https://docs.rubocop.org/rubocop-rails/2.9/
180
- bug_tracker_uri: https://github.com/rubocop-hq/rubocop-rails/issues
187
+ changelog_uri: https://github.com/rubocop/rubocop-rails/blob/master/CHANGELOG.md
188
+ source_code_uri: https://github.com/rubocop/rubocop-rails/
189
+ documentation_uri: https://docs.rubocop.org/rubocop-rails/2.11/
190
+ bug_tracker_uri: https://github.com/rubocop/rubocop-rails/issues
181
191
  post_install_message:
182
192
  rdoc_options: []
183
193
  require_paths:
@@ -186,14 +196,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
186
196
  requirements:
187
197
  - - ">="
188
198
  - !ruby/object:Gem::Version
189
- version: 2.4.0
199
+ version: 2.5.0
190
200
  required_rubygems_version: !ruby/object:Gem::Requirement
191
201
  requirements:
192
202
  - - ">="
193
203
  - !ruby/object:Gem::Version
194
204
  version: '0'
195
205
  requirements: []
196
- rubygems_version: 3.2.1
206
+ rubygems_version: 3.2.18
197
207
  signing_key:
198
208
  specification_version: 4
199
209
  summary: Automatic Rails code style checking tool.