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 an error for Rails/UniqueValidationWithoutIndex #1021

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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1021](https://github.com/rubocop/rubocop-rails/pull/1021): Fix an error for `Rails/UniqueValidationWithoutIndex`. ([@ydah][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/unique_validation_without_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UniqueValidationWithoutIndex < Base
RESTRICT_ON_SEND = %i[validates].freeze

def on_send(node)
return if uniqueness_part(node).falsey_literal?
return if uniqueness_part(node)&.falsey_literal?
ydah marked this conversation as resolved.
Show resolved Hide resolved
return if condition_part?(node)
return unless schema

Expand Down
24 changes: 18 additions & 6 deletions spec/rubocop/cop/rails/unique_validation_without_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@

RSpec.describe RuboCop::Cop::Rails::UniqueValidationWithoutIndex, :config do
context 'without db/schema.rb' do
it 'does nothing' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
validates :account, uniqueness: true
end
RUBY
context 'when using `uniqueness: true`' do
it 'does nothing' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
validates :account, uniqueness: true
end
RUBY
end
end

context 'when using other validation helpers' do
it 'does nothing' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
validates :name, presence: true
end
RUBY
end
end
end

Expand Down