Skip to content

Commit adbf53c

Browse files
committed
RuboCop plugin system via LintRoller
Adds a LintRoller-based plugin so RuboCop >= 1.72 can discover and load the custom cops automatically through the `plugins:` configuration. The legacy `require:` approach is preserved for older RuboCop versions.
1 parent 25f544d commit adbf53c

6 files changed

Lines changed: 51 additions & 7 deletions

File tree

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,18 +247,20 @@ end
247247
## Using with RuboCop
248248

249249
DatabaseValidations provides custom cops for RuboCop to help you consistently apply the improvements.
250-
To use all of them, use `rubocop --require database_validations/rubocop/cops` or add to your `.rubocop.yml` file:
250+
251+
For RuboCop >= 1.72, add the plugin to your `.rubocop.yml` file:
251252

252253
```yaml
253-
require:
254-
- database_validations/rubocop/cops
254+
plugins:
255+
- database_validations:
256+
require_path: rubocop-database_validations
255257
```
256258
257-
Or you case use some specific cop directly:
259+
For older versions of RuboCop, use `require` instead:
260+
258261
```yaml
259262
require:
260-
- database_validations/rubocop/cop/belongs_to
261-
- database_validations/rubocop/cop/uniqueness_of
263+
- database_validations/rubocop/cops
262264
```
263265

264266
## Development

config/rubocop-default.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DatabaseValidations/BelongsTo:
2+
Enabled: true
3+
4+
DatabaseValidations/UniquenessOf:
5+
Enabled: true

database_validations.gemspec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ The main goal of the gem is to provide compatibility between database constraint
1717
and ActiveRecord validations with better performance and consistency."
1818
spec.homepage = 'https://github.com/toptal/database_validations'
1919
spec.license = 'MIT'
20-
spec.files = Dir['lib/**/*']
20+
spec.files = Dir['lib/**/*', 'config/**/*']
2121
spec.require_paths = ['lib']
2222
spec.required_ruby_version = '>= 3.2.0'
2323

24+
spec.metadata['default_lint_roller_plugin'] = 'RuboCop::DatabaseValidations::Plugin'
25+
2426
spec.add_dependency 'activerecord', '>= 7.2.0'
27+
spec.add_dependency 'lint_roller'
2528

2629
spec.add_development_dependency 'benchmark-ips'
2730
spec.add_development_dependency 'bundler'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'rubocop'
2+
3+
require_relative 'rubocop/database_validations'
4+
require_relative 'rubocop/database_validations/plugin'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require_relative '../../lib/database_validations/rubocop/cop/belongs_to'
2+
require_relative '../../lib/database_validations/rubocop/cop/uniqueness_of'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'lint_roller'
2+
3+
module RuboCop
4+
module DatabaseValidations
5+
class Plugin < LintRoller::Plugin
6+
def about
7+
LintRoller::About.new(
8+
name: 'rubocop-database_validations',
9+
version: ::DatabaseValidations::VERSION,
10+
homepage: 'https://github.com/toptal/database_validations',
11+
description: 'RuboCop cops for database_validations gem.'
12+
)
13+
end
14+
15+
def supported?(context)
16+
context.engine == :rubocop
17+
end
18+
19+
def rules(context)
20+
LintRoller::Rules.new(
21+
type: :path,
22+
config_format: :rubocop,
23+
value: File.join(__dir__, '..', '..', '..', 'config', 'rubocop-default.yml')
24+
)
25+
end
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)