sevencop 0.22.0 → 0.24.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/Gemfile.lock +1 -1
  4. data/README.md +5 -16
  5. data/config/default.yml +16 -143
  6. data/lib/rubocop/cop/sevencop/rails_action_name.rb +61 -0
  7. data/lib/rubocop/cop/sevencop/rspec_describe_http_endpoint.rb +23 -10
  8. data/lib/sevencop/cop_concerns.rb +0 -3
  9. data/lib/sevencop/version.rb +1 -1
  10. data/lib/sevencop.rb +1 -16
  11. metadata +3 -21
  12. data/lib/rubocop/cop/sevencop/rails_migration_add_check_constraint.rb +0 -111
  13. data/lib/rubocop/cop/sevencop/rails_migration_add_column_with_default_value.rb +0 -229
  14. data/lib/rubocop/cop/sevencop/rails_migration_add_foreign_key.rb +0 -166
  15. data/lib/rubocop/cop/sevencop/rails_migration_add_index_concurrently.rb +0 -164
  16. data/lib/rubocop/cop/sevencop/rails_migration_batch_in_batches.rb +0 -95
  17. data/lib/rubocop/cop/sevencop/rails_migration_batch_in_transaction.rb +0 -83
  18. data/lib/rubocop/cop/sevencop/rails_migration_batch_with_throttling.rb +0 -108
  19. data/lib/rubocop/cop/sevencop/rails_migration_change_column.rb +0 -113
  20. data/lib/rubocop/cop/sevencop/rails_migration_change_column_null.rb +0 -128
  21. data/lib/rubocop/cop/sevencop/rails_migration_create_table_force.rb +0 -89
  22. data/lib/rubocop/cop/sevencop/rails_migration_jsonb.rb +0 -131
  23. data/lib/rubocop/cop/sevencop/rails_migration_remove_column.rb +0 -258
  24. data/lib/rubocop/cop/sevencop/rails_migration_rename_column.rb +0 -81
  25. data/lib/rubocop/cop/sevencop/rails_migration_rename_table.rb +0 -79
  26. data/lib/rubocop/cop/sevencop/rails_migration_reserved_word_mysql.rb +0 -232
  27. data/lib/rubocop/cop/sevencop/rails_migration_unique_index_columns_count.rb +0 -92
  28. data/lib/sevencop/cop_concerns/batch_processing.rb +0 -32
  29. data/lib/sevencop/cop_concerns/column_type_method.rb +0 -26
  30. data/lib/sevencop/cop_concerns/disable_ddl_transaction.rb +0 -49
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e2aff4e0e776ef3fa5b3f71ece7b7c83013caadaf829fc86827ab246f5f80382
4
- data.tar.gz: 1889785ce282e4d530c7eef4a54afaa948181b1557e788425e9b30e831935b77
3
+ metadata.gz: 47139f7d9fb3b1e73416958e9f2f87ccafa4145864e046a1539ffd33018bd603
4
+ data.tar.gz: f9c759a3adbbf7e25b327755c8d5fc0a00d5f6438f79b4df3e33d138ca296fb7
5
5
  SHA512:
6
- metadata.gz: 386a275378516b205cbd1dc4d50226776d106dc98eb8c534973ff7379c39b59f0e49ae4985058f79254d2256a09ac459b6357e1c5d0f03a8e2d780ec8b1fc990
7
- data.tar.gz: 91adea811d280655577fd88cdb4b861b3a9e04f4c89d9542a4ce38281f325cfe0be590cb45ce8937375b4d13e67970518e6381531f46087689e58f3b8d0c5156
6
+ metadata.gz: e43613b26de17bca55c264a4892a3c20684fcc5c6e285c2407249c6369218d82650bdfb0e8963fd43c19c25a5fe24d885c4074894c8bc28fc105a0ba2dbfbb6e
7
+ data.tar.gz: 4697e89105ef1bcd388a72af2505d3b1d19ea5fa0f1c3867d3fca0313d0c470d38b92eb89fa477e6782fbd83dff3e32300cfd812b67dd06b472fd6747480b10a
data/.rubocop.yml CHANGED
@@ -3,7 +3,7 @@ require:
3
3
  - rubocop-performance
