Skip to content

Commit

Permalink
Merge pull request #1021 from ydah/fix_an_error_for_rails_unique_vali…
Browse files Browse the repository at this point in the history
…dation_without_index

Fix an error for `Rails/UniqueValidationWithoutIndex`
  • Loading branch information
koic authored Jun 19, 2023
2 parents b45aef9 + cdaef41 commit 05ad15b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
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?
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

0 comments on commit 05ad15b

Please sign in to comment.