Skip to content

Commit

Permalink
Merge pull request #349 from Tietew/unique_validation_without_index_e…
Browse files Browse the repository at this point in the history
…rror_fix

Fix errors of `Rails/UniqueValidationWithoutIndex`
  • Loading branch information
koic committed Sep 9, 2020
2 parents 1bda5d2 + 095a126 commit fa637ca
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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)

Expand Down Expand Up @@ -281,3 +282,4 @@
[@mobilutz]: https://github.com/mobilutz
[@bubaflub]: https://github.com/bubaflub
[@dvandersluis]: https://github.com/dvandersluis
[@Tietew]: https://github.com/Tietew
1 change: 1 addition & 0 deletions lib/rubocop/cop/mixin/active_record_helper.rb
Expand Up @@ -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|
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/rails/unique_validation_without_index.rb
Expand Up @@ -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)

Expand Down
39 changes: 39 additions & 0 deletions spec/rubocop/cop/rails/unique_validation_without_index_spec.rb
Expand Up @@ -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

0 comments on commit fa637ca

Please sign in to comment.