4
4
  - rubocop-rake
5
5
  - rubocop-rspec
6
- - sevencop
6
+ - ./lib/sevencop
7
7
 
8
8
  AllCops:
9
9
  NewCops: enable
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sevencop (0.22.0)
4
+ sevencop (0.24.2)
5
5
  activesupport
6
6
  rubocop
7
7
 
data/README.md CHANGED
@@ -36,27 +36,16 @@ Note that all cops are `Enabled: false` by default.
36
36
  - [Sevencop/MethodDefinitionInIncluded](lib/rubocop/cop/sevencop/method_definition_in_included.rb)
37
37
  - [Sevencop/MethodDefinitionKeywordArgumentOrdered](lib/rubocop/cop/sevencop/method_definition_keyword_argument_ordered.rb)
38
38
  - [Sevencop/MethodDefinitionOrdered](lib/rubocop/cop/sevencop/method_definition_ordered.rb)
39
+ - [Sevencop/RailsActionName](lib/rubocop/cop/sevencop/rails_action_name.rb)
39
40
  - [Sevencop/RailsBelongsToOptional](lib/rubocop/cop/sevencop/rails_belongs_to_optional.rb)
40
41
  - [Sevencop/RailsInferredSpecType](lib/rubocop/cop/sevencop/rails_inferred_spec_type.rb)
41
- - [Sevencop/RailsMigrationAddCheckConstraint](lib/rubocop/cop/sevencop/rails_migration_add_check_constraint.rb)
42
- - [Sevencop/RailsMigrationAddColumnWithDefaultValue](lib/rubocop/cop/sevencop/rails_migration_add_column_with_default_value.rb)
43
- - [Sevencop/RailsMigrationAddForeignKey](lib/rubocop/cop/sevencop/rails_migration_add_foreign_key.rb)
44
- - [Sevencop/RailsMigrationAddIndexConcurrently](lib/rubocop/cop/sevencop/rails_migration_add_index_concurrently.rb)
45
- - [Sevencop/RailsMigrationBatchInBatches](lib/rubocop/cop/sevencop/rails_migration_batch_in_batches.rb)
46
- - [Sevencop/RailsMigrationBatchInTransaction](lib/rubocop/cop/sevencop/rails_migration_batch_in_transaction.rb)
47
- - [Sevencop/RailsMigrationBatchWithThrottling](lib/rubocop/cop/sevencop/rails_migration_batch_with_throttling.rb)
48
- - [Sevencop/RailsMigrationChangeColumn](lib/rubocop/cop/sevencop/rails_migration_change_column.rb)
49
- - [Sevencop/RailsMigrationChangeColumnNull](lib/rubocop/cop/sevencop/rails_migration_change_column_null.rb)
50
- - [Sevencop/RailsMigrationCreateTableForce](lib/rubocop/cop/sevencop/rails_migration_create_table_force.rb)
51
- - [Sevencop/RailsMigrationJsonb](lib/rubocop/cop/sevencop/rails_migration_jsonb.rb)
52
- - [Sevencop/RailsMigrationRemoveColumn](lib/rubocop/cop/sevencop/rails_migration_remove_column.rb)
53
- - [Sevencop/RailsMigrationRenameColumn](lib/rubocop/cop/sevencop/rails_migration_rename_column.rb)
54
- - [Sevencop/RailsMigrationRenameTable](lib/rubocop/cop/sevencop/rails_migration_rename_table.rb)
55
- - [Sevencop/RailsMigrationReservedWordMysql](lib/rubocop/cop/sevencop/rails_migration_reserved_word_mysql.rb)
56
- - [Sevencop/RailsMigrationUniqueIndexColumnsCount](lib/rubocop/cop/sevencop/rails_migration_unique_index_columns_count.rb)
57
42
  - [Sevencop/RailsOrderField](lib/rubocop/cop/sevencop/rails_order_field.rb)
58
43
  - [Sevencop/RailsUniquenessValidatorExplicitCaseSensitivity](lib/rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity.rb)
59
44
  - [Sevencop/RailsWhereNot](lib/rubocop/cop/sevencop/rails_where_not.rb)
60
45
  - [Sevencop/RequireOrdered](lib/rubocop/cop/sevencop/require_ordered.rb)
61
46
  - [Sevencop/RSpecDescribeHttpEndpoint](lib/rubocop/cop/sevencop/rspec_describe_http_endpoint.rb)
62
47
  - [Sevencop/RSpecExamplesInSameGroup](lib/rubocop/cop/sevencop/rspec_examples_in_same_group.rb)
48
+
49
+ ## Notes
50
+
51
+ All migration related cops are moved to [r7kamura/rubocop-migration](https://github.com/r7kamura/rubocop-migration).
data/config/default.yml CHANGED
@@ -68,6 +68,22 @@ Sevencop/MethodDefinitionKeywordArgumentOrdered:
68
68
  Safe: false
69
69
  VersionAdded: '0.13'
70
70
 
71
+ Sevencop/RailsActionName:
72
+ Description: |
73
+ Use only specific action names.
74
+ Enabled: false
75
+ VersionAdded: '0.24'
76
+ ActionNames:
77
+ - create
78
+ - destroy
79
+ - edit
80
+ - index
81
+ - new
82
+ - show
83
+ - update
84
+ Include:
85
+ - app/controllers/**/*.rb
86
+
71
87
  Sevencop/RailsBelongsToOptional:
72
88
  Description: |
73
89
  Force `belongs_to` with `optional: true` option.
@@ -82,149 +98,6 @@ Sevencop/RailsInferredSpecType:
82
98
  Safe: false
83
99
  VersionAdded: '0.9'
84
100
 
85
- Sevencop/RailsMigrationAddCheckConstraint:
86
- Description: |
87
- Activate a check constraint in a separate migration in PostgreSQL.
88
- Enabled: false
89
- Safe: false
90
- VersionAdded: '0.22'
91
- Include:
92
- - db/migrate/**/*.rb
93
-
94
- Sevencop/RailsMigrationAddColumnWithDefaultValue:
95
- Description: |
96
- Add the column without a default value then change the default.
97
- Enabled: false
98
- Safe: false
99
- VersionAdded: '0.22'
100
- Include:
101
- - db/migrate/**/*.rb
102
-
103
- Sevencop/RailsMigrationAddForeignKey:
104
- Description: |
105
- Activate a foreign key validation in a separate migration in PostgreSQL.
106
- Enabled: false
107
- Safe: false
108
- VersionAdded: '0.22'
109
- Include:
110
- - db/migrate/**/*.rb
111
-
112
- Sevencop/RailsMigrationAddIndexConcurrently:
113
- Description: |
114
- Use `algorithm: :concurrently` on adding indexes to existing tables in PostgreSQL.
115
- Enabled: false
116
- Safe: false
117
- VersionAdded: '0.22'
118
- Include:
119
- - db/migrate/**/*.rb
120
-
121
- Sevencop/RailsMigrationBatchInBatches:
122
- Description: |
123
- Use `in_batches` in batch processing.
124
- Enabled: false
125
- Safe: false
126
- VersionAdded: '0.22'
127
- Include:
128
- - db/migrate/**/*.rb
129
-
130
- Sevencop/RailsMigrationBatchInTransaction:
131
- Description: |
132
- Disable transaction in batch processing.
133
- Enabled: false
134
- Safe: false
135
- VersionAdded: '0.22'
136
- Include:
137
- - db/migrate/**/*.rb
138
-
139
- Sevencop/RailsMigrationBatchWithThrottling:
140
- Description: |
141
- Use throttling in batch processing.
142
- Enabled: false
143
- Safe: false
144
- VersionAdded: '0.22'
145
- Include:
146
- - db/migrate/**/*.rb
147
-
148
- Sevencop/RailsMigrationCreateTableForce:
149
- Description: |
150
- Create tables without `force: true` option.
151
- Enabled: false
152
- VersionAdded: '0.22'
153
- Include:
154
- - db/migrate/**/*.rb
155
-
156
- Sevencop/RailsMigrationJsonb:
157
- Description: |
158
- Prefer `jsonb` to `json`.
159
- Enabled: false
160
- Safe: false
161
- VersionAdded: '0.22'
162
- Include:
163
- - db/migrate/**/*.rb
164
-
165
- Sevencop/RailsMigrationChangeColumn:
166
- Description: |
167
- Avoid changing column type that is in use.
168
- Enabled: false
169
- Safe: false
170
- VersionAdded: '0.22'
171
- Include:
172
- - db/migrate/**/*.rb
173
-
174
- Sevencop/RailsMigrationChangeColumnNull:
175
- Description: |
176
- Avoid simply setting `NOT NULL` constraint on an existing column in PostgreSQL.
177
- Enabled: false
178
- Safe: false
179
- VersionAdded: '0.22'
180
- Include:
181
- - db/migrate/**/*.rb
182
-
183
- Sevencop/RailsMigrationRemoveColumn:
184
- Description: |
185
- Add to `ignored_columns` and then remove the column.
186
- Enabled: false
187
- Safe: false
188
- VersionAdded: '0.22'
189
- Include:
190
- - db/migrate/**/*.rb
191
-
192
- Sevencop/RailsMigrationRenameColumn:
193
- Description: |
194
- Avoid renaming columns that are in use.
195
- Enabled: false
196
- Safe: false
197
- VersionAdded: '0.22'
198
- Include:
199
- - db/migrate/**/*.rb
200
-
201
- Sevencop/RailsMigrationRenameTable:
202
- Description: |
203
- Avoid removing tables that are in use.
204
- Enabled: false
205
- Safe: false
206
- VersionAdded: '0.22'
207
- Include:
208
- - db/migrate/**/*.rb
209
-
210
- Sevencop/RailsMigrationReservedWordMysql:
211
- Description: |
212
- Avoid using MySQL reserved words as identifiers.
213
- Enabled: false
214
- Safe: false
215
- VersionAdded: '0.20'
216
- Include:
217
- - db/migrate/**/*.rb
218
-
219
- Sevencop/RailsMigrationUniqueIndexColumnsCount:
220
- Description: |
221
- Keep unique index columns count less than a specified number.
222
- Enabled: false
223
- VersionAdded: '0.22'
224
- MaxColumnsCount: 3
225
- Include:
226
- - db/migrate/**/*.rb
227
-
228
101
  Sevencop/RailsOrderField:
229
102
  Description: |
230
103
  Wrap safe SQL String by `Arel.sql`.
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Sevencop
6
+ # Use only specific action names.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # class UsersController < ApplicationController
11
+ # def articles
12
+ # end
13
+ # end
14
+ #
15
+ # # good
16
+ # class UserArticlesController < ApplicationController
17
+ # def index
18
+ # end
19
+ # end
20
+ class RailsActionName < Base
21
+ include VisibilityHelp
22
+
23
+ MSG = 'Use only specific action names.'
24
+
25
+ # @param node [RuboCop::AST::DefNode]
26
+ # @return [void]
27
+ def on_def(node)
28
+ return unless bad?(node)
29
+
30
+ add_offense(node.location.name)
31
+ end
32
+
33
+ private
34
+
35
+ # @param node [RuboCop::AST::DefNode]
36
+ # @return [Boolean]
37
+ def action?(node)
38
+ node_visibility(node) == :public
39
+ end
40
+
41
+ # @param node [RuboCop::AST::DefNode]
42
+ # @return [Boolean]
43
+ def bad?(node)
44
+ action?(node) &&
45
+ !configured_action_name?(node)
46
+ end
47
+
48
+ # @param node [RuboCop::AST::DefNode]
49
+ # @return [Boolean]
50
+ def configured_action_name?(node)
51
+ configured_action_names.include?(node.method_name.to_s)
52
+ end
53
+
54
+ # @return [Array<String>]
55
+ def configured_action_names
56
+ cop_config['ActionNames']
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -33,30 +33,43 @@ module RuboCop
33
33
  # @param node [RuboCop::AST::SendNode]
34
34
  # @return [void]
35
35
  def on_send(node)
36
- return unless top_level_describe?(node)
37
- return unless node.first_argument
38
- return if http_endpoint_identifier?(node.first_argument)
36
+ return unless bad?(node)
39
37
 
40
38
  add_offense(node.first_argument)
41
39
  end
42
40
 
43
41
  private
44
42
 
45
- # @!method top_level_describe?(node)
43
+ # @!method describing_http_endpoint_identifier?(node)
46
44
  # @param node [RuboCop::AST::SendNode]
47
45
  # @return [Boolean]
48
- def_node_matcher :top_level_describe?, <<~PATTERN
46
+ def_node_matcher :describing_http_endpoint_identifier?, <<~PATTERN
49
47
  (send
50
- (const nil? :RSpec)
48
+ _
51
49
  :describe
50
+ (str DESCRIPTION_PATTERN)
52
51
  ...
53
52
  )
54
53
  PATTERN
55
54
 
56
- # @param node [RuboCop::AST::Node, nil]
57
- # @return [Boolean]
58
- def http_endpoint_identifier?(node)
59
- node&.str_type? && node.value.match?(DESCRIPTION_PATTERN)
55
+ # @!method describing_at_top_level?(node)
56
+ # @param node [RuboCop::AST::SendNode]
57
+ # @return [Boolean]
58
+ def_node_matcher :describing_at_top_level?, <<~PATTERN
59
+ (send
60
+ (const
61
+ {nil? cbase}
62
+ :RSpec
63
+ )
64
+ :describe
65
+ ...
66
+ )
67
+ PATTERN
68
+
69
+ # @param node [RuboCop::AST::SendNode]
70
+ def bad?(node)
71
+ describing_at_top_level?(node) &&
72
+ !describing_http_endpoint_identifier?(node)
60
73
  end
61
74
  end
62
75
  end
@@ -2,9 +2,6 @@
2
2
 
3
3
  module Sevencop
4
4
  module CopConcerns
5
- autoload :BatchProcessing, 'sevencop/cop_concerns/batch_processing'
6
- autoload :ColumnTypeMethod, 'sevencop/cop_concerns/column_type_method'
7
- autoload :DisableDdlTransaction, 'sevencop/cop_concerns/disable_ddl_transaction'
8
5
  autoload :Ordered, 'sevencop/cop_concerns/ordered'
9
6
  end
10
7
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sevencop
4
- VERSION = '0.22.0'
4
+ VERSION = '0.24.2'
5
5
  end
data/lib/sevencop.rb CHANGED
@@ -12,24 +12,9 @@ require_relative 'rubocop/cop/sevencop/method_definition_arguments_multiline'
12
12
  require_relative 'rubocop/cop/sevencop/method_definition_in_included'
13
13
  require_relative 'rubocop/cop/sevencop/method_definition_keyword_argument_ordered'
14
14
  require_relative 'rubocop/cop/sevencop/method_definition_ordered'
15
+ require_relative 'rubocop/cop/sevencop/rails_action_name'
15
16
  require_relative 'rubocop/cop/sevencop/rails_belongs_to_optional'
16
17
  require_relative 'rubocop/cop/sevencop/rails_inferred_spec_type'
17
- require_relative 'rubocop/cop/sevencop/rails_migration_add_check_constraint'
18
- require_relative 'rubocop/cop/sevencop/rails_migration_add_column_with_default_value'
19
- require_relative 'rubocop/cop/sevencop/rails_migration_add_foreign_key'
20
- require_relative 'rubocop/cop/sevencop/rails_migration_add_index_concurrently'
21
- require_relative 'rubocop/cop/sevencop/rails_migration_batch_in_batches'
22
- require_relative 'rubocop/cop/sevencop/rails_migration_batch_in_transaction'
23
- require_relative 'rubocop/cop/sevencop/rails_migration_batch_with_throttling'
24
- require_relative 'rubocop/cop/sevencop/rails_migration_change_column'
25
- require_relative 'rubocop/cop/sevencop/rails_migration_change_column_null'
26
- require_relative 'rubocop/cop/sevencop/rails_migration_create_table_force'
27
- require_relative 'rubocop/cop/sevencop/rails_migration_jsonb'
28
- require_relative 'rubocop/cop/sevencop/rails_migration_remove_column'
29
- require_relative 'rubocop/cop/sevencop/rails_migration_rename_column'
30
- require_relative 'rubocop/cop/sevencop/rails_migration_rename_table'
31
- require_relative 'rubocop/cop/sevencop/rails_migration_reserved_word_mysql'
32
- require_relative 'rubocop/cop/sevencop/rails_migration_unique_index_columns_count'
33
18
  require_relative 'rubocop/cop/sevencop/rails_order_field'
34
19
  require_relative 'rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity'
35
20
  require_relative 'rubocop/cop/sevencop/rails_where_not'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sevencop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.22.0
4
+ version: 0.24.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-16 00:00:00.000000000 Z
11
+ date: 2022-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -64,24 +64,9 @@ files:
64
64
  - lib/rubocop/cop/sevencop/method_definition_in_included.rb
65
65
  - lib/rubocop/cop/sevencop/method_definition_keyword_argument_ordered.rb
66
66
  - lib/rubocop/cop/sevencop/method_definition_ordered.rb
67
+ - lib/rubocop/cop/sevencop/rails_action_name.rb
67
68
  - lib/rubocop/cop/sevencop/rails_belongs_to_optional.rb
68
69
  - lib/rubocop/cop/sevencop/rails_inferred_spec_type.rb
69
- - lib/rubocop/cop/sevencop/rails_migration_add_check_constraint.rb
70
- - lib/rubocop/cop/sevencop/rails_migration_add_column_with_default_value.rb
71
- - lib/rubocop/cop/sevencop/rails_migration_add_foreign_key.rb
72
- - lib/rubocop/cop/sevencop/rails_migration_add_index_concurrently.rb
73
- - lib/rubocop/cop/sevencop/rails_migration_batch_in_batches.rb
74
- - lib/rubocop/cop/sevencop/rails_migration_batch_in_transaction.rb
75
- - lib/rubocop/cop/sevencop/rails_migration_batch_with_throttling.rb
76
- - lib/rubocop/cop/sevencop/rails_migration_change_column.rb
77
- - lib/rubocop/cop/sevencop/rails_migration_change_column_null.rb
78
- - lib/rubocop/cop/sevencop/rails_migration_create_table_force.rb
79
- - lib/rubocop/cop/sevencop/rails_migration_jsonb.rb
80
- - lib/rubocop/cop/sevencop/rails_migration_remove_column.rb
81
- - lib/rubocop/cop/sevencop/rails_migration_rename_column.rb
82
- - lib/rubocop/cop/sevencop/rails_migration_rename_table.rb
83
- - lib/rubocop/cop/sevencop/rails_migration_reserved_word_mysql.rb
84
- - lib/rubocop/cop/sevencop/rails_migration_unique_index_columns_count.rb
85
70
  - lib/rubocop/cop/sevencop/rails_order_field.rb
86
71
  - lib/rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity.rb
87
72
  - lib/rubocop/cop/sevencop/rails_where_not.rb
@@ -91,9 +76,6 @@ files:
91
76
  - lib/sevencop.rb
92
77
  - lib/sevencop/config_loader.rb
93
78
  - lib/sevencop/cop_concerns.rb
94
- - lib/sevencop/cop_concerns/batch_processing.rb
95
- - lib/sevencop/cop_concerns/column_type_method.rb
96
- - lib/sevencop/cop_concerns/disable_ddl_transaction.rb
97
79
  - lib/sevencop/cop_concerns/ordered.rb
98
80
  - lib/sevencop/rubocop_extension.rb
99
81
  - lib/sevencop/version.rb
@@ -1,111 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module Sevencop
6
- # Activate a check constraint in a separate migration in PostgreSQL.
7
- #
8
- # Adding a check constraint without `NOT VALID` blocks reads and writes in Postgres and
9
- # blocks writes in MySQL and MariaDB while every row is checked.
10
- #
11
- # @safety
12
- # Only meaningful in PostgreSQL.
13
- #
14
- # @example
15
- # # bad
16
- # class AddCheckConstraintToOrdersPrice < ActiveRecord::Migration[7.0]
17
- # def change
18
- # add_check_constraint :orders, 'price > 0', name: 'orders_price_positive'
19
- # end
20
- # end
21
- #
22
- # # good
23
- # class AddCheckConstraintToOrdersPriceWithoutValidation < ActiveRecord::Migration[7.0]
24
- # def change
25
- # add_check_constraint :orders, 'price > 0', name: 'orders_price_positive', validate: false
26
- # end
27
- # end
28
- #
29
- # class ActivateCheckConstraintOnOrdersPrice < ActiveRecord::Migration[7.0]
30
- # def change
31
- # validate_check_constraint :orders, name: 'orders_price_positive'
32
- # end
33
- # end
34
- class RailsMigrationAddCheckConstraint < RuboCop::Cop::Base
35
- extend AutoCorrector
36
-
37
- MSG = 'Activate a check constraint in a separate migration in PostgreSQL.'
38
-
39
- RESTRICT_ON_SEND = %i[
40
- add_check_constraint
41
- ].freeze
42
-
43
- # @param node [RuboCop::AST::SendNode]
44
- # @return [void]
45
- def on_send(node)
46
- return unless bad?(node)
47
-
48
- add_offense(node) do |corrector|
49
- autocorrect(corrector, node)
50
- end
51
- end
52
-
53
- private
54
-
55
- # @!method add_check_constraint?(node)
56
- # @param node [RuboCop::AST::SendNode]
57
- # @return [Boolean]
58
- def_node_matcher :add_check_constraint?, <<~PATTERN
59
- (send
60
- nil?
61
- :add_check_constraint
62
- ...
63
- )
64
- PATTERN
65
-
66
- # @!method add_check_constraint_with_validate_false?(node)
67
- # @param node [RuboCop::AST::SendNode]
68
- # @return [Boolean]
69
- def_node_matcher :add_check_constraint_with_validate_false?, <<~PATTERN
70
- (send
71
- nil?
72
- :add_check_constraint
73
- _
74
- _
75
- (hash
76
- <
77
- (pair
78
- (sym :validate)
79
- false
80
- )
81
- ...
82
- >
83
- )
84
- )
85
- PATTERN
86
-
87
- # @param corrector [RuboCop::Cop::Corrector]
88
- # @param node [RuboCop::AST::SendNode]
89
- # @return [void]
90
- def autocorrect(
91
- corrector,
92
- node
93
- )
94
- target = node.last_argument
95
- target = target.pairs.last if target.hash_type?
96
- corrector.insert_after(
97
- target,
98
- ', validate: false'
99
- )
100
- end
101
-
102
- # @param node [RuboCop::AST::SendNode]
103
- # @return [Boolean]
104
- def bad?(node)
105
- add_check_constraint?(node) &&
106
- !add_check_constraint_with_validate_false?(node)
107
- end
108
- end
109
- end
110
- end
111
- end