Skip to content

Commit cdaef41

Browse files
committed
Fix an error for Rails/UniqueValidationWithoutIndex
This PR is fix an error for `Rails/UniqueValidationWithoutIndex`.
1 parent b45aef9 commit cdaef41

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1021](https://github.com/rubocop/rubocop-rails/pull/1021): Fix an error for `Rails/UniqueValidationWithoutIndex`. ([@ydah][])

lib/rubocop/cop/rails/unique_validation_without_index.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class UniqueValidationWithoutIndex < Base
3131
RESTRICT_ON_SEND = %i[validates].freeze
3232

3333
def on_send(node)
34-
return if uniqueness_part(node).falsey_literal?
34+
return if uniqueness_part(node)&.falsey_literal?
3535
return if condition_part?(node)
3636
return unless schema
3737

spec/rubocop/cop/rails/unique_validation_without_index_spec.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22

33
RSpec.describe RuboCop::Cop::Rails::UniqueValidationWithoutIndex, :config do
44
context 'without db/schema.rb' do
5-
it 'does nothing' do
6-
expect_no_offenses(<<~RUBY)
7-
class User < ApplicationRecord
8-
validates :account, uniqueness: true
9-
end
10-
RUBY
5+
context 'when using `uniqueness: true`' do
6+
it 'does nothing' do
7+
expect_no_offenses(<<~RUBY)
8+
class User < ApplicationRecord
9+
validates :account, uniqueness: true
10+
end
11+
RUBY
12+
end
13+
end
14+
15+
context 'when using other validation helpers' do
16+
it 'does nothing' do
17+
expect_no_offenses(<<~RUBY)
18+
class User < ApplicationRecord
19+
validates :name, presence: true
20+
end
21+
RUBY
22+
end
1123
end
1224
end
1325

0 commit comments

Comments
 (0)