From 095a1263588ce8d4a641a3336cb9dd973ceff61f Mon Sep 17 00:00:00 2001 From: Tietew Date: Tue, 8 Sep 2020 16:35:06 +0900 Subject: [PATCH] Fix errors of `Rails/UniqueValidationWithoutIndex`: - When the table does not exist. - When defining `module` instead of `class`. --- CHANGELOG.md | 2 + lib/rubocop/cop/mixin/active_record_helper.rb | 1 + .../rails/unique_validation_without_index.rb | 2 + .../unique_validation_without_index_spec.rb | 39 +++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ee83edaaf..9c1c75e85d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Bug fixes * [#345](https://github.com/rubocop-hq/rubocop-rails/issues/345): Fix error of `Rails/AfterCommitOverride` on `after_commit` with a lambda. ([@pocke][]) +* [#349](https://github.com/rubocop-hq/rubocop-rails/pull/349): Fix errors of `Rails/UniqueValidationWithoutIndex`. ([@Tietew][]) ## 2.8.0 (2020-09-04) @@ -281,3 +282,4 @@ [@mobilutz]: https://github.com/mobilutz [@bubaflub]: https://github.com/bubaflub [@dvandersluis]: https://github.com/dvandersluis +[@Tietew]: https://github.com/Tietew diff --git a/lib/rubocop/cop/mixin/active_record_helper.rb b/lib/rubocop/cop/mixin/active_record_helper.rb index 8021d60028..2611c068d3 100644 --- a/lib/rubocop/cop/mixin/active_record_helper.rb +++ b/lib/rubocop/cop/mixin/active_record_helper.rb @@ -52,6 +52,7 @@ def table_name(class_node) # @param table [RuboCop::Rails::SchemaLoader::Table] # @return [String, nil] def resolve_relation_into_column(name:, class_node:, table:) + return unless table return name if table.with_column?(name: name) find_belongs_to(class_node) do |belongs_to| diff --git a/lib/rubocop/cop/rails/unique_validation_without_index.rb b/lib/rubocop/cop/rails/unique_validation_without_index.rb index 35c362b978..06cdd8d0f3 100644 --- a/lib/rubocop/cop/rails/unique_validation_without_index.rb +++ b/lib/rubocop/cop/rails/unique_validation_without_index.rb @@ -46,6 +46,8 @@ def on_send(node) def find_schema_information(node) klass = class_node(node) + return unless klass + table = schema.table_by(name: table_name(klass)) names = column_names(node) diff --git a/spec/rubocop/cop/rails/unique_validation_without_index_spec.rb b/spec/rubocop/cop/rails/unique_validation_without_index_spec.rb index 0cb7200fdd..491e3da7b7 100644 --- a/spec/rubocop/cop/rails/unique_validation_without_index_spec.rb +++ b/spec/rubocop/cop/rails/unique_validation_without_index_spec.rb @@ -495,5 +495,44 @@ class User end end end + + context 'when the table does not exist' do + let(:schema) { <<~RUBY } + ActiveRecord::Schema.define(version: 2020_02_02_075409) do + create_table "users", force: :cascade do |t| + t.string "account", null: false, unique: true + end + end + RUBY + + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + class Article + validates :account, uniqueness: true + end + RUBY + end + end + + context 'when module' do + let(:schema) { <<~RUBY } + ActiveRecord::Schema.define(version: 2020_02_02_075409) do + create_table "users", force: :cascade do |t| + t.string "account", null: false, unique: true + end + end + RUBY + + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + module User + extend ActiveSupport::Concern + included do + validates :account, uniqueness: true + end + end + RUBY + end + end end end