From adbf53c93302bb15c3ac2673a0dc5378f02ec301 Mon Sep 17 00:00:00 2001 From: Alfonso Uceda Date: Tue, 31 Mar 2026 13:44:13 +0200 Subject: [PATCH 1/6] 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. --- README.md | 14 ++++++----- config/rubocop-default.yml | 5 ++++ database_validations.gemspec | 5 +++- lib/rubocop-database_validations.rb | 4 ++++ lib/rubocop/database_validations.rb | 2 ++ lib/rubocop/database_validations/plugin.rb | 28 ++++++++++++++++++++++ 6 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 config/rubocop-default.yml create mode 100644 lib/rubocop-database_validations.rb create mode 100644 lib/rubocop/database_validations.rb create mode 100644 lib/rubocop/database_validations/plugin.rb diff --git a/README.md b/README.md index cb07fea..21f7659 100644 --- a/README.md +++ b/README.md @@ -247,18 +247,20 @@ end ## Using with RuboCop DatabaseValidations provides custom cops for RuboCop to help you consistently apply the improvements. -To use all of them, use `rubocop --require database_validations/rubocop/cops` or add to your `.rubocop.yml` file: + +For RuboCop >= 1.72, add the plugin to your `.rubocop.yml` file: ```yaml -require: - - database_validations/rubocop/cops +plugins: + - database_validations: + require_path: rubocop-database_validations ``` -Or you case use some specific cop directly: +For older versions of RuboCop, use `require` instead: + ```yaml require: - - database_validations/rubocop/cop/belongs_to - - database_validations/rubocop/cop/uniqueness_of + - database_validations/rubocop/cops ``` ## Development diff --git a/config/rubocop-default.yml b/config/rubocop-default.yml new file mode 100644 index 0000000..868f7f2 --- /dev/null +++ b/config/rubocop-default.yml @@ -0,0 +1,5 @@ +DatabaseValidations/BelongsTo: + Enabled: true + +DatabaseValidations/UniquenessOf: + Enabled: true diff --git a/database_validations.gemspec b/database_validations.gemspec index 1490515..1f8b6ba 100644 --- a/database_validations.gemspec +++ b/database_validations.gemspec @@ -17,11 +17,14 @@ The main goal of the gem is to provide compatibility between database constraint and ActiveRecord validations with better performance and consistency." spec.homepage = 'https://github.com/toptal/database_validations' spec.license = 'MIT' - spec.files = Dir['lib/**/*'] + spec.files = Dir['lib/**/*', 'config/**/*'] spec.require_paths = ['lib'] spec.required_ruby_version = '>= 3.2.0' + spec.metadata['default_lint_roller_plugin'] = 'RuboCop::DatabaseValidations::Plugin' + spec.add_dependency 'activerecord', '>= 7.2.0' + spec.add_dependency 'lint_roller' spec.add_development_dependency 'benchmark-ips' spec.add_development_dependency 'bundler' diff --git a/lib/rubocop-database_validations.rb b/lib/rubocop-database_validations.rb new file mode 100644 index 0000000..518b47e --- /dev/null +++ b/lib/rubocop-database_validations.rb @@ -0,0 +1,4 @@ +require 'rubocop' + +require_relative 'rubocop/database_validations' +require_relative 'rubocop/database_validations/plugin' diff --git a/lib/rubocop/database_validations.rb b/lib/rubocop/database_validations.rb new file mode 100644 index 0000000..e48086e --- /dev/null +++ b/lib/rubocop/database_validations.rb @@ -0,0 +1,2 @@ +require_relative '../../lib/database_validations/rubocop/cop/belongs_to' +require_relative '../../lib/database_validations/rubocop/cop/uniqueness_of' diff --git a/lib/rubocop/database_validations/plugin.rb b/lib/rubocop/database_validations/plugin.rb new file mode 100644 index 0000000..339eed7 --- /dev/null +++ b/lib/rubocop/database_validations/plugin.rb @@ -0,0 +1,28 @@ +require 'lint_roller' + +module RuboCop + module DatabaseValidations + class Plugin < LintRoller::Plugin + def about + LintRoller::About.new( + name: 'rubocop-database_validations', + version: ::DatabaseValidations::VERSION, + homepage: 'https://github.com/toptal/database_validations', + description: 'RuboCop cops for database_validations gem.' + ) + end + + def supported?(context) + context.engine == :rubocop + end + + def rules(context) + LintRoller::Rules.new( + type: :path, + config_format: :rubocop, + value: File.join(__dir__, '..', '..', '..', 'config', 'rubocop-default.yml') + ) + end + end + end +end From 495226ef2074dc4b35eb2c9374792e52881be9a3 Mon Sep 17 00:00:00 2001 From: Alfonso Uceda Date: Tue, 31 Mar 2026 13:49:52 +0200 Subject: [PATCH 2/6] CHANGELOG is updated with RuboCop plugin entry --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4125eae..ccd2cb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog -## [2.0.0] - 20-03-2026 +## Unreleased - xxxx-xx-xx + +- Add RuboCop plugin system via LintRoller for RuboCop >= 1.72 + +## [2.0.0] - 2026-03-20 ### Improvements - Add Rails 8.1 support From 327bf3438699a4f44ff03547eb5d942bb449bc45 Mon Sep 17 00:00:00 2001 From: Alfonso Uceda Date: Tue, 31 Mar 2026 14:58:35 +0200 Subject: [PATCH 3/6] RuboCop plugin lint issues are corrected --- .rubocop.yml | 1 + lib/rubocop/database_validations/plugin.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.rubocop.yml b/.rubocop.yml index 91e9cb0..b597e66 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -18,6 +18,7 @@ AllCops: Naming/FileName: Exclude: + - 'lib/rubocop-database_validations.rb' - 'example/Gemfile' - 'example/Rakefile' diff --git a/lib/rubocop/database_validations/plugin.rb b/lib/rubocop/database_validations/plugin.rb index 339eed7..4f2fdfc 100644 --- a/lib/rubocop/database_validations/plugin.rb +++ b/lib/rubocop/database_validations/plugin.rb @@ -16,7 +16,7 @@ def supported?(context) context.engine == :rubocop end - def rules(context) + def rules(_context) LintRoller::Rules.new( type: :path, config_format: :rubocop, From 5b0c634b73461aa59ce0522823d71636ab200dec Mon Sep 17 00:00:00 2001 From: Alfonso Uceda Date: Tue, 31 Mar 2026 15:12:39 +0200 Subject: [PATCH 4/6] RuboCop spec helper require is corrected Uses rubocop-database_validations instead of plain rubocop to properly load the plugin in specs. --- spec/rubocop/spec_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/rubocop/spec_helper.rb b/spec/rubocop/spec_helper.rb index e504a21..678249a 100644 --- a/spec/rubocop/spec_helper.rb +++ b/spec/rubocop/spec_helper.rb @@ -1,4 +1,4 @@ -require 'rubocop' +require 'rubocop-database_validations' require 'rubocop/rspec/support' require 'database_validations/rubocop/cops' From a5dda48895b7fc507e9f66ca4038c8d065cad684 Mon Sep 17 00:00:00 2001 From: Alfonso Uceda Date: Tue, 31 Mar 2026 15:12:42 +0200 Subject: [PATCH 5/6] rspec_junit_formatter test dependency is removed --- Gemfile | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Gemfile b/Gemfile index 05e7795..f4d2411 100644 --- a/Gemfile +++ b/Gemfile @@ -5,8 +5,4 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } # Specify your gem's dependencies in database_validations.gemspec gemspec -group :test do - gem 'rspec_junit_formatter', '~> 0.4.1' -end - eval(File.read(ENV['GEMFILE_PATH'])) if ENV['GEMFILE_PATH'] # rubocop:disable Security/Eval From 7ce747e719b4e9e18958e4152cd3ff3e7b22b6c9 Mon Sep 17 00:00:00 2001 From: Alfonso Uceda Date: Tue, 31 Mar 2026 15:19:22 +0200 Subject: [PATCH 6/6] DatabaseValidations::VERSION constant is loaded in RuboCop plugin The plugin referenced ::DatabaseValidations::VERSION without requiring the version file, causing an uninitialized constant error when RuboCop loaded the plugin. --- lib/rubocop/database_validations/plugin.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/rubocop/database_validations/plugin.rb b/lib/rubocop/database_validations/plugin.rb index 4f2fdfc..b78a137 100644 --- a/lib/rubocop/database_validations/plugin.rb +++ b/lib/rubocop/database_validations/plugin.rb @@ -1,4 +1,5 @@ require 'lint_roller' +require 'database_validations/version' module RuboCop module DatabaseValidations