Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors of Rails/UniqueValidationWithoutIndex #349

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